I want to implement the Enhanced Ecommerce with Google Tag Manager and I want to push some data for the tag Universal Analytics.
I always created the dataLayer before the GTM script, but now I need to send other data with dataLayer.push
And it doesn't work, datalaLayer.push only works if I do it just before the GTM script starts.
Example. This 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 use dataLayer.push after the GTM script does not work, no data is sent and no errors are reported.
This isn't working for me:
<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>
//something 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>