Found this useful? Love this post
0

Workaround for when the hitCallback function does not receive a response (analytics.js)

Updated with common adblockers / privacy filters (Jan 2014)

I’ve found in some circumstances when sending a command using Google Universal analytics.js, code sitting inside the hitCallback function does not execute.

So it might appear that the hitCallback function isn’t working. Well actually, it’s working just fine (sort-of).

It turns out I had an AdBlocker running which prevented analytics.js from sending the data, and as result, the hitCallback function not firing.

A number of most common AdBlockers / Browser Privacy filters used are:

If you’re having issues, I’d suggest you start debugging with Ghostery first, this software is the most comprehensive analytics blocker out the there listed.

I had a look at the ga object. This is what it looks like when it loads successfully:

analytics.js ga object in it's successfully loaded state.

And what it looks like when it doesn’t load:

analytics.js ga object when it doesn't load

Based on this, I developed the quick solution below:

var url = "http://www.domsammut.com/"

/*
 * Simply checks that the loaded property exists 
 * and that it has a value of true. 
 */
if (ga.hasOwnProperty('loaded') && ga.loaded === true) {
    //Success, ga is loaded
    ga('send', 'event', {
        'eventCategory' : 'Outbound',
        'eventAction' : 'Link',
        'eventLabel' : url,
        'eventValue' : '1',
        'hitCallback' : function () {
            document.location = url;
        }
    });
} else {
    //Not loaded, continue without tracking the data
    document.location = url;
}

While in the past, if a user hasn’t been able to load Google Analytics, it hasn’t been a major concern as the send command fails silently. While this is still the case with analytics.js, you can hook code into Google Analytics that is dependant on successfully completing the send command.

Note: This doesn’t solve the problem above, since the analytics.js script was not able to load at all.

Ideally, Google would expand this function so that you could pass a timeout value to execute your function regardless.

ga('send', 'event', {
    'eventCategory' : 'Outbound',
    'eventAction' : 'Link',
    'eventLabel' : url,
    'eventValue' : '1',
    'hitCallbackTimeout' : 500, // ms
    'hitCallback' : function () {
        document.location = url;
    }
});

Or have another function that gets triggered after X seconds with no response (time either defined by Google or optionally overridden by user).

ga('send', 'event', {
    'eventCategory' : 'Outbound',
    'eventAction' : 'Link',
    'eventLabel' : url,
    'eventValue' : '1',
    'hitCallback' : function () {
        document.location = url;
    },
    'hitCallbackFail' : function () {
        alert("Unable to send Google Analytics data");    
    }
});

In the mean time, hopefully this solution helps you out a little.

Subscribe

You'll only get 1 email per month containing new posts (I hate spam as much as you!). You can opt out at anytime.

Categories

Leave a comment or question