Search code examples
javascripthtmlgoogle-analyticsheartbeatpageviews

Google analytics - hearthbeat, keep alive visitor in HTML5 game


I have online HTML5 game, with many players, but I have problem with goole analytics.

Count of online users not equal to total players on my servers. Google analytics kill after few minutes session and user disapere from google analtyics (real time), but I want see in google analytics real online users.

Is there any method like heartbeat ? I found only this:

        function ga_heartbeat(){
          _gaq.push(['_trackEvent', 'Heartbeat', 'Heartbeat', '', 0, true]);
          setTimeout(ga_heartbeat, 5*60*1000);
        }
        ga_heartbeat();

Unfortunely it's not working, I also try set life of session to 2 hours, same problem.

Another solution can be send every 5 minutes pageview, but it will ruin other statistics.

      ga('send', 'pageview');

How I can solve this problem ?


Solution

  • Update your "heartbeat" to use Universal Analytics syntax:

      function ga_heartbeat(){
              ga('send','event','<category>','<name>')
              setTimeout(ga_heartbeat, 5*60*1000);
            }
            ga_heartbeat();
    

    Where category and name are placeholders.

    Since this is a game and people are presumably making progress you might want to send some game information with that event call instead of creating pointless data.

    Changing the session duration does not help (as you found out) since the time window for the realtime report works independently from the session settings.