Google Tag Manager tags explained


Alexander Starbucks

Can someone explain the Google Tag Manager (container) code and help me learn the following:

<script>
(
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],
var j = d.createElement(s),
var 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-xxxxx');
</script>

String:

  1. Why start with an opening bracket? ("(function...") What are the 5 arguments to wdsli?

    1. Set the "L" element of the "w" array to itself or to an empty array.
    2. Pushes an object into the last position of the w array, 2 properties and 2 values.
    3. Set var f to the first element of the s array.
    4. Set var j to element s (but what is "d."?).
    5. Asynchronous loading is true, all clear here.
    6. Javascript sources, all clear here.
    7. Absolutely no idea what this does, something to do with the DOM?
    8. Does this seem like a function call? The first letter in the function prototype seems to correspond to these parenthesized parameters?

Also, if the dataLayer doesn't already exist in the html page, how will this code generate the dataLayer before itself? I know it has to come before this container code, otherwise GTM can't use the data in the dataLayer.

Totally lost here, so any help is appreciated!


EDIT 2: I completely forgot one line, and the one I had the most trouble understanding:

var dl = l != 'dataLayer' ? '&l=' +l : '' ;

"If variable dl(dataLayer?) is set to value l(dataLayer?), but "dataLayer" does not exist, then (somewhat oddly) + l (add dataLayer) else empty/empty string

Thanks!

Sumner Evans

Before explaining, I want to point out that this is stripped down code, which is why it's hard to read. They do this to avoid unnecessary re-use of the same value, which takes up precious bytes.

  1. A sort of. Why Start With Open Parenthood? Because they encapsulate a function, which is called pass in hereafter (window, document,'script','dataLayer','GTM-xxxxx').

    b. What are the 5 parameters w d s l i? They will be passed in the code behind. They are window, document, script, dataLayer, and GMT-xxxxxrespectively.

  2. Set the lelement of the array wto itself or an empty array. If you w[l]have defined if as an array, don't override it. Otherwise, create it (you can't call array functions on an uninitialized array). This basically says (after passing the value):

    window['dataLayer'] = window['dataLayer'] || []
    
  3. Push the object into the last place warray, 2 properties and 2 times the value you have like this one correctly

  4. What does setting var fto the first element of san array mean:

    var f = document.getElementsByTagName('script')[0]
    

    This will get the first scriptlabel.

  5. Set var jto an element s(but what d?), which actually means:

    var j = document.createElement('script')
    
  6. load true asynchronously, all this is very simple here

  7. javascript source, here again it's all clear and simple

  8. Absolutely no idea what this does, something to do with the DOM? this means:

    1. Get fthe parent of the element (i.e. the encapsulating node), which in this case could be<head>
    2. Insert jelement (this is a new <script>front) felement
  9. This seems to be a function call? The first letter in the function prototype seems to correspond to these parenthesized arguments? Yes

Related


Google Tag Manager tags explained

Alexander Starbucks Can someone explain the Google Tag Manager (container) code and help me learn the following: <script> ( 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

Google Tag Manager - No tags in container

Bukovski So I tried to implement google tag manager as follows : https://developers.google.com/tag-manager/enhanced-ecommerce . But nothing happened to me. Nothing is posted and nothing is shown in the tag manager admin. Here is a simple code snippet taken fro

Google Tag Manager - No tags in container

Bukovski So I tried to implement google tag manager as follows : https://developers.google.com/tag-manager/enhanced-ecommerce . But nothing happened to me. Nothing is posted and nothing is shown in the tag manager admin. Here is a simple code snippet taken fro

Google Tag Manager - No tags in container

Bukovski So I tried to implement google tag manager as follows : https://developers.google.com/tag-manager/enhanced-ecommerce . But nothing happened to me. Nothing is posted and nothing is shown in the tag manager admin. Here is a simple code snippet taken fro

Google Tag Manager - No tags in container

Bukovski So I tried to implement google tag manager as follows : https://developers.google.com/tag-manager/enhanced-ecommerce . But nothing happened to me. Nothing is posted and nothing is shown in the tag manager admin. Here is a simple code snippet taken fro

Trigger tags on certain pages in Google Tag Manager

Mr. Sponge I'm trying to create a regular expression to trigger a tag in Google Tag Manager on certain pages. The problem I'm having is that I don't want this tag to be fired on URLs that match the query string in them, since it's just a session identifier, an

Custom HTML tags in Google Tag Manager with variables

Furman I have a custom html tag in my google tag manager <script>const d = 'sdfsf'; console.log(d);</script> Now I want to sdfsfbe a variable from my HTML to pass to GTM, where I have the GTM init fragment. Is this achievable anyway? Ike Pilstorff GTM support

How to make a tag run before other tags in google tag manager?

klang i created a tag in google tag manager and made it custom_html and bun a javascript function code inside, i used that function in another tag but after i posted it it said fbq_custom() was not defined but when i posted it When opening the gtm file I can s

Tag Manager will not show tags

Erwin I am trying to make a simple web page using the Google Maps window. In this window I have a site location marker (which can be changed by filling in new latitude and longitude and clicking the "Check site" button). When pressed, it will move the site mar

Tag Manager will not show tags

Erwin I am trying to make a simple web page using the Google Maps window. In this window I have a site location marker (which can be changed by filling in new latitude and longitude and clicking the "Check site" button). When pressed, it will move the site mar

Tag Manager will not show tags

Erwin I am trying to make a simple web page using the Google Maps window. In this window I have a site location marker (which can be changed by filling in new latitude and longitude and clicking the "Check site" button). When pressed, it will move the site mar

Tag Manager will not show tags

Erwin I am trying to make a simple web page using the Google Maps window. In this window I have a site location marker (which can be changed by filling in new latitude and longitude and clicking the "Check site" button). When pressed, it will move the site mar

Google Tag Manager - Copy tags from previous page source/media

Mel I ran into an issue where our 3rd party payment system changed our source/medium to referral. Is there a way to set a tab so that the payment page has the same origin/medium as the previous page? Ike Pilstorff I'll use Google 's official documentation on r

Google Tag Manager trigger for data layer not showing tags

Gazubi I was reading the new documentation on Google Tag Manager and based on that I created a custom event like this: Here event is the key and the value is Shop Now Start. I push this information in the "data" layer of the website. However, at this point, th

Can you trigger tags based on region in Google Tag Manager?

Danny Is it possible to trigger tags from Google Tag Manager using the location of a web visitor as a trigger? What I'm asking is to trigger the EU cookie notice, but only visitors from the EU can see it. Ideally I would be able to set it up in GTM. If possibl

Google Tag Manager trigger for data layer not showing tags

Gazubi I was reading the new documentation on Google Tag Manager and based on that I created a custom event like this: Here event is the key and the value is Shop Now Start. I push this information in the "data" layer of the website. However, at this point, th