问题
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