0

I have a GTM code where I'm able to push data but eventCallback is not working.

I have tries eventCallback and hitCallback but both are not working. I have also referred following post google analytics send event callback function, but still no luck.

Following is by GTM Code:

utils.gtm_push = function(event, category, action, label, params, callback) {
  var data = {
    "event": event,
    "category": category,
    "action": action,
    "label": label,
  };
  for (param in params) {
    data[param] = params[param]
  }
  if (callback) {
    data.eventCallback = callback;
  }
  if (window.dataLayer) {
    dataLayer.push(data);
  }
};

Note: I'm looking for a proper way and not setTimeout.

Can someone point me in the right direction?

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Are you looking for information on hitcallback (which belongs to Google Analytics) or eventCallback (which is native to GTM, so they are not the same) ? – Eike Pierstorff Feb 13 '16 at 08:54
  • I'm looking for `eventCallback`. I didn't know `hitCallback`, is not in GTM. Thanks for pointing it. – Rajesh Feb 13 '16 at 08:56

1 Answers1

0

No idea what's going wrong for you - I tested your code and it works fine. The callback will run after all tags that are triggered by the current GTM event (gtm.js by default) have fired. Below is my (working) test code, maybe you can see from there what's going wrong in your site:

function callback() {
    alert("This is a callback!");
}    
utils = {};

    utils.gtm_push = function(event, category, action, label, params, callback) {
      var data = {
        "event": event,
        "category": category,
        "action": action,
        "label": label,
      };
      for (param in params) {
        data[param] = params[param]
      }
      if (callback) {
        data.eventCallback = callback;
      }
      if (window.dataLayer) {
        dataLayer.push(data);
      }
    };

    utils.gtm_push("event","cat","action","label",["a","b","c"],callback);
Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
  • Is it possible that tagging is failing? I actually have about 10 pages and this is not working for only 1. I have checked all functions and they are passing proper values. I can also see data in `dataLayer`, and `eventCallback` is a function but still it does not fires. – Rajesh Feb 14 '16 at 03:51
  • Maybe you do the push after all GTM evens have already fired ? Or there might be an unrelated JS error on the page that interferes with GTM. Can you share an URL so we can have a look at it ? – Eike Pierstorff Feb 14 '16 at 10:11