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:
- The value of time is always an Integer.
- It doesn't matter if I only have the "page load" timings command
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?
ga('send', 'timing', ...)commands? It should either say success or tell you what failed. - Philip Walton