Jiew Meng Jiew Meng - 3 years ago 178
Javascript Question

Google Analytics Screen Time Incorrect when using Measurement Protocol

I am using the measurement protocol for my Tizen TV application since I cannot use the JS (requires a domain name) nor Android/iOS SDKs.

I am sending

{
v: 1,
tid: GA_TRACKING_ID,
cid: data.deviceId,
t: 'screenview',
dh: 'something.com',
dp: encodeURIComponent($location.path()),
cd: transition.to().title + ($stateParams.gaTitle ? ' (' + $stateParams.gaTitle + ')' : '') || 'Unknown',
an: 'XXX',
'ga:mobileDeviceModel': data.deviceModel
}


To
https://www.google-analytics.com/collect


But the screen times seem off its always in the seconds eg. 30s etc. I tested staying on page for a long time but it does not seem correctly reflected. I guess its because I only send this hit once and theres no way for Google to know when it stopped? Is there a way to fix this?

Answer Source

First you need to decide on the session timeout (Admin->property->tracking.js) the default of 30 minutes means you will need to generate hits at intervals bellow 30 minutes to prevent new hits from being in a new session.

Then you need to make sure hits are frequent enough and include their current page/screen names, for example:

{ // start video
        v: 1,
        tid: GA_TRACKING_ID,
        cid: data.deviceId,
        t: 'screenview',
        dh: 'something.com',
        dp: encodeURIComponent($location.path()),
        cd: transition.to().title + ($stateParams.gaTitle ? ' (' + $stateParams.gaTitle + ')' : '') || 'Unknown',
        an: 'XXX',
        'ga:mobileDeviceModel': data.deviceModel
}
{ // < 30 minutes later
        v: 1,
        tid: GA_TRACKING_ID,
        cid: data.deviceId,
        t: 'event',
        ec: 'Inactivity',
        ea: 'Watching Video',
        el: ..video name..,
        ev: 28,
        ni: 0,  // count as interaction, ni=1 are ignored in time calculations 
        dh: 'something.com',
        dp: encodeURIComponent($location.path()),
        cd: transition.to().title + ($stateParams.gaTitle ? ' (' + $stateParams.gaTitle + ')' : '') || 'Unknown',
        an: 'XXX',
        'ga:mobileDeviceModel': data.deviceModel
}
{  // user does something (can wait 30 minutes more before a new ni event)
        v: 1,
        tid: GA_TRACKING_ID,
        cid: data.deviceId,
        t: 'event',
        ec: 'Activity',
        ea: 'Volume Adjustment Down',
        el: ..video name..,
        ev: 5,
        ni: 0,
        dh: 'something.com',
        dp: encodeURIComponent($location.path()),
        cd: transition.to().title + ($stateParams.gaTitle ? ' (' + $stateParams.gaTitle + ')' : '') || 'Unknown',
        an: 'XXX',
        'ga:mobileDeviceModel': data.deviceModel
}
{  // user goes to new screen (also calculated as the end of screen time)
        v: 1,
        tid: GA_TRACKING_ID,
        cid: data.deviceId,
        t: 'screenview',
        dh: 'something.com',
        dp: encodeURIComponent($location.path()),
        cd: 'somewhere else',
        an: 'XXX',
        'ga:mobileDeviceModel': data.deviceModel
}

If you have the ability to send on all exit events then you may want to use queue time on exit (or every 4 hours) to calculate for the session after all the data for the period is in.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download