I am looking to incorporate Enhanced Ecommerce using Google Tag Manager and I need to send additional data for the Universal Analytics tag.
Usually, I have created the dataLayer before the GTM script, but now I need to push more data with dataLayer.push
However, I'm facing an issue where dataLayer.push
only works if it is placed right before the GTM script.
For example, this setup works:
<script>
<head>
dataLayer = [{
'google_tag_params': {
'ecomm_pagetype': 'category',
'ecomm_category': '{{ $resource->seo->h1 }}',
}
}];
dataLayer.push({
'ecommerce': {
'currencyCode': 'EUR',
'impressions': [
{
'id': '12312',
'price': 24,
'category': 'wfwefwerwerwer',
'position': 2,
'name': 'wfwefwerwerwer',
'brand': 'My Brand',
'list': 'Product List',
}
]
}
});
(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],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');
</script>
</head>
But if I place dataLayer.push after the GTM script, it doesn't work - no data is sent and no errors are reported.
This configuration is not effective:
<head>
<script>
dataLayer = [{
'google_tag_params': {
'ecomm_pagetype': 'category',
'ecomm_category': '{{ $resource->seo->h1 }}',
}
}];
(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],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');
</script>
</head>
<body>
<!-- some content HTML here -->
<footer></footer>
<script>
dataLayer.push({
'ecommerce': {
'currencyCode': 'EUR',
'impressions': [
{
'id': '12312',
'price': 24,
'category': 'wfwefwerwerwer',
'position': 2,
'name': 'wfwefwerwerwer',
'brand': 'My Brand',
'list': 'Product List',
}
]
}
});
</script>
</body>