GTM data layer eventCallback function

旧城冷巷雨未停 提交于 2019-12-22 17:06:24

问题


So i have Ecommerce code setup in GTM data layer, but after that is sent to Analytics i want the page to be redirected so i know i should be using eventCallback but it is not working for some reason.

<body>
<script>
    dataLayer = [{
        'transactionId': '56833',
        'transactionAffiliation': 'TestName',
        'transactionTotal': 1540.00,
        'transactionTax': 385,
        'transactionShipping': 0,
        'transactionProducts': [{
            'sku': 'FFSS44HH',
            'name': 'test',
            'category': 'TestCategory',
            'price': 1540.00,
            'quantity': 2
        }],
        'eventCallback': function() {
            document.location.href='https://www.paypal.com/something';
        }
    }];
</script>
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(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=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXX');</script>
<!-- End Google Tag Manager -->

I have set up Ecommerce firing rule in GTM for this URL. I tried hitCallback and eventCallback but the redirect does not happen... What am i doing wrong here?


回答1:


Taken from: http://www.simoahava.com/gtm-tips/use-eventtimeout-eventcallback/

Always add the eventTimeout parameter when using eventCallback. The former takes a numerical value as its parameter, representing the number of milliseconds to wait before calling eventCallback anyway. In other words, even if your Tags stall and never signal completion, after two seconds eventCallback is invoked.

window.dataLayer.push({
  'event' : 'processLink',
  'eventCallback' : function() {
    window.location = targetUrl
  },
  'eventTimeout' : 2000
});



回答2:


i think i found a solution, i just added this ('event' : 'datalayerReady') at the end of the data layer:

'event' : 'datalayerReady',
'eventCallback': function() {
  document.location = "https://www.paypal.com/something";
}



回答3:


Dreadlord,

try using this: (no href after window.location)

'eventCallback': function() {
    document.location = 'https://www.paypal.com/something';
}

This should fix it. Hope this helps, Petr




回答4:


This works well for me.

<script type="text/javascript">
if (typeof dataLayer !== "undefined") {
        dataLayer.push({
            'event': 'MyEvent',
            'eventCallback' : function() {
                window.location = 'http://stackoverflow.com/questions/26802554/gtm-data-layer-eventcallback-function';
            }
        });
    }
</script>
<noscript>
    <meta http-equiv="refresh" content="0;url=http://stackoverflow.com/questions/26802554/gtm-data-layer-eventcallback-function" />
</noscript>


来源:https://stackoverflow.com/questions/26802554/gtm-data-layer-eventcallback-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!