hitcallback thumbnailI learned early on in my analytics career that 100% accuracy is not attainable in this business. And while we deal with incomplete and messy data all the time, we always look for ways to improve the quality of data we collect and apply best practices to give marketers more confidence in their analytics reports and optimization efforts.

There could be several reasons your Google Analytics code is not working properly. For example, we sometimes get stuck in a situation where you run into a race condition and you want to be certain that your Google Analytics tracking beacon is sent to servers, especially if you’re tracking an outbound link or form where the thank you page resides on a 3rd party domain, but in a lot of cases, the browser will redirect away from the page before your Google Analytics data can even be registered.

Traditionally, the solution that we’ve seen (and have used ourselves) is to use the JavaScript function setTimeout to create a delay that allows the GA tracking enough time to execute. This is not an ideal solution for several reasons.

Much better is hitCallback. hitCallback is the magical solution you need ensure that the JavaScript executes before the browser leaves the page. In this (technical) post, I’ll share the technique to help you capture key data elements that you might be missing today (and thus improving the quality of your reports).

What the heck is “hitCallback”?

“Callback” is a piece of executable code that is passed as an argument to another function.
So hitCallback is a JavaScript function that is passed as an argument to another Google Analytics method.

ga('send', 'pageview', {'page': '/my-new-page', 'hitCallback': function() {CALLBACK CODE GOES HERE;}});

In the above example, the code is pushing pageview to Google Analytics servers. Once done, it’ll execute whatever code you have in the area bolded. So for example, you can replace the above bolded text with actions like form submission code, etc. to ensure that first your pageview data is sent to GA 100%, ONLY THEN will the form be submitted and redirect the visitor to a thank you page.

Nested Callbacks

You can also utilize hitCallback to control the order of executing your code.
The below example illustrates how to control the order of Google Analytics data beacon transfer to servers. We’ve also added alerts so you can see when each step is executed:

ga('send', 'pageview', {'page': '/vp/page.html, 'hitCallback': function()
       alert('Pageview data has been sent to GA. Anything else?');
       ga('send', 'event', 'Form', 'Submit', { 'hitCallback' : function ()
           alert('Event data has been sent to GA. Anything else?');
           alert('now submitting the form');
           javascript:document.paymentForm.submit();
       }});
  • In the first line we send a virtual pageview and define a callback function where an alert is executed.
  • Then we fire another callback method inside the first callback where we send an event to Google Analytics.
  • Finally we submit the payment form which redirects the user away from the current page.

Is hitCallback for Universal Analytics only?

No! hitCallback can be used for both Universal and traditional Async version of Google Analytics as follows:

_gaq.push(['_set','hitCallback',function() {
    javascript:document.paymentForm.submit(); // Submit underlying form
}]);
_gaq.push(['_trackEvent', 'Form', 'Submit']);

In this example the event will be pushed to GA, and only then will the form will be submitted.

Pinterest: Example of the benefits of hitCallback

pinterest iconLet’s say you have a pinterest button on your website page, and you had planned to track it as both social and an event in Google Analytics. You tagged the button by adding _trackEvent and _trackSocial methods to the onclick of the pinterest button.

onclick="_gaq.push(['_trackEvent', 'Social', 'Pinterest', thislink]);
_gaq.push(['_trackSocial', 'pinterest', 'pin', thislink, document.URL]);"

You expected to see data in your Google Analytics reports but for some reason, you can’t find it. What happened?

The browser redirected your visitors to the pinterest.com before executing trackEvent and trackSocial methods. So both methods were aborted and nothing was sent to the Google Analytics servers.

google-analytics-service-requets

hitCallback solves your problem:

onclick="var thislink = this.href; _gaq.push(['_set','hitCallback',function() { window.location = thislink; }]); _gaq.push(['_trackEvent', 'Social', 'Pinterest', thislink]); _gaq.push(['_trackSocial', 'pinterest', 'pin', thislink, document.URL]); return false;"

This is how the code works:
It stops the “pin it” anchor from auto-redirect by adding “return false;” to the onclick of the anchor.
window.location is the callback method that will redirect after sending data packets to Google Analytics Servers.

Conclusion

You could be getting form submissions or pins, but not measuring your data accurately. How do you know where to optimize? But now, you can ensure everything is tracked properly and executed in the correct order, so your reports are precise.

Other scenarios you have and you’d like to share? We love to hear your feedback

Message Sent

Thank you for registering.

Cardinal Path hosted a live session to connect with you and answer all your questions on Google Analytics.
Get all the expertise and none of the consultancy fees in this not-to-be-missed, rapid-fire virtual event.

Thank you for submitting the form.

Thank you for submitting the form.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you.

Click here to download access the tool.

Message Sent

Thank you for registering.

Message Sent

Thank you.

Message Sent

Thank you.

Message Sent

Thank you

Message Sent

Thank you

Message Sent

Thank you.

Message Sent

Thank you

Message Sent

Thank you.

Message Sent

Success!
Your message was received.

Thank you.

Thank you for registering.

Cardinal Path is continuing with its series of free training. Next we are conducting training on Google Data Studio. Check it out here.

Message Sent

Thank you for registering.

Thank you for your submission.

Your request has been submitted and a rep will reach out to you shortly.

Message Sent

Thank you for your interest.

Thank you for registering.

You should receive a confirmation email from GoToWebinar with your unique webinar login information. If you do not receive this email or have trouble logging in to the event, please email asmaa.mourad@cardinalpath.com.

Thank you for subscribing!

You're now looped into the world's largest GMP resource hub!

Thank you for your submission.

Thank you for your submission.

Thank you for your submission.

Thank you for your submission.

Message Sent

Thank you for registering.

Message Sent

Thank you for your submission.

Thank you for your submission.

Message Sent

Thank you for registering.

Thank you for registering.​

Paid media spend by Government websites increased a whopping 139% YoY in 2020.

2020 Online Behavior Live Dashboard

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

2020 Online Behavior Live Dashboard

Thank you for your submission.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Success! Thank you
for reaching out.