javascriptajaxperformancegoogle-analytics

How to Integrate Google Analytics (PageSpeed and Pageview both) for Ajax Loaded Pages?


I am having a web application which runs completely on Ajax Loaded pages (For fast browsing). I integrated Google Analytics but it's not tracking speed of my Ajax Requests. Can anyone please tell me how to track speed? So, that I can tweak my ajax loaded pages as per requirements.

I've seen some people were suggesting to use ga('send','pagespeed') but I don't know how to implement such ga requests because of low-quality documentations.


Solution

  • You can create two Custom Metrics with the scope hit and the formatting integer:

    When the request is sent, start a timer:

    var ajaxLoadTimeStart = Date.now();
    

    When the response is received, end the timer:

    var ajaxLoadTimeEnd = (Date.now() - ajaxLoadTimeStart)/1000;
    

    Then send a new page view with the AJAX metrics:

    ga('send', 'pageview', {
      'metric1': ajaxLoadTimeEnd,
      'metric2': 1
    });