GTM Variable: Check if Google Analytics Code is Found in the Page Source

There are times when you need to fire a Google Analytics tag in Google Tag Manager only if you don’t have Google Analytics hardcoded. The best scenario I can think of is a website migration. At least this is something I faced so I needed to find a solution to make the transition seamless and prevent any data loss from GA.

I found a good solution and I’m even not surprised that it had been created by genius Simo Ahava. The original post by Simo is here but in my post, I’ll guide you through more detailed steps of creating the needed variable, trigger and tag. Note that you will still need some basic GTM knowledge to do that.

How to Check for Analytics Code

This method is based on finding a Google Analytics <script> in a page source and returning true if the code is present. Thus you will need to create the following 3 instances in Google Tag Manager:

  1. Variable – a code that scans a page template and returns true if a GA code is found;
  2. Trigger – a rule according to which your tag fires if the variable returns true (it will be used in an exclusion section);
  3. Tag – your simple GA tag.

Making Sure You’re Checking The Right GA Code

Before any further steps, I want to explain you something. The original code by Simo is awesome and works great when it comes to checking if any GA code is added to the page. The key word here is any. So if you have the wrong GA code on the page or use some embedded media with its own GA tracking, your main Google Analytics code won’t fire.

Consider the following scenario:

  • the Variable (JS code) checks for www.google-analytics.com/analytics.js (or www.google-analytics.com/ga.js)
  • the Variable returns ‘true’ as it finds this string in your page source even though this string doesn’t belong to your main GA code
  • the GA Tag doesn’t fire
  • your Analytics doesn’t collect visits stats.

In order to avoid such situations, I added (with my friend’s help, thank you!) one more variable to the code: a unique Google Analytics property tracking ID (UA-119684838-1 in my case). So if you check for this string www.google-analytics.com/analytics.js and it is found in the code but with a different tracking ID, then the Variable code will return ‘false’ and your main GA tag will be fired.

This is very exciting! And even if it doesn’t make sense for you know now, you should understand this by the end of this post.

Create a variable

  • Navigate to Variables -> User-Defined Variables -> New
  • Choose variable type: Custom Javascript
  • Copy this code and paste into the Variable field:
Note: replace the ‘UA-119684838-1’ with your Google Analytics account tracking ID (found under Admin – > Property -> Tracking Code).
function() {
var scripts = document.getElementsByTagName('script'),
ga = true, // set to false if you don't want to check for ga.js
ua = true, // set to false if you don't want to check for analytics.js
dc = false, // set to false if you don't want to check for dc.js
i = len = 0;
if (ga || ua || dc) {
var googleAnalyticsTestWasSuccessfull = false;
var uaElementWasFound = false;

for (i, len = scripts.length; i < len; i += 1) {
if (ga && /www\.google-analytics\.com\/ga\.js/.test(scripts[i].src)) {
googleAnalyticsTestWasSuccessfull = true;
}
if (ua && /www\.google-analytics\.com\/analytics\.js/.test(scripts[i].src)) {
googleAnalyticsTestWasSuccessfull = true;
}
if (dc && /stats\.g\.doubleclick\.net\/dc\.js/.test(scripts[i].src)) {
googleAnalyticsTestWasSuccessfull = true;
}
if (scripts[i].innerHTML.includes('UA-119684838-1')){
uaElementWasFound = true;
}
}
if(googleAnalyticsTestWasSuccessfull && uaElementWasFound){
return true;
}
}
return false;
}

Don’t forget to name your variable, e.g. GA Code True

gtm-check-ga-code-on-page--variable

Set up a trigger

  • Triggers -> New
  • Trigger type: Page view
  • This trigger fires on: choose Some Page Views
  • Choose your variable from the drop-down
  • Set ‘equals’ to true

gtm-check-ga-code-on-page-trigger-true

Create a tag

For this step you will need a simple GA tag. If you do have one set up, then just edit it. If you don’t, here are quick steps of creating a general GA tag to track page views:

  • Tags -> New
  • Tag type: Google Analytics – Universal Analytics
  • Track type: Page View
  • Insert your tracking code or use a Google Analytics setting variable if you have created one

And here comes the most interesting part. Usually, you would want to trigger this GA tag on All pages. But in your case, we also need to check if a GA code is already found on a page and if yes, we want to not fire the GTM tag to avoid double counting. Thus:

  • Add an “All Pages” standard trigger to fire your GA tag across the website
  • Add your newly created trigger to Exceptions to prevent the GA code firing if this GA code is already found in a page template.

gtm-check-ga-code-on-page--ga-tag

Preview and publish your tag

Before submitting anything in GTM, don’t forget to preview how your new tag/tags behave.

This is how my preview looks like on this post about Dynamic Search Ads:

gtm-check-ga-code-on-page-tag-not-firing

As I have a GA code with a tracker ID UA-119684838-1 added to the template, my GTM tag for Universal Analytics isn’t firing. This is the correct behaviour. (I’m going to remove the GA code from the template as I’ve installed it just for writing this post 🙂 ).

After previewing the changes, hit ‘Publish’ in Google Tag Manager. After that, your version will be live.

What if My Site Uses a New gtag.js Library?

If you go and check your tracking code in Google Analytics, you will see that now it uses a new js library – gtag.jsAnd this code differs from the one which is checked by the Variable. See yourself:

Universal Analytics code:

ga-universal-analytics-tag

Global site tag:

ga-global-site-tag

As you can see, there is no ‘www.google-analytics.com/analytics.js’ part in the Global site tag. So at first, I thought that the variable wouldn’t work. But it did.

Though this part (www\.google-analytics\.com\/analytics\.js) is not found directly in the page source, it’s still available in this js file which is linked to from the Global tag:

https://www.googletagmanager.com/gtag/js?id=UA-119684838-1

Here is how it looks like:

gtag-ga-code

Summing It Up

Now you know how to fire a GA tag via Google Tag Manager only if you don’t have this GA tag hardcoded in the page template.

But one thing is for sure – when it comes to website migrations, redesigns and any other serious changes, it’s important to track and check all the changes. This tag helps with GA tracking but it doesn’t solve any potential issues entirely.

[Sassy_Social_Share]