ProgrammerBret
ProgrammerBret

Reputation: 174

Google Analytics real-time active page -- keep alive

I have a site that simply streams a video for as much as two or three hours-- there are no user interactions once the page is loaded; I know google analytics sets the timeout to 5 minutes for the real-time feature. I would like to keep the page alive using javascript until the browser is closed so I get accurate real-time reporting. I am fairly new to js so I am seeking the proper code bit... Thanks

Upvotes: 2

Views: 3038

Answers (2)

Łukasz Rysiak
Łukasz Rysiak

Reputation: 3078

you should implement events interacting with your player API to receive calls from the player - youtube and vimeo have built in javascript api which sends events and you can catch them and record events in GA - typical usage is monitoring play/pause events, tracking playback progress in intervals or % of movie played.

You can check ready to use plugins by Sander Heilbron:

If you use another player you can use these plugins as a skeleton to implement your own api calls.

If you're looking for heartbeat solution you could simply go for solution in this question: Does Google Analytics have a "heartbeat" function for long running web applications?

heartbeat-function-for-long-running-web-applicati
function ga_heartbeat(){
  _gaq.push(['_trackEvent', 'Heartbeat', 'Heartbeat', '', 0, true]);
  setTimeout(ga_heartbeat, 5*60*1000);
}
ga_heartbeat();

BUT! as mentioned in many other places, GA has a limit for requests per session - cap is set to 500, and when the page loads, you actually have around 10-12 tokens to use, and in each second you get more - up to 500. This is a simple DDoS protection on Google side and you have to keep this in mind when developing heartbeats.

Upvotes: 1

Petr Havlik
Petr Havlik

Reputation: 3317

I think the easiest way to go would be sending the repeated events once the user start the streaming. There are also virtual pageviews, but they would inflate the total numbers and might skew some metrics, so I would stick with the events.

The code might look like this:

setInterval(function(){
  _gaq.push(['_trackEvent', 'Video', 'Playing', 'Name of the video?'])
},270000);

The number 270000 represents milliseconds of the interval -- this way the function will be execute every 4.5 minutes (270 seconds). Bear in mind that there are some limits on the number of events sent to Google Analytics (500 hits per visit), so if somebody is watching the streaming videos for the whole day, you might end up loosing them. Otherwise this should be a fine workaround that will work, even though not a "clean" thing.

The benefit of using this is that you can sent other data with events -- like video name etc. Hope this helps.

Upvotes: 1

Related Questions