{"id":1664,"date":"2016-04-12T08:29:28","date_gmt":"2016-04-12T12:29:28","guid":{"rendered":"http:\/\/www.gregcruce.com\/?p=1664"},"modified":"2017-12-16T11:48:59","modified_gmt":"2017-12-16T16:48:59","slug":"where-to-place-google-tag-manager-code","status":"publish","type":"post","link":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/","title":{"rendered":"Where To Place Google Tag Manager Code"},"content":{"rendered":"

I’ve seen a bit of confusion around Google Tag Manager placement in some blogs and websites. Some blogs and websites say that that the Google Tag Manager code placement should be above the closing <\/body><\/code> tag, while others say the Google Tag Manager code placement needs to be just after the opening <body><\/code> tag, and yet others say Google Tag Manager code works the same as Google Analytics in the <head><\/head><\/code>.<\/p>\n

So, which of these GTM code placements is right?<\/p>\n

Best Google Tag Manger Placement<\/h2>\n

\n

Updated October 2016<\/h3>\n

<\/p><\/div>
\nGoogle updated their Google Tag Manager code mid-2016 and split up the duties, which means there are now two pieces of code to insert, and those two pieces of GTM code do in different locations. If you are worried about your GTM code being out of date – the old code still works, but the newer way is preferred for installations going forward.<\/p>\n

1st portion – as high in the <head> as possible<\/h4>\n

This fetches a JS file early to set up Google Tag Manager. Be sure to replace the “XXX-XXXXX” in the code below with your GTM ID.<\/p>\n

<!-- Google Tag Manager -->\r\n<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':\r\nnew Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\r\nj=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\r\n'https:\/\/www.googletagmanager.com\/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);\r\n})(window,document,'script','dataLayer','XXX-XXXXX');<\/script>\r\n<!-- End Google Tag Manager --><\/pre>\n

2nd portion – immediately after the opening <body> tag<\/h4>\n

This code will fire only if JS isn’t detected or disabled by the user. Again, be sure to replace the “XXX-XXXXX” with your GTM ID.<\/p>\n

<!-- Google Tag Manager (noscript) -->\r\n<noscript><iframe src=\"https:\/\/www.googletagmanager.com\/ns.html?id=XXX-XXXXX\"\r\nheight=\"0\" width=\"0\" style=\"display:none;visibility:hidden\"><\/iframe><\/noscript>\r\n<!-- End Google Tag Manager (noscript) --><\/pre>\n

Together, these two scripts ensure fast loading of Google Tag Manager.<\/p>\n

Does The Legacy\u00a0Google Tag Manager Code And Placement Still Work?<\/h2>\n

Yep, in fact, many sites still run the legacy Google Tag Manager code. Of course, it’s best to use the new GTM code, but if you have hundreds of sites to update, there’s no sense of urgency and this allows you to update the GTM code as time allows.<\/p>\n

Legacy Google Tag Manager Code And Best Placement<\/h2>\n

Legacy Google Tag Manager code placement works best when directly after the opening<\/strong> <body><\/code> tag<\/strong>. Google even says so in their setup documentation<\/a>.<\/p>\n

This can be confusing to someone familiar with traditional Google Analytics code<\/a>, which works best in the <head><\/head><\/code> portion of the page.<\/p>\n

Example Code<\/h4>\n
<\/head>\r\n<body>\r\n<!-- Google Tag Manager -->\r\n<noscript><iframe src=\"\/\/www.googletagmanager.com\/ns.html?id=GTM-XXXXXX\" height=\"0\" width=\"0\" style=\"display:none;visibility:hidden\"><\/iframe><\/noscript>\r\n<script>\/\/ <![CDATA[\r\n(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':\r\nnew Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\r\nj=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\r\n'\/\/www.googletagmanager.com\/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);\r\n})(window,document,'script','dataLayer','GTM-A1B2C3');\r\n\/\/ ]]><\/script>\r\n<!-- End Google Tag Manager -->\r\n<\/pre>\n

The Reasoning<\/h4>\n

So why the difference between Google Analytics code placement versus legacy Google Tag Manager and the new Tag Manager code? I think it’s partly because there are differences\u00a0between HTML and XHTML for <noscript><\/code><\/a>. HTML says it’s fine to have <noscript><\/code> in the head, why XHTML says you cannot. That could create a large support nightmare and easily be overlooked, so it’s likely why Google\u00a0opted to update the code beyond the legacy version.<\/p>\n","protected":false},"excerpt":{"rendered":"

