简体   繁体   中英

Does order of dataLayer matter in Google Tag Manager?

If I create a tag which is triggered on 'page view' in GTM, can I push data to this tag after the tag has been fired?

This is the event type I'm using (page view)

<script>
    window.dataLayer = window.dataLayer || []; //dataLayer object is instantiated
</script>

<script>
    var data = {page data, user data etc.}
    dataLayer.push(data);
</script>

<script>
    GoogleTagManager container code
</script>

// Will dataLayer.push(moreData) get caputured in the page view event?

It's my understanding that using dataLayer.push will update the dataLayer on events, however as the pageview event fires on the pageview, it means only information in the dataLayer before the GTM container code is captured in this event.

Is this correct, or can I use dataLayer.push to update the pageview information?

You can always use another trigger, such as gtm.dom or gtm.load to read in any more info that you've pushed in to the dataLayer after the Pageview event has fired. But I always recommed that you push in an event along with any additional data and use that event to trigger your tag because you may not necessarily want to wait until the DOM has loaded or until the page has entirely loaded.

In the example you have provided, Google Tag Manager would likely capture the extra information on DOM ready.

DataLayer information persists until the page is closed (either new page, or closed page). This can make it valuable for tracking information (eg a click value), if you want to collect it at a later time (eg a form submission). By firing a dataLayer push on the click (eg did a user click on a store?), that information will be stored within the dataLayer and be ready to be re-called at any time (for that page) like the contact form submission. This means that you can have an events such as

Event Category: Contact
Event Action: Form Submitted
Event Label: Store info interaction | No store info interaction

In addition, it is always best practice to add an event key into each dataLayer push, for example:

dataLayer.push({
  'event': 'addToCart',
  'ecommerce': {...
   }
});

It is also worth noting that Enhanced Ecommerce by default reads in the dataLayer in dataLayer version1. This means that only the most recent ecommerce object will be read in.

In practice, this means that if you have products and promotions on the same page, and they are sent in different pushes, Google Tag Manager will only collect the most recent push on the initial Pageview. This is why it is always important to have an event key, as it provides you with some flexibility for firing your tags.

The "data" in the tag will remain the same, you can't retroactively change the "data." When you use GTM preview and debug you see "Page View, "Event," "DOM Ready," etc. When you click on these you can see what the datalayer is for each. You can set your tag to fire at a different time to ensure the information you want is ready to be called. So set up one tag to do the datalayer.push and then another tag to fire with the trigger event equals addToCart rather than pageview. That way the datalayer has all the information you want as it fires after the datalayer.push.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM