Listening google events

风流意气都作罢 提交于 2019-12-11 13:49:13

问题


Could you tell me, how can I listening google analytics (google tag manager) event with native javascript? For example:

The client sent this event

ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue]);

I need to subscribe to this event.


回答1:


You can use tasks, which allow you to hook into the Google Analytics tracker's execution routine, for instance, hook into the sendHitTask to retrieve the payload of every hit sent:

ga('create', 'UA-XXXXX-Y', 'auto');

ga(function(tracker) {

  // Grab a reference to the default sendHitTask function.
  var originalSendHitTask = tracker.get('sendHitTask');

  // Modifies sendHitTask to send a copy of the request to a local server after
  // sending the normal request to www.google-analytics.com/collect.
  tracker.set('sendHitTask', function(model) {
    originalSendHitTask(model);
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/localhits', true);
    xhr.send(model.get('hitPayload'));
  });
});

ga('send', 'pageview');



回答2:


You can use a hit callback as per documentation - a JavasScript function that will be executed after the hit has fired.



来源:https://stackoverflow.com/questions/48806235/listening-google-events

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