Reputation: 2223
Edit: Here was the code that was acutuallynot working
(I had changed it to simplify the question)
<script>
// save startTime at the begginning of the page
var _startTime = new Date().getTime();
</script>
<!--
... page code here ...
-->
<script>
//Google Analytics code
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXX-Y', 'auto');
ga('send', 'pageview');
(function (w,d,$,ga,createFunction) {
createFunction = function(timingVar) {
return function() {
var time = (new Date().getTime() - _startTime);
if (time > 0) {
ga('send', 'timing', 'performance', timingVar, time, 'desktop');
} //if
if (console && console.log) console.log(timingVar +':'+ time);
};
};
$(d).on('ready',createFunction('page ready'));
$(w).on('load',createFunction('page load'));
})(window,document,jQuery,ga);
</script>
Still not sure why this code is not working.
Here is my Original Question (code works)
I'm trying to use User Timings function of Google Analytics to track page load times under different scenarios.
I'm unable to get the timings command to be sent on page load. Here is the code I am using to get this to happen.
<script>
// save startTime at the begginning of the page
var _startTime = new Date().getTime();
</script>
<!--
... page code here ...
-->
<script>
//Google Analytics code
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXX-Y', 'auto');
ga('send', 'pageview');
// track document ready even time
$(document).on('ready',function() {
var time = (new Date().getTime() - _startTime);
if (time > 0) {
ga('send', 'timing', 'performance', 'page ready', time, 'desktop');
} //if
console.log('page ready:'+ time);
});
// track window load time
$(window).on('load',function() {
var time = (new Date().getTime() - _startTime);
if (time > 0) {
ga('send', 'timing', 'performance', 'page load', time, 'desktop');
} //if
console.log('page load:'+ time);
});
</script>
I'm testing this in Chrome and have the Google Analytics Debugger extension installed.
I look in the console and I see the pageview and timings "page ready" commands being sent, but the timings "page load" command is never sent.
Both console.log()
show the events being fired, yet there is no data sent to Google Analytics for the timings "page load" command.
Here are other verifications I've done:
Here is the results I get when having only the pageview and timings page load event:
Any ideas on what might be the reason the command is not sent on window's load event?
Upvotes: 2
Views: 1570
Reputation: 30441
Something must be different about the code you're actually running and the code you've pasted on this page. Everything here looks fine to me, and if I run your exact code, it works.
You can take a look at this jsbin demo for proof. Notice how three hits are sent: 1 pageview and 2 timing hits.
http://jsbin.com/kafeduweme/edit?html,console
Upvotes: 2
Reputation: 1529
This is not a mistake. This is about default configuration of tracker itself. This functionality is limited by sampling and this feature has 1% sampling by default for timing tracking function. So there is very low posibility to see event in console.
Set sample rate when initiating tracker:
ga('create', 'UA-XXXX-Y', {'siteSpeedSampleRate': 50});
Field reference: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#siteSpeedSampleRate
Feature description: https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings
Upvotes: 0