4

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?

3
  • Would you need to do a datalayer.push in order to trigger the eventCallback, as per this resource: simoahava.com/gtm-tips/hitcallback-eventcallback?
    – nyuen
    Commented Nov 7, 2014 at 17:00
  • There is something really wrong, becouse i tryed copy pasting some examples and they still dont work. I tried dosument.location and alert so i just see a message, but nothing works... Do you have any idea what could i be doing wrong here?
    – Dreadlord
    Commented Nov 10, 2014 at 8:37
  • You don't have an event parameter Commented Jun 13, 2018 at 14:48

4 Answers 4

12

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
  • essential part should be here Commented Aug 16, 2016 at 14:06
  • 2
    Is there any official Google hosted documentation on these parameters? Commented Jun 28, 2023 at 14:45
4

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";
}
1
  • 3
    this is correct, because every push into dataLayer has to have defined 'event' otherwise it ll not be processed/fired
    – gondo
    Commented Aug 28, 2015 at 11:14
3

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>
2
-2

Dreadlord,

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

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

This should fix it. Hope this helps, Petr

4
  • document.location and document.location.href should work the same, should they not?
    – nyuen
    Commented Nov 8, 2014 at 18:31
  • i tryed with and without href but it is still not redirecting anywhere... Do you know what else could i try?
    – Dreadlord
    Commented Nov 10, 2014 at 7:45
  • 1
    It should be window.location.href, not document.location.href
    – rolznz
    Commented Apr 21, 2020 at 7:33
  • thank you @rolznz for the correction, updated my answer accordingly Commented Oct 11, 2020 at 9:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.