Safely Migrating To Google Tag Manager | LunaMetrics

Safely Migrating To Google Tag Manager

/

blog-safely-migrate-gtm

This is a guide for technically-proficient users who are looking to safely migrate from on-page Google Analytics code to Google Tag Manager. You should be comfortable with JavaScript, web applications, and web servers.

Google Tag Manager offers an elegant solution for quickly adding marketing-specific tracking without cluttering up source code or backlogs. The process isn’t all roses, there’s a certain level of technical expertise needed and security concerns are often brought up (though GTM offers a lot of features to address these concerns!). However, the benefits of better and more agile data collection far outweigh these potential negatives.

Migrating a very large site to GTM can pose some serious challenges: if done incorrectly, data collection can stop while the code is switched (bad), become dirtied by conflicting code (very bad), or cease altogether (extremely bad). The end goal of this process is that you data in Google Analytics remains constant, without any disruption when you make the transition.

This migration guide is a result of our many hours consulting on Google Analytics and Google Tag Manager, and what we collectively have found to be the best way to manage the process. We also present this guide

This guide assumes you have a working knowledge of Tags, Triggers, and Variables and doesn’t stop along the way to explain the ins-and-outs. If you’d like to take a crash course, we offer a full-day Google Tag Manager workshop in cities all across the United States.

Step #1: Duplicate Your Current Configuration

