0

GA Tag Manager - multiple containers

Is there any way that anyone was able to connect multiple containers from GA tag manager with segment? 

Any hacks? 

4replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hey Natwar, 

    Do you mean multiple GTM containers so that multiple container ID's will flow through one source in Segment? Unfortunately, no there are no hacks or workarounds.

    Reply Upvote
  • Yes, that's what I meant. Thanks Alex. 

    Reply Upvote
  • Hey Natwar, you can use analytics.ready to fire off native GTM code on the page after Segment loads the GTM container you put in your GTM integration settings inside of Segment. I'm not a GTM expert but I'm guessing it works similar to loading multiple instances of GA on a page, Segment has some docs about that here: https://segment.com/docs/integrations/google-analytics/#multiple-trackers

    Reply Upvote 1
  • Because this topic comes up often, I wanted to provide a bit more detail about how exactly to implement this, if needed.

    As Jake referenced in our documentation, customers will need to use analytics.ready to initialize the native GTM container (which will then initialize GA). We recommend doing this in the initialization script between analytics.load and analytics.page so that the page event is picked up by GTM.

     

    It would look like this:

    <script>
        //Segment Code
        !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t,e){var n=document.createElement("script");n.type="text/javascript";n.async=!0;n.src="https://cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(n,a);analytics._loadOptions=e};analytics.SNIPPET_VERSION="4.1.0";
        analytics.load("your_unique_write_key");
        analytics.ready(function(){(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='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-XXXXXX');
        });
        analytics.page();
        }}();
    </script>

    You should also ensure that the native GTM code is added separately within the <body> tag as Google indicates in their docs. You do not need analytics.ready to run the code in the body.

    That's it. This will allow Segment's Google Analytics destination to initialize in device mode on the browser and will also safely initialize GTM & GA onto the browser for multiple tracking.

    Reply Upvote