Seamlessly Switch from Hardcoded Analytics to GTM

Despite how great tag management tools are, many sites out there still haven’t made the switch. It’s no big surprise, though; to the uninitiated, tag management can seem unnecessary, complicated, or even somewhat terrifying. Not to worry; that’s where people like us come in.

This post will explain how to roll hardcoded page view tracking over to Google Tag Manager page view tracking in a simple and seamless way. What this approach will allow you to do is set up GTM ahead of time, have it running on the site without collecting data, and set it to “listen” for the removal of the old, hardcoded analytics tracking script. At that point, all you need to do is put in a request for your development team/partner to remove the old code and GTM will jump in to pick up page view tracking as soon as the old script is removed. Sound too good to be true? It’s not.

Some basic concepts to make sure we all understand:

  • Hardcoded analytics means you have a script in the HTML of your site’s pages that looks like the script below.
  • If you’re using “Universal Analytics” (the new version), you’ll see a file ending in analytics.js
  • If you’re using “Classic Analytics” (the legacy version), you’ll see a file ending in ga.js
  • You shouldn’t have both running on your site, but you might. One or both of these should be removed if you want to make the full switch to Google Tag Manager so as not to inflate page view counts.

Here’s what the hardcoded analytics script looks like:

See the Pen Sample Google Analytics Script by Mike Arnesen (@mike_arnesen) on CodePen.

The big hurdle, of course, is how to switch over from hardcoded (removing the current analytics script) to Google Tag Manager without either losing data or inflating data (by having both versions on the site at the same time). If you have a proper staging environment set up and a development cadence that doesn’t take forever to get changes pushed through, that’s the ideal process to use: make changes in staging, test to be sure that everything works, push changes to the live site and overwrite the old with the new.

However, businesses without dedicated web teams often don’t have the luxury of doing things the “right way”. Just wanted to clear that up before the comments get flooded with folks asking why we don’t just use a staging environment (we’re not all that lucky).

Step 1

Add your Google Tag Manager container tag to the site — Google’s documentation here. Don’t worry about removing the hardcoded analytics tracking for now.

Step 2

Set up a page view tracking tag in Google Tag Manager with all the relevant info. Don’t worry about creating any exceptions now; we’ll hit that in Step 5.

Step 3

Create a new Variable in Google Tag Manager. Name it something like “js – Hardcoded Analytics Check”. Select the “Custom JavaScript” type and then copy the JS function below into it. What the function does is look into your website’s source code and scan for any hardcoded <script> tag that contains /ga.js (Classic Analytics) or /analytics.js (Universal Analytics). If either of those are found, it returns “TRUE” and we know hardcoded analytics is still on the site. If it returns FALSE, we know we’re safe to fire GTM tracking.

hardcoded-analytics-to-gtm-variable


The completed Variable should look like this.
hardcoded-analytics-to-gtm-variable

Step 4

Create a new Trigger in GTM and name it something like “Hardcoded Analytics Script”. Set it to check if the Variable we just created equals “TRUE”. This Trigger will be used to block our Google Tag Manager page view tracking for as long as there is a hardcoded analytics script on the site. hardcoded-analytics-to-gtm-trigger

Step 5

Modify the existing page view tracking tag in Google Tag Manager to add an “Exception” under the “Fire On” section. Select “Hardcoded Analytics Script” as your “Exception”. Once you save, the tag will not fire if hardcoded analytics is on the site; GTM page view tracking will only fire if that Variable returns “FALSE”.   hardcoded-analytics-to-gtm-tag

A Seamless Switch from Hardcoded Analytics to GTM

Once you hit Publish, you can ask your developer(s) to remove hardcoded analytics from the site. As soon as they make that change and hit save on the live site, Google Tag Manager will figure out that the code’s no longer present and start using Google Tag Manager page view tracking!

Related Posts

Leave a Reply

Your email address will not be published.