Create a new Property in your Google Analytics account, then copy all of the configurations from your primary Property and View. If you’re currently using Google Analytics Classic (your code uses the _gaq.push([ ... ]); syntax, you will also be migrating to Universal Analytics.

Screen Shot 2015-06-09 at 4.05.00 PM

Step #2: Create Your Google Tag Manager Container & Add A Pageview Tag

If you haven’t already, sign up for an account at tagmanager.google.com. Create a new container and add the GTM snippet to your site.

Ideally, this should be placed immediately after the opening tag and should not be nested in another HTML element (more).

On some browsers, having an iframe in the <head> can cause issues; this is why GTM is placed in the body. For enhanced performance, or if placing the tag within the body is not possible, place the <noscript> section of the snippet in your footer and the <script> section of the snippet as close to the top of your <head> as possible.

Inside the Google Tag Manager interface, select the Variables section from the left-hand menu, then click the New button. Select the Constant Variable type, and paste the UA Number from the Google Analytics Property you created. Name the Variable GA Tracking ID.

constant-ga-id

Next, click the Tags menu item, and again click New. Select Google Analytics, then Universal Analytics. In the Configuration section, enter {{GA Tracking ID}}. Click Continue, and then All Pages, then Save. You will now track pageviews in two Properties; one from your on-page code, and the other from your Google Tag Manager container.

Step #3: Locate Other Tracking

Most IDEs and text editors will allow you to search through all of the files involved in your site or application; do a search for _gaq.push( if you’re using Classic code or ga( if you’re using Universal Analytics code. Map out every instance of on-page tracking on your site, what it does, and when it fires.

In this step, we’re looking for things like Events that track user interactions, as well as more crucial things like Ecommerce Tracking.

Step #4: Recreate Other Tracking in GTM

In Google Tag Manager, recreate all of the other tracking you discovered hardcoded on your site. Sending information to Google Analytics can be replicated using the built in Universal Analytics tag. You may have custom JavaScript to help track certain events, like PDF downloads, etc. These custom scripts can often be replaced using Triggers, for instance, a Click trigger that is enabled on all pages and fires when the {{Click URL}} contains “pdf”. (More info about Triggers)

For advanced features, refer to the GTM Documentation or do a site search of LunaMetrics. The goal is to have both properties receive identical tracking, with your Test Property receiving its hits from Google Tag Manager.

Step #5: Collect Data & Verify

Remember that you can use the Preview feature to test as you go, looking at your live site to make sure Tags are firing at the appropriate times. Once all tracking has been duplicated in your Google Tag Manager container, publish the container on your site. Collect data for a few days, and then compare major numbers between your old Property and your test Property. Look at metrics like:

  • Users
  • Pageviews
  • Sessions
  • Top Pages
  • Top Events
  • Bounce Rate
  • Conversion Rate
  • Goals/Transactions

These numbers should be within 1-2% of each other; they will not match exactly. If they appear to be out of sync, investigate your implementation and see if you can spot where things are different. Setting the Advanced Configuration -> Debug Mode to {{Debug Mode}} will cause Google Analytics to print detailed information about the hits it is sending to the Developer Console.

If you discover discrepancies, continue to test until they are resolved.

Step #6: Remove On-Page Code & Switch GTM To Using Production UA Number

Once your test Property data matches what you see in your production Property, it’s time to remove the old code from the page. In Google Tag Manager, first create a new Data Layer Variable. Name it fireGtm, and set the Data Layer Variable Name field to the same thing.

Next, change your Google Analytics UA number from a Constant to a Lookup Table Variable. Select fireGtm as the Input Variable. Add one row and set the input to true. In the Output field, enter in the UA number of your original on-page Property (UA-XXXXXX-YY in the example below). Finally, check the Set Default Value box and enter in the UA number of your testing Property (UA-XXXXXX-YZ in the example below). It should look something like this:

You can now publish your container. At this point, your Test Property will still be receiving data. Next, replace the old on-page GA snippet with the below and remove any other on-page code left on the site, simultaneously. The snippet below must be placed above the GTM snippet, or this will not work.

Push your changes into production. Google Tag Manager will begin to fire tracking to your production Google Analytics account as soon as it detects the fireGtm Data Layer Variable, which it will only see if the GA code has been removed. This prevents any out-of-sync page loads from dirtying your data, either by firing both, or neither.

After a week or two, after you’ve definitely removed the old code from every page on your site, you can change your GA Property ID Variable back to a Constant, and this time, you can set it to your Production UA number, and then publish a new version of your container. You can then remove the fireGtm.

Make sure to add an annotation to your Google Analytics to mark the transition!

If you have questions about the process or are looking for additional technical guidance, drop me a line in the comments.

Dan Wilkerson is a Software Engineer at LunaMetrics. He is passionate about web technology, measurement, and analysis. Dan is the winner of the 1999 Forge Road Elementary School Science Fair for his groundbreaking report on how magnets work. (ICP, take note.) Dan has worked at LunaMetrics in social media, as our marketing manager, and now in our analytics department.

  • https://www.sunnyreports.com bastienSR

    the second post I read on your blog I discovered some minutes ago. Re wow ! Amazing content you share with us. Posts of this quality are rare. Thanks.

  • Craig Gorton

    Dan,

    I haven’t been able to find an answer to this question, so I thought I’d try you.
    I am using sampling (for a high volume site). I have it configured in GTM, no problem. My question is, if I also have event tracking tags in that GTM container, should they also get the sampling configured, or do they “inherit” it from the basic pageview sampling?

    So, my pageview tag is set to a 10% sample rate. In the event tag, there is also the option to set that parameter, but if I do so, will I be sampling 10% of the 10% sample?

    Thanks,
    Craig

    • Dan Wilkerson

      Hi Craig,

      Yes, you do. Each time you fire a Tag in GTM, it creates a brand new tracker, with brand new configurations. This means any setting you’d like to see on every hit, e.g. cookieDomain or sampleRate, must be set on each tag.

      Dan

  • tenacious_d1

    Thanks for writing this. It’s been surprisingly hard to find info on how to migrate from google analytics.

    • Dan Wilkerson

      No problem!

  • Luke

    I have had to remove so many event tags to migrate – here is regex to match UA event tags for mass deletion 🙂
    ^on(c|C)lick.*’);”
    just make sure that you have no other onClick events in hrefs because that could potentially match them
    btw thanks for this article!

    • Dan Wilkerson

      Hi Luke,

      You could also search for the methods that Google Analytics uses to fire your events; _gaq.push( … ) or ga( … ). Good luck!

      Dan

  • Leo

    Do I remove the datalayer script when I remove the fireGtm variable. if there is no previous tags , just migrating from UA to UA via GTM do I need to do step 6. Thanks in advance

    • Dan Wilkerson

      If there’s nothing else in there besides fireGtm, you can remove the entire script tag.

      Dan

  • Danny Rivera

    In a couple of months we will be switching websites, the new website will be on WordPress and hosted by another organization. On our current site we aren’t using GTM, all of the coding snippets are just being individually added. My question is, should I start using GTM with our current site, or should I wait until our new site is created? Also, just so you know the new site will have a different URL…

    • Dan Wilkerson

      Hi Danny,

      A transition like this will present many challenges. Can you tell me more about the changes? I have a few questions that will influence my recommendations. Maybe it would be better to send me a private note? You can email me at dwilkerson@lunametrics.com.

      Dan

  • Quaani

    Hi Dan, please help, my question might be quite dump but I need clarification anyway. I am migrating from hard coded to Tag Manager. What happens to current goals in analytics? Do I make sure I setup exact same in Tag Manager for Analytics to populate Data?

    • Dan Wilkerson

      Hi Quanni,

      If you follow the above method, you will replicate all of your current tracking in a test property. This includes replicating your goals. After collecting data to ensure everything matches up, you’ll just switch the UA in Tag Manager with the current hard-coded UA. You shouldn’t have to adjust your current goals or anything like that in order to manage this transition.

      Also, don’t forget – Tag Manager doesn’t *replace* Google Analytics, it is just a different way of getting the data into Google Analytics. Since goals are configured in the interface and are calculated based on data you’re already sending, there is no necessary code change in order to measure the same things with GTM.

      Dan

  • http://www.strangecatholic.com Joe Feeley

    Great post Dan!

    Just one quick question:
    At the end of your post, you say “Push your changes into production.” Can you explain what you are referring to here? Are you referring to the steps after that sentence or something else?

    Thanks!

    • Dan Wilkerson

      Hi Joe,

      Production is a term commonly used to refer to the live version of a website. In larger or more tech-oriented companies, as sites are updated, they typically go through several stages before being ‘released’ (e.g. the changes are published on the live site). I’m referring to this process!

      Dan

  • n

    Hi Dan, Great article. At the fireGtm step, is this so that we don’t lose any historic analytics data? Thanks.

    • Dan Wilkerson

      Yes! That is part of the reason; the other reason is to prevent cached versions of your site from triggering both the on-page code and the GTM-based code. This method ensures only one or the other is firing.

  • A

    Hi Dan Great post and I followed this and migrated to GTM. However I have noticed that the search engine optimisation has stopped working when I migrated to GTM. Are you able to explain what I might have missed?

    • Dan Wilkerson

      Hi A,

      Could you be more descriptive about your issue? Are you referring to the Search Engine Optimization reports in Google Analytics?

      Dan

  • Ninoslav

    Hi Dan, We are considering doing this migration, but we also have some events that fires on our current GA setup, that are setup to give us a more correct bounce rate. So the events are set as Interaction events. Would this change if we choose to migrate to GTM?

    BR

    Nino

    • Dan Wilkerson

      Hi Nino,

      GTM doesn’t change the way the data is collected, just how you deploy the collection method; just like with the normal GA code, you can specify that an event is not meant to impact or is meant to impact your bounce rate by using the Non-Interaction parameter. By default, your event will be Interaction, so if that’s what you want, you’ll just leave that to its default setting!

      Dan

      • Ninoslav

        Thank you for the advice Dan.

  • Massimo

    Hi Dan…Great post and I’m wondering if the setup is still the same or has anything changed? BTW, are there any plans to offer training courses in Toronto, Canada or recommend any?

    Thanks!

    • Dan Wilkerson

      Hi Massimo,

      Nope, this is still the way that we manage it!

      As of right now, I’m not sure that we have any plans, but I’ll let our Marketing Manager Jon know you asked 🙂

      Dan

      • Massimo

        Thanks Dan.

        As well, a new client has Classic GA on their site but they never realized they were upgraded to Universal GA. As I would like to set them up on GTM, at what point should I upgrade their GA code? Before going through the recommended steps for migrating to GTM?

        Thanks!

        Massi

        • Dan Wilkerson

          I would set up a Test Property, mimic their on-page Classic code with UA code via GTM, collect data and verify it’s working as expected, and then switch them to UA via GTM.

          Dan

  • Su

    Hi Dan,

    After discovering this MONTHS ago, I’m finally putting it into action. Thank you so much for doing this!

    Another cheeky request: I’m trying to also set up enhanced ecommerce analytics at the same time as doing all of this, but keep finding myself stumped. Do you know of a good guide that gives a rundown of this? Generally finding everything else out there either gives too much or too little information for my Magento setup managing it through GTM!

    Thanks,

    Suzannah

    PS please please come and do training in the UK too – I trust you more than most others I’ve seen thanks to your excellent content marketing!

    • Dan Wilkerson

      Hi Suzannah,

      Thanks for the kind words. Enhanced Ecommerce is tricky – it’s typically very custom. We don’t have a great guide here *yet*, but we have talked about putting one together in the future. In the meantime, the best resource I can offer is the Google EE Guides:

      https://developers.google.com/tag-manager/enhanced-ecommerce

      You may have already been through those, though. Good luck!

      Dan

  • Kantipath

    Just we lunched our new theme with previous GA code, it started to malfunction. GA data was drastically down. we were unable to understand, why it was?
    This “help article” really helped to understand what the wrong was going on with my site. There was 2 property code inserted in pages.
    Now going to take help of “GA tag manager” . But it is tough to do with simple knowledge of IT. Only a professional can fix this “manager”.
    Or, is there any simple way?

    Thanks for informative article

    • Dan Wilkerson

      Hi there!

      Unfortunately there’s no silver bullet for this kind of thing – you just have to pick it up as you go. We’ve got lots of articles that may help get you going, though, so poke around the archives!

      Dan

      • Kantipath

        Thanks for valuable suggestion .

  • Lisa

    Thanks for this post, great information!

    I have some click events coded for UA. Can I use both onClick=ga(send) and dataLayer.push at the same time, while I’m in that test period with both UA and GTM?

    Thanks!
    Lisa

    • Dan Wilkerson

      Hi Lisa,

      Ideally, yes! You’ll want to collect data as-usual in your production property, and replicate everything into your test property. The only problem you may run into will be removing those calls once you’ve finished replication.

      Dan

      • Lisa

        Thanks for your reply, Dan!

        After thinking about this more, I’m not sure I need to use the data layer. Currently, when authors load banner ads in our CMS, they define a label. That label is just sent onClick. Would I use a macro for that label? Using the destination URL will make things a little confusing, we would rather define a label related to the campaign.

  • Steve

    My WordPress site has the basic GA tracking snippet implemented through the Monster Insights ( Yoast ) plugin. I have the option ticked to tracked events within the plugin. I have woocommerce setup and running.

    I’d like to set up GTM to do the Event tracking at least to make life easier. Can I use GTM purely to set up Tags & Triggers for the event tracking ( by adding the GTM snippet too ) leaving the basic GA snippet code on page to handle the ecommerce transactions. Under the covers the data is being sent to the same property so all should be good even though some data is being sent via the GA code snippet whilst the event data is being sent through the GTM snippet. Am I right ?

  • Locke

    Thank you for al this amazing tutorial.

    When you mean, place the section of the snippet in your footer and the section of the snippet as close to the top of your as possible.

    is like :

    (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-0000');

    Refering that , you need to put the inside and the in the footer??

    thank you

    • Dan Wilkerson

      Hi Locke,

      You’ve got the right idea. The <noscript>’d iframe doesn’t do anything out of the box; it’s only useful if you’re a power user. I’ve only had occasion to make use of the feature 1 time. You could even ignore it altogether, in my opinion.

      Dan

  • http://www.sarah-hebert.com Sarah Hebert

    Hi… When I decid use TAG Manager, i have to put off my Old GA Code? Tx

Contact Us.

LunaMetrics

24 S. 18th Street, Suite 100,
Pittsburgh, PA 15203

Follow Us

1.877.220.LUNA

1.412.381.5500

getinfo@lunametrics.com

Questions?
We'll get back to you
in ONE business day.