I’ve seen a bit of confusion around Google Tag Manager placement in some blogs and websites. Some places say that it needs to be above the closing <\/body> tag, while others say it needs to be just after the opening tag, while yet others say it works the same as Google Analytics in the <\/head>. So who is right?<\/p>\n","protected":false},"author":1,"featured_media":1686,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88,1],"tags":[],"yoast_head":"\nWhere To Place Google Tag Manager Code - www.gregcruce.com<\/title>\n<meta name=\"description\" content=\"I've seen confusion around Google Tag Manager placement. Is it the closing body tag, the opening body tag, or the head tag. I've got the answer!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Where To Place Google Tag Manager Code - www.gregcruce.com\" \/>\n<meta property=\"og:description\" content=\"I've seen confusion around Google Tag Manager placement. Is it the closing body tag, the opening body tag, or the head tag. I've got the answer!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/\" \/>\n<meta property=\"og:site_name\" content=\"www.gregcruce.com\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-12T12:29:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-12-16T16:48:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.gregcruce.com\/wp-content\/uploads\/where-to-place-google-tag-manager-code.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1540\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Greg Cruce\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/gregcruce\/\" \/>\n<meta name=\"twitter:site\" content=\"@gregcruce\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Greg Cruce\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/\",\"url\":\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/\",\"name\":\"Where To Place Google Tag Manager Code - www.gregcruce.com\",\"isPartOf\":{\"@id\":\"https:\/\/www.gregcruce.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.gregcruce.com\/wp-content\/uploads\/where-to-place-google-tag-manager-code.jpg\",\"datePublished\":\"2016-04-12T12:29:28+00:00\",\"dateModified\":\"2017-12-16T16:48:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.gregcruce.com\/#\/schema\/person\/f106cd91d8004063aab0536d0bb8865c\"},\"description\":\"I've seen confusion around Google Tag Manager placement. Is it the closing body tag, the opening body tag, or the head tag. I've got the answer!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#primaryimage\",\"url\":\"https:\/\/www.gregcruce.com\/wp-content\/uploads\/where-to-place-google-tag-manager-code.jpg\",\"contentUrl\":\"https:\/\/www.gregcruce.com\/wp-content\/uploads\/where-to-place-google-tag-manager-code.jpg\",\"width\":1540,\"height\":600,\"caption\":\"Where To Place Google Tag Manager Code\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.gregcruce.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Where To Place Google Tag Manager Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.gregcruce.com\/#website\",\"url\":\"https:\/\/www.gregcruce.com\/\",\"name\":\"www.gregcruce.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.gregcruce.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.gregcruce.com\/#\/schema\/person\/f106cd91d8004063aab0536d0bb8865c\",\"name\":\"Greg Cruce\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gregcruce.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6be99f0190ebf09a94accef908c2f398?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6be99f0190ebf09a94accef908c2f398?s=96&d=mm&r=g\",\"caption\":\"Greg Cruce\"},\"description\":\"Greg has over 20 years of combined experience in the disciplines of web development, computer programming, IT, and Internet marketing and strategy. This unique skillset allows for powerful insights from consulting, strategy, execution and analysis.\",\"sameAs\":[\"https:\/\/www.gregcruce.com\",\"https:\/\/www.linkedin.com\/pub\/greg-cruce\/66\/240\/6b0\",\"https:\/\/x.com\/https:\/\/www.twitter.com\/gregcruce\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Where To Place Google Tag Manager Code - www.gregcruce.com","description":"I've seen confusion around Google Tag Manager placement. Is it the closing body tag, the opening body tag, or the head tag. I've got the answer!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/","og_locale":"en_US","og_type":"article","og_title":"Where To Place Google Tag Manager Code - www.gregcruce.com","og_description":"I've seen confusion around Google Tag Manager placement. Is it the closing body tag, the opening body tag, or the head tag. I've got the answer!","og_url":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/","og_site_name":"www.gregcruce.com","article_published_time":"2016-04-12T12:29:28+00:00","article_modified_time":"2017-12-16T16:48:59+00:00","og_image":[{"width":1540,"height":600,"url":"https:\/\/www.gregcruce.com\/wp-content\/uploads\/where-to-place-google-tag-manager-code.jpg","type":"image\/jpeg"}],"author":"Greg Cruce","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/gregcruce\/","twitter_site":"@gregcruce","twitter_misc":{"Written by":"Greg Cruce","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/","url":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/","name":"Where To Place Google Tag Manager Code - www.gregcruce.com","isPartOf":{"@id":"https:\/\/www.gregcruce.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#primaryimage"},"image":{"@id":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#primaryimage"},"thumbnailUrl":"https:\/\/www.gregcruce.com\/wp-content\/uploads\/where-to-place-google-tag-manager-code.jpg","datePublished":"2016-04-12T12:29:28+00:00","dateModified":"2017-12-16T16:48:59+00:00","author":{"@id":"https:\/\/www.gregcruce.com\/#\/schema\/person\/f106cd91d8004063aab0536d0bb8865c"},"description":"I've seen confusion around Google Tag Manager placement. Is it the closing body tag, the opening body tag, or the head tag. I've got the answer!","breadcrumb":{"@id":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#primaryimage","url":"https:\/\/www.gregcruce.com\/wp-content\/uploads\/where-to-place-google-tag-manager-code.jpg","contentUrl":"https:\/\/www.gregcruce.com\/wp-content\/uploads\/where-to-place-google-tag-manager-code.jpg","width":1540,"height":600,"caption":"Where To Place Google Tag Manager Code"},{"@type":"BreadcrumbList","@id":"https:\/\/www.gregcruce.com\/where-to-place-google-tag-manager-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.gregcruce.com\/"},{"@type":"ListItem","position":2,"name":"Where To Place Google Tag Manager Code"}]},{"@type":"WebSite","@id":"https:\/\/www.gregcruce.com\/#website","url":"https:\/\/www.gregcruce.com\/","name":"www.gregcruce.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.gregcruce.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.gregcruce.com\/#\/schema\/person\/f106cd91d8004063aab0536d0bb8865c","name":"Greg Cruce","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gregcruce.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6be99f0190ebf09a94accef908c2f398?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6be99f0190ebf09a94accef908c2f398?s=96&d=mm&r=g","caption":"Greg Cruce"},"description":"Greg has over 20 years of combined experience in the disciplines of web development, computer programming, IT, and Internet marketing and strategy. This unique skillset allows for powerful insights from consulting, strategy, execution and analysis.","sameAs":["https:\/\/www.gregcruce.com","https:\/\/www.linkedin.com\/pub\/greg-cruce\/66\/240\/6b0","https:\/\/x.com\/https:\/\/www.twitter.com\/gregcruce\/"]}]}},"_links":{"self":[{"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/posts\/1664"}],"collection":[{"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/comments?post=1664"}],"version-history":[{"count":0,"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/posts\/1664\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/media\/1686"}],"wp:attachment":[{"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/media?parent=1664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/categories?post=1664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gregcruce.com\/wp-json\/wp\/v2\/tags?post=1664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}