0
votes

Where should I put dataLayer creation code in the page? Is it mandatory to put it above the GTM snippet?

Where should I call the dataLayer.push() function? Should it always be put in the code were the dataLayer creation code is placed like below?

<script>
dataLayer=[{‘Pcategory’:’login’,‘Pno’:1}],
dataLayer.push({‘Pcategory’:’login’,Pno:1});
</script>

Is it mandatory to declare an event for each dataLayer we push?

Is it mandatory to declare the dataLayer before we call dataLayer.push() function or does GTM provide a built in dataLayer?

Where should I put the code below? If I want declare it, how can I do it?

<a href="#" onclick="dataLayer.push({'event': 'GAEvent','pageURL': '/link-click/'});">Click Here...</a>
1

1 Answers

0
votes

You need to initialize the dataLayer above the GTM snippet. You need to put all information there that is supposed to be available on page load (the GTM snippet on page load can only access stuff that preceded the tag managenemt script). If you do not need custom data on page load the dataLayer might be empty. You still need to declare, else the push later on will fail.

You use a push when you want to add data after page load, e.g. if you want to respond to user interactions.

You do not need to add custom events to clicks or form submissions, since GTM has what's called auto-eventhandlers (i.e. the necessary javascript to respond to clicks is already part of the GTM script).

You need a custom event if you want to trigger tags on user interactions that are not clicks or form submits (or timers). E.g. if you run a javascript function and you need to trigger tags dependening on the result the function can push a custom event per possible result to the dataLayer, and you can fire tags accordingly.

For your example you might, for example, set up a click based trigger; if the link text is unqiue you could set the a pageUrl Variable based on the link text (which is automatically available via the {{Click Text}} variable if you use a click trigger). So for that example you would not have to use any custom code at all, you can simply use what GTM provides.