Safely Migrating To Google Tag Manager



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 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.


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.

Though really a Palmyra, PA expat, Dan insists he's a "non-native" 'Burgher. Approaching social early, then marketing, exigencies of native targeting soon entangled him in numbers. Dan is happy to have ended up living in Pittsburgh and (accidentally) as a self-taught and avid programmer.

  • 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


    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?


    • 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.


  • 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 🙂
    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!


  • 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.


  • 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


  • 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.


  • 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?


    • 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!


  • 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?


  • 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?



    • 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!


      • 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?


    • 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 🙂


      • 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?



        • 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.


  • 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!



    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:

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


  • 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!


      • Kantipath

        Thanks for valuable suggestion .

      • Kantipath

        Thanks for advice, problem is fixed now with google help.

  • 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?


    • 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.


      • 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 ?

    • Dan Wilkerson

      You can, but you run the risk of misconfigured tags interacting poorly with your on page code. This is one of those things that works theoretically but often runs into issues in practice. Just make sure you’re testing thoroughly.


  • 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 :

    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],

    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.


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

    • Dan Wilkerson

      Yes – this guide should help you do it properly.


  • When you first create the Variable GA Tracking ID as a constant, are you entering the original (-1) or newly created (-2) property ID? It looks like it should be the original, but I’m not sure why. You write “You will now track pageviews in two Properties; one from your on-page code, and the other from your Google Tag Manager container.” I guess I’m not sure what that constant variable does or why it is needed. What I’m hoping is that this set up enables exclusively GTM data on the new property and has no effect on the old property data so I can compare well. Thanks for the post; it’s very helpful.

    • Dan Wilkerson

      It should be the new property, although they both end in “-1” in my examples so I could see how that might be confusing.


  • AuxaneD

    Hi Dan,
    Do you have any recommendation for relaunching a website already set-up on GTM to a new hosting platform and CMS?
    Migrating from GTM to GTM so to speak, without losing any analytics data gathering in the process if possible.
    Thanks a lot in advance for any insight.

    • Dan Wilkerson

      Yep, you’ll have to go through your current tags and ensure that you’ve replicated them on your new site. If there’s enough changes, you could consider making a second container.


  • Thanks Dan, the previous web developer setup Tag Manager with Analytics in it. Can we just remove the Google Tag Manager tracking code and add the regular GA tracking code? We want to make sure the previous developer doesn’t have control of it anymore. (I guess it’s the reverse of what you’ve outlined here)

    • Dan Wilkerson

      I wouldn’t recommend it – why give up the flexibility of GTM? Do you not have access to the GTM account? You could just remove their access in the Admin panel.


  • Jeremy Miller


    I have read a bunch of your blogs and they are great! I am having an issue with Tag Manager where only my Pageview tag is firing. I have tried everything, and I can’t get any other events to fire! I wrote my own tags and triggers, no luck… I read tutorials and videos and everything looks okay, still no luck. Finally, I uploaded one of your JSON files and blew away all my other tags and still only the pageview tag is firing. Le sigh.

    Any idea what is happening?

    Quick notes:

    1. I have a WP site but I have access to all the templates and the editor, so no issues on getting the snippet where it needs to be
    2. I have tried deploying the snippet as Google recommends (one part in header, the other in the body) as well as putting the entire snippet in the body (this makes Google Tag Assistant happy)
    3. Before GTM rolled out the most recent version, things seemed to be working okay

    Not sure this is a common problem, but I am totally at a loss on what to do.


  • Ron Ray

    Hey Dan-

    Sorry for the very basic question, but I can’t find the answer to this question anywhere. If we implement GA in GTM, should we completely remove the hard coded GA script from the site?


    • Dan Wilkerson

      Yep! See step 6.

      [EDIT] But not without migrating it using the same method as above.


  • Jenny Patterson

    Hi Dan,

    I need your help! I want to set up tracking on a brand new site. Do I setup GA first then migrate to GTM? or is there a way to setup in GTM add the GTM code on the site from the get go and still get GA data? if so how do I do this? and how do I access GA if I set up in GTM? What would you receommend??

    • Dan Wilkerson

      Hi Jenny,

      You’ll want to just install GTM and add GA into your GTM container; I’d suggest reading through this article to learn more about how they work together:


      • Jenny Patterson

        Thank you Dan will have a read through, If I have further questions Ill ask.

      • Jenny Patterson

        Hi Dan, I read the article my question was not answered unfortunately. I have a new site as stated above that has no tracking of GA or GTM on it. My question is around setting up tracking on the page. I know I can implement universal analytics and then migrate to GTM, however lets say I dont have universal analytics installed on the site is it possible to just implement GTM without universal ? I see in GTM they ask for the universal tracking ID when you want to link it up, is it possible to create universal analytics in GTM without having to implement it separately on the site? In summary I want to just know whether when implementing brand new tracking I need to implement both Google analytics & GTM on my site, or if I need to first implement Google analytics & then migrate to GTM?? Your detailed explanation on my problem would be very appreciated 🙂

        • Dan Wilkerson

          Hi Jenny,

          Short answer: you do not need to implement on-page Google Analytics, just install Google Tag Manager and add Google Analytics Tracking from within there, not in your HTML.

          We’ve got a great quick-start container that you can import into your Google Tag Manager setup that includes:
          – Google Analytics Pageview Tracking
          – Outbound Link Click Tracking
          – Contact Us Link Click Tracking
          – File Download Tracking

          You can find it here:

          I’d recommend starting there and experimenting as you go!


  • katiria ceballos

    Hi Dan,

    I’m trying to create a TAG Manager to see the referral data of our main domain, I followed some tutorial (subdomain tracking ) but for some reason I can’t get any data, can you please guide me !! .. thanks is advance

  • Steve

    If you you have a complex property/view configuration, what’s the easiest way to “copy” these settings over to a new test property? It appears Google only allows you to “move” a property, but not copy it.

Contact Us.


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

Follow Us



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