2
votes

I'm using Google Tag Manager and implementing Enhanced Ecommerce tracking via dataLayer. Everything is working fine. However, I'm now adding a CTA (call to action) that I want to track impressions for (there's multiple versions). This CTA is rendered as a partial, so at the time dataLayer is being constructed, it doesn't exist yet and I have no idea what will end up being there.

With straight Google Analytics, it looks like you can manually track an impression via:

ga('ec:addImpression', {
    // impression data
});

But, this doesn't work with GTM, as ga is not defined in that scenario. According to the GTM Enhanced Ecommerce documentation, the only other "option", is to manually track the impression through the GTM control panel based on page view. Again, that's not feasible as the impression data is not always the same.

After a little research, I found a third "option" in delaying pushing dataLayer. For example, instead of letting if fire on GTM load, you can tie it to a particular event and then send that event at a later point. I suppose that would let me, then, alter the dataLayer in this partial, as long as I made sure that the event was not sent until well after it's been rendered. However, that not only seems clunky and prone to error, but it would also require me to substantially alter the rest of my Enhanced Ecommerce tracking code.

Is there no way to just send the impression as you can with straight GA, with GTM?

1

1 Answers

2
votes

I am not quite sure that I understand the problem (but I have a go anyway). The way to do this is to push new data to the datalayer and have a custom event, i.e. the keyword "event" within the datalayer with a custom value; you then can use a "custom event" type trigger with the value you have pushed:

dataLayer.push({
  "event":"mycustomevent",
  "impressionData": myData
})

Specifications for EEC dataLayer are here. You then send a GA event that has EEC enabled and reads EEC data from the impressionData variable in the example.

You seem to have figured that out in principle, but seem to think this is somehow bad. No, it isn't, it is the recommended way by Google.

The dataLayer is an array of objects, and you can add new data to it by using the push method. This is not the native push method, but a custom implementation by Google. It scans the added data on every push, and if it finds the "event" key in the added object it adds the data from the on-page dataLayer variable to GTMs internal data model, where you then can use it in your tags.

Your problem might be, if I understand correctly, that you seem to think you have to construct all of the datalayer in a single go. But it is perfectly fine to split it up into multiple pieces and add new data via dataLayer.push as you need it.