I am trying to use GA user timings to track the duration of ajax events in our website. I have a centralized ajaxCall function that encapsulates the jQuery ajax call method. This seemed like an ideal place to put the timing for the calls since almost all of our ajax calls go through this method.
The problem that I'm having is that GA does not seem to be registering the timing hits.
Here's the code I'm using for my ajaxCall method:
function ajaxCall(url, data, fnSuccess, dataType, fnFailure)
{
if (!fnFailure)
{
fnFailure = function(jqXhr)
{
var lastDitchHtml = window.getCriticalErrorDialog("An error occurred during the ajax call.");
window.showCeebox(lastDitchHtml, 120, 350);
};
}
var a = $('<a>', { href : url })[0];
var startTime = getTimestampMilliseconds();
$.ajax({
cache : false,
async : false,
url : url,
type : 'POST',
dataType : dataType,
data : data,
success : fnSuccess,
error : fnFailure
});
var endTime = getTimestampMilliseconds();
var duration = endTime - startTime;
window.ga('send', 'timing', 'AjaxCall', a.pathname, duration);
}
Here is the code for the getTimestampMilliseconds method:
function getTimestampMilliseconds()
{
if (window.performance)
{
return window.performance.now();
}
else
{
return new Date().getTime();
}
}
I have tried the alternate syntax for the send using a javascript object for the options as follows:
window.ga('send', {
hitType : 'timing',
timingCategory : 'AjaxCall',
timingVar : a.pathname,
timingValue : duration});
and
window.ga('send', {
'hitType' : 'timing',
'timingCategory' : 'AjaxCall',
'timingVar' : a.pathname,
'timingValue' : duration});
I have verified that the beacon is getting sent to GA through the Chrome Google Analytics Debugger add-in and Fiddler. Fiddler shows that the request to GA returns a small image and a 200 OK result.
I have waited a full 24 hours for the results to show up and nothing.
I have verfied that pageviews and events track just fine, just not timing requests.
Any help would be greatly appreciated.
Further investigation by one of my colleagues found the answer.
The getTimeStampMilliseconds function I wrote was returning a value with a fractional part (e.g., 450.98761), which apparently the Google API doesn't like. Complicating this was the fact that Google didn't return any kind of error code at all, leading us to think everything was ok.
Changing the code to pass only whole number of milliseconds in the timing call fixed the problem.