{"id":527,"date":"2016-03-11T15:13:17","date_gmt":"2016-03-11T15:13:17","guid":{"rendered":"http:\/\/www.hannahourani.com\/writer\/?p=527"},"modified":"2016-03-12T12:04:36","modified_gmt":"2016-03-12T12:04:36","slug":"how-to-read-custom-site-data-from-the-gtm-datalayer","status":"publish","type":"post","link":"https:\/\/hannahourani.com\/writer\/2016\/03\/11\/how-to-read-custom-site-data-from-the-gtm-datalayer\/","title":{"rendered":"How to read custom site data from the GTM dataLayer"},"content":{"rendered":"<h2><\/h2>\n<p><strong>Google Tag Manager<\/strong>(GTM)\u00a0has some pretty good documentation about how to implement it with Google Analytics and other Google products. However, I wanted to pass\u00a0some custom site variables directly into GTM and then to feed that data into a custom HTML tag that&#8217;s sitting inside the GTM container.\u00a0I figured I&#8217;d outline the solution here in simple terms for all to see.<\/p>\n<h2>The Data Flow<\/h2>\n<figure id=\"attachment_529\" aria-describedby=\"caption-attachment-529\" style=\"width: 184px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/hannahourani.com\/writer\/2016\/03\/11\/how-to-read-custom-site-data-from-the-gtm-datalayer\/data-flow\/\" rel=\"attachment wp-att-529\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-529\" src=\"https:\/\/hannahourani.com\/writer\/wp-content\/uploads\/2016\/03\/Data-flow-184x300.jpg\" alt=\"Data flow\" width=\"184\" height=\"300\" srcset=\"https:\/\/hannahourani.com\/writer\/wp-content\/uploads\/2016\/03\/Data-flow-184x300.jpg 184w, https:\/\/hannahourani.com\/writer\/wp-content\/uploads\/2016\/03\/Data-flow.jpg 215w\" sizes=\"auto, (max-width: 184px) 100vw, 184px\" \/><\/a><figcaption id=\"caption-attachment-529\" class=\"wp-caption-text\">The data flow as I envisaged it.<\/figcaption><\/figure>\n<p>GTM offers a simple way to pass custom site data into its container. This is done through the dataLayer object.<\/p>\n<p>The dataLayer is an array that GTM asynchronously reads. Basically, it&#8217;s a table with your data as pairs of values.<\/p>\n<p>For example, you want to pass\u00a0your page category and the type of visitor that&#8217;s on the site\u00a0in to your dataLayer.<!--more--><\/p>\n<p>page category would\u00a0be named something like pageCategory and given a value and same with type of visitor.<\/p>\n<p>So what you want to do first is create the dataLayer, which will contain your variable, and place that dataLayer object above the GTM tag on the site.<\/p>\n<p>[code language=&#8221;javascript&#8221; title=&#8221;An example dataLayer which goes first on the page&#8221;]<br \/>\ndataLayer = [{<br \/>\n &#8216;pageCategory&#8217;: &#8216;blog&#8217;,<br \/>\n &#8216;visitorType&#8217;: &#8216;repeatCustomer&#8217;<br \/>\n }];<br \/>\n[\/code]<\/p>\n<p>[code language=&#8221;javascript&#8221; title=&#8221;The GTM tag, which goes second on the page&#8221;]<br \/>\n&lt;!&#8211; Google Tag Manager &#8211;&gt;<br \/>\n&lt;noscript&gt;&lt;iframe src=&quot;\/\/www.googletagmanager.com\/ns.html?id=GTM-WASDKH&quot;<br \/>\nheight=&quot;0&quot; width=&quot;0&quot; style=&quot;display:none;visibility:hidden&quot;&gt;&lt;\/iframe&gt;&lt;\/noscript&gt;<br \/>\n&lt;script&gt;(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({&#8216;gtm.start&#8217;:<br \/>\nnew Date().getTime(),event:&#8217;gtm.js&#8217;});var f=d.getElementsByTagName(s)[0],<br \/>\nj=d.createElement(s),dl=l!=&#8217;dataLayer&#8217;?&#8217;&amp;l=&#8217;+l:&#8221;;j.async=true;j.src=<br \/>\n&#8216;\/\/www.googletagmanager.com\/gtm.js?id=&#8217;+i+dl;f.parentNode.insertBefore(j,f);<br \/>\n})(window,document,&#8217;script&#8217;,&#8217;dataLayer&#8217;,&#8217;GTM-WTLZKH&#8217;);&lt;\/script&gt;<br \/>\n&lt;!&#8211; End Google Tag Manager &#8211;&gt;<br \/>\n[\/code]<\/p>\n<p>The dataLayer object goes first so that the dataLayer is there already when the gtm tag loads and calls it, as web pages load sequentially for the most part.<\/p>\n<p>If you do not specify a dataLayer object first, gtm will create one for you by default, but if you want to read\u00a0site variables through it in that case you&#8217;ll have to push them manually to gtm, using:<\/p>\n<pre class=\"prettyprint\"><span class=\"pln\">dataLayer<\/span><span class=\"pun\">.<\/span><span class=\"pln\">push<\/span><span class=\"pun\">(<\/span><span class=\"pun\">);<\/span><\/pre>\n<p>In some cases, pushing data to the dataLayer is the only way to go, for example when you want to send data after an event, such a click or a selection. However, this isn&#8217;t part of the scope of this post\u00a0today though so for simplicity&#8217;s sake we&#8217;ll use the easy approach, and if you have a\u00a0question about this bit drop it in the comments section.<\/p>\n<p>Your dataLayer code will look different from the one I&#8217;ve written above, obviously, depending on the data that you want to pass on to the GTM tag. The GTM tag will also be your GTM tag not the one that&#8217;s in the snippet above. Don&#8217;t go pasting my tag on your site now (it won&#8217;t work).<\/p>\n<h2>Setting up a dataLayer variable in GTM<\/h2>\n<p>So then, next step is to create variables in GTM which will carry the variables that you have in your dataLayer. Although it is technically possible to access your dataLayer variable directly in your custom code by just calling the JS array in there, you&#8217;ll want to set up a variable so that if that variable name changes on the site you wouldn&#8217;t have to go and amend a dozen custom codes.<\/p>\n<p>So go to variables-&gt; create new variable-&gt;\u00a0select dataLayer variable.<\/p>\n<figure id=\"attachment_535\" aria-describedby=\"caption-attachment-535\" style=\"width: 391px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/hannahourani.com\/writer\/?attachment_id=535\" rel=\"attachment wp-att-534\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-535 size-full\" src=\"https:\/\/hannahourani.com\/writer\/wp-content\/uploads\/2016\/03\/variable.jpg\" alt=\"\" width=\"391\" height=\"553\" srcset=\"https:\/\/hannahourani.com\/writer\/wp-content\/uploads\/2016\/03\/variable.jpg 391w, https:\/\/hannahourani.com\/writer\/wp-content\/uploads\/2016\/03\/variable-212x300.jpg 212w\" sizes=\"auto, (max-width: 391px) 100vw, 391px\" \/><\/a><figcaption id=\"caption-attachment-535\" class=\"wp-caption-text\">the data Layer variable name should match the name of the variable on your site. It is case-sensitive.<\/figcaption><\/figure>\n<p>After that, give your variable a name and you click\u00a0create variable.<\/p>\n<h2>The custom HTML<\/h2>\n<p>Next up you can go into your custom HTML tag and call this variable. To call a variable, you need to surround its name with a two brackets. It&#8217;s the variable name as you named it in its title in GTM, not the name as it is on the site, so in our example it is {{Category}} and NOT {{pageCategory}}<\/p>\n<p>[code language=&#8221;javascript&#8221; title=&#8221;You can use this snippet in a custom HTML tag to test that your data is passing through.&#8221;]<\/p>\n<p>&lt;script&gt;<\/p>\n<p>alert({{pageCategory}});<\/p>\n<p>&lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p>And that&#8217;s basically it, really. Quite simple. In our example here, the data in the dataLayer is static and loaded in the header. You can obviously populate that data with a server-side language like php, python, node, and the like and pass data into your tags dynamically.<\/p>\n<p>You can also push data to the dataLayer on events and the like but that bit is documented quite well on other blogs so I won&#8217;t rehash it here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google Tag Manager(GTM)\u00a0has some pretty good documentation about how to implement it with Google Analytics and other Google products. However, I wanted to pass\u00a0some custom site variables directly into GTM and then to feed that data into a custom HTML tag that&#8217;s sitting inside the GTM container.\u00a0I figured I&#8217;d outline the solution here in simple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"amp_status":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[40,6],"tags":[],"class_list":["post-527","post","type-post","status-publish","format-standard","hentry","category-data","category-media","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1Yr1F-8v","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/comments?post=527"}],"version-history":[{"count":15,"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/posts\/527\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hannahourani.com\/writer\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}