5

When somebody clicks the "buy" button on my website, I'm trying to track this event with Google Tag Manager using dataLayer and the dataLayer.push.

There's what it looks like:

dataLayer.push({
  'event': 'service',
  'category': 'purchase',
  'action': 'product selected',
  'label': product
});

The problem is, every time the user clicks the button, the service immediately redirects to the shopping cart, so in some cases the event doesn't have enough time to fire and the data in Google Analytics is incomplete.

I know there's an "eventCallback" option you can use with dataLayer, which looks like this

dataLayer.push({
  'event': 'service',
  'category': 'purchase',
  'action': 'product selected',
  'label': product,
  'eventCallback': function () {
    //redirect to the shopping cart
  }
});

});

But the problem with this option, it makes a dependency and there have been cases when the user wasn't redirected to the shopping cart because of some hickup of Google Tag Manger (it's a huge issue for us).

Is there a more smooth and efficient way to handle this problem, so that we could collect as much data as possible without breaking our service (say, when the tracking protection is enabled in the user's browser)

Eugene Krall
  • 503
  • 5
  • 20
  • You could use the `transport` field which, on supported browsers, basically sends the hit before the page unloads, https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport. – nyuen May 18 '16 at 13:34

1 Answers1

0

There's a property eventTimeout which you can use to ignore GTM and redirect the user anyway. However, it's also possible that the browser blocks GTM entirely, so you also need to check that the script has been loaded correct in your application, and if it didn't, just call that callback instead of pushing to the data layer

Thiago Camargo
  • 148
  • 1
  • 6