Four Killer Uses for hitCallback You Need to be Using… RIGHT NOW!


Google Analytics Hit Callbacks

Firing tags in Google Tag Manager (GTM) in the correct order can be, at times, difficult. We always want our important tags to fire first, and often we have specific tags that should fire before or after another tag. GTM introduced a feature called Tag firing priority awhile ago, but that just rearranged when tags would start to load, not necessarily waiting for anything to complete in between.

Then came Tag Sequencing inside of Google Tag Manager, which is extremely helpful. Simo Ahava has a nice writeup of tag sequencing on his blog, and the feature has been an excellent addition to our arsenal of tools.

There’s another great feature that we can use, specifically when dealing with Google Analytics tags, that lets us wait for the Google Analytics command to finish and then executes something else immediately after. Imagine a swimming relay during the Olympics, where one swimmer waits patiently on the block for Michael Phelps to finish his leg of the relay. The next action cannot start until the first one completes.

Behold the hitCallback

The hitCallback is a whizzbang feature of Google Analytics (GA). It allows you to execute code after a hit is successfully sent to GA. One of the benefits of using the hitCallback is that it will work with on-page code, as well as in Google Tag Manager. In GTM, the hitCallback is configured in the ‘fields to set’ section of a GA tag.


Per GA’s Developer documentation the callback executes a function on success. In GTM, we can achieve this using a custom JavaScript variable that executes a function that returns another function we want to be run on success. It looks something like this:

In the past, this has been handy, especially for lead generation sites. Whenever a user submits a form, the POST process is interrupted, the GA hit is sent and then the hitCallback allows the process to continue. The inner workings of this process can be found here.

Today, form submits can easily be handled using Google Tag Manager and its wait for tags and check validation.

Yet, that still leaves us with hitCallback. How else can we take advantage this great feature? (I’m so glad you asked!) One thing we can do is write or clear persistent data.

Get ready to dive in, below are a few examples:

1. Fetch and Send the Client ID

(Courtesy Dan Wilkerson)
Getting the Client ID (CID) into GA can prove very beneficial in troubleshooting and analysis. However, if you try to access the Client ID before its been created and stored, then you won’t get any information.

We can use hitCallback to 1) wait until the pageview has been sent and then 2) send an event straight after the pageview sends. This way, if they are a new user we will receive their client ID immediately after the tracker is set and the pageview sends.

With this method we send a GTM event (storeCid) that triggers a GA Event tag (lots of different definitions of the word “event”, I know) that includes the CID as both a custom dimension and as an event label.


Here’s our client ID JavaScript:

2. Establish Membership Values

Have a loyalty or rewards member program? Maybe you want to know when a user is logged in? Whenever some successfully signs up or signs in, we can establish a cookie that successfully registers them accordingly. Here, we are using LunaMetrics’ Cookie Management GTM Recipe to set these values:

We leverage hitCallback to initiate this dataLayer push after each successful GA pageview. We can then pick up this value as a 1st party cookie variable in GTM and (re)use this value as they return to the site.

3. Null Content Metadata

Infinite scroll content makes for great user engagement, but can prove problematic especially when using the data layer for sending contextual data to GA via GTM. Using data layer pushes will update prior information, but can allow preexisting values to carry over to the next pageview. If a custom dimension is set from the data layer on one hit, but you don’t want it to be set necessarily on subsequent hits, than you might need something like this.

For instance, suddenly an Author is given credit for content they didn’t write! Why not clear out those values using hitCallback?

This way the ‘slate’ is clean before the next hit to be sent to GA. This will keep your content meta data fresh as each article loads!

4. Clean up GA Events/Virtual Pageviews

Sometimes, we modify existing JavaScript on the page to create data layer pushes to trigger GA Events in GTM. This process can be handy and speed up migration, but doesn’t always benefit from the full functionality of using Google Tag Manager.

Also, just like above, relying on data layer variables can prove problematic if they’re not set with regularity. We can also clear these values for GA events and virtual page paths being sent through data layer pushes.

Beyond having multiple events on the page, this could be very beneficial for single page applications provided they are using the data layer and ‘fields to set’ to report changes in page path.

In the end, hitCallback can be leveraged in some interesting ways that complement the existing Tag sequencing and tag firing priority features inside of GTM.

Dan is a Manager at LunaMetrics with an Agency/Development background. He has over five years of experience working with Google Analytics. He loves leveraging data to help clients make better decisions. Dan loves new methods and lives by the motto, “break it and make it better.” Dan is a husband and father of four future data science-loving sons. When not putting kids to bed, he writes songs and plays guitar.

  • Scott Kublin

    Hi Dan – What I wanted to do for a client I didn’t think was possible. But after reading this article, it opened up some possibilities. My client would like to have a goal created in Google Analytics that is triggered when their website receives traffic from a “New Visitor”, AND who doesn’t “Bounce”. Do you think that by using the hitCallback functionality, you could trigger an Event when the two criteria match, which could then be used as a Goal conversion?

    • Daniel Settlemire

      Hello, Scott:
      While this may be possible, I’d recommend using the count of users based on a new user, non-bounce segment. Have you ruled out segments already?
      Goals are great for capturing broad, general markers to success, but become somewhat lacking when it comes to evaluating multiple non-hit-based factors. I hope this helps.

      • Scott Kublin

        Hi Dan – Thanks for replying back! I haven’t ruled out segments but was trying to find an easier way for someone who isn’t very familiar with setting up segments to easily see this type of visitor when viewing goals. Also, it bothers me when I can’t figure something out and I feel like there has to be a way to collect this information via the data layer and then use a hitCallback to fire off the event.

Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187