'How to dataLayer.push using GTM Custom Templates?

I have GTM Custom HTML tag that intercepts dataLayer.push called on the page, checks it for Ecommerce and if it is detected send Ecommerce data to remarketing pixels.

Now I'm trying to wrap this tag in a GTM Custom Template. To do this I need to make a dataLayer.push from my GTM Custom Template.

I studied the official documentation:
https://developers.google.com/tag-manager/templates/api#createqueue

There is an example of dataLayer.push method using GTM Sandbox JavaScript:

const dataLayerPush = createQueue('dataLayer');
dataLayerPush({'event': 'test_push'});

But if I run the code from the example, I get this result:

dataLayer
(6) [{…}, {…}, lj, {…}, {…}, {…}, push: ƒ]
0: {pageType: "main"}
1: {gtm.start: 1570440507302, event: "gtm.js", gtm.uniqueEventId: 2172}
2: lj
fi:
event: "test_push"
gtm.uniqueEventId: 7689
__proto__: Object
__proto__: Object
3: {event: "gtm.dom", gtm.uniqueEventId: 13084}
4: {event: "productImpression", ecommerce: {…}, gtm.uniqueEventId: 18479}
5: {event: "gtm.load", gtm.uniqueEventId: 24517}
push: ƒ ()
length: 6
__proto__: Array(0)

As you can see dataLayer[2] is not my push.

What is 'lj'?
What is 'fi'?

If I try to create another global array with a different name (not 'dataLayer') everything works correctly:

const newDataLayerPush = createQueue('newDataLayer');
newDataLayerPush ({'event': 'test_push'});

Output:

[{…}]
0: {event: "test_push"}
length: 1
__proto__: Array(0)

How can I send push to dataLayer using GTM Custom Templates?



Solution 1:[1]

For a dataLayer push you can use a custom HTML template and trigger it via page view then add the below code

<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'test_push'
 });
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jesse