Every now and then we want to create a bridge between the stateful machines we send data to (e.g. Google Analytics), and the stateless environment where we collect the data itself (e.g. Google Tag Manager). This is not easy. There is no synergy between Google Analytics and Google Tag Manager which would let the latter understand anything about things like sessions or landing pages or Bounce Rates.

One thing we can reliably measure, however, is whether or not the visitor is a New User in Google Analytics. This is done by checking whether or not the Universal Analytics cookie _ga exists in the user’s browser when they enter the site. Naturally, this check needs to happen before the Universal Analytics Tag has time to create the cookie, which is the only difficult thing to solve here.

X

The Simmer Newsletter

Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava into your email inbox!

Tip 32: Check for a new Google Analytics user

The process is quite simple. When the user lands on the page, check if they have the _ga cookie in their browser. If not, then they’re a “New User” in Google Analytics. Once this check is made, you can do whatever you want with this information (with one caveat, see below).

In any case, the first thing you need is a 1st Party Cookie Variable, which checks for the existence of the _ga cookie:

This would return undefined if the cookie does not exist, so we can check against this in a Custom HTML Tag.

The only thing to solve here is when to fire the Custom HTML Tag. Basically, you have two options. Either upon an event that takes place before the first Universal Analytics Tag on the page fires, or upon the same event which fires the first Universal Analytics Tag.

If you make the Custom HTML Tag fire upon an event which happens after the Universal Analytics Tag is fired, it’s possible that a race condition emerges, and the UA Tag has time to create the cookie before the check is made.

Still following?

The Custom HTML Tag could look like this:

<script>
  if (!{{Cookie - _ga}}) {
    window.dataLayer.push({
      'event' : 'newUser',
      'ga_newUser' : 'true'
    });
  }
</script>

This Tag would push a dataLayer object which has an event and an arbitrary key (ga_newUser in the example). You can use these to fire some Tag or run some functions that you only want to run for Google Analytics “New Users”.

UPDATE

As pointed out by both dusoft and Stephen Harris in the comments (thanks guys!), you can use tag sequencing to send user status information to the main Tag without having to build a clumsy event chain. You can follow the guide linked to in the previous sentence, and simply update GTM’s data model with google_tag_manager[{{Container ID}}].dataLayer.set('ga_newUser', 'true') in the Custom HTML tag.

This way the Custom HTML tag, being the setup tag of the sequence, updates the value of ga_newUser in GTM’s data model, and it is then available to the main tag to use in a Data Layer Variable.

Caveats

So, there are some caveats here. First, it’s most likely that your Universal Analytics Tag fires on the “All Pages” Trigger. That’s the most common setup. If you set the Custom HTML Tag to fire upon the All Pages Trigger as well, any information you push into dataLayer in the Custom HTML Tag will not be available to the Universal Analytics Tag. Google Tag Manager has a fixed Data Layer for the duration of an event, so any changes will not be available until the next Data Layer event.

Also, it’s possible that the user is a “New User” even if they have the _ga in the browser. The cookie lifetime is 2 years, so it’s entirely possible that the cookie exists due to an older installation or something. Also, the page can collect to a number of Universal Analytics trackers, each with different cookie configurations. The cookie name could be different, or the cookie domain could vary. In this case, just looking for _ga might not be robust enough to identify a New User. You’ll simply need to adapt to how your site’s Universal Analytics configuration is setup.