Vinod Kumar

Vinod Kumar

  • NA
  • 2
  • 1.5k

Google Analytics User timings - Page Usage

Nov 18 2015 1:50 AM

Using google analytics user timings api to track the Page Usage ( Time to track how long the user stays on the Specific Page ). Have achieved the following by using the Logic.

  1. Initialize the start Time when user navigate to Page ( Eg. User Details Page )

    document.onreadystatechange = function () { var startUserTrackTime = new Date(); }
  2. User will fill up few data ( Name , Email, Age ) FYI - No post back / no operations performed as simple as that.

  3. User navigate to other pages ( Have to Track the time now )

    window.onunload = function () {     trackPageUsage(); }  function trackPageUsage() { if (startUserTrackTime) {         ga('send', {             timingCategory: 'xx',             timingVar: 'xx',             timingValue: new Date().getTime() - startUserTrackTime.getTime(),             timingLabel: 'xxx' }); } }
  4. Will calcuate the time-stamp, returns the number of milliseconds.

Few doubts points are :

Considering the user stays for 10 minutes on ( User Details Page ). Will the code work to get correct milliseconds :

timingValue: new Date().getTime() - startUserTrackTime.getTime()

Is the approach taken to track time for specific page is correct or possible fault is seen.

I don't want to use the Performance API ( Does not suit my requirement )