有什么方法可以检测 ga 发送何时完成(承诺、回调、事件等)?
Is there any way to detect when ga send is finished (promise, callback, event, etc.)?
我需要使用 ga() 发送一个事件,然后立即重定向到另一个页面。如果 ga() 发送未完成,它将被重定向取消。是否有任何机制(例如回调)可用于检测事件何时已传送到 GA(或出错)?我目前只是延迟了一小段时间,但这是不确定的。
有(甚至叫回调):Hit Callback。跟踪呼叫后重定向确实是主要用例。例如,示例中的文档涉及拦截表单提交,并在发送表单后重新提交:
// Gets a reference to the form element, assuming
// it contains the id attribute "signup-form".
var form = document.getElementById('signup-form');
// Adds a listener for the "submit" event.
form.addEventListener('submit', function(event) {
// Prevents the browser from submitting the form
// and thus unloading the current page.
event.preventDefault();
// Sends the event to Google Analytics and
// resubmits the form once the hit is done.
ga('send', 'event', 'Signup Form', 'submit', {
hitCallback: function() {
form.submit();
}
});
});
我需要使用 ga() 发送一个事件,然后立即重定向到另一个页面。如果 ga() 发送未完成,它将被重定向取消。是否有任何机制(例如回调)可用于检测事件何时已传送到 GA(或出错)?我目前只是延迟了一小段时间,但这是不确定的。
有(甚至叫回调):Hit Callback。跟踪呼叫后重定向确实是主要用例。例如,示例中的文档涉及拦截表单提交,并在发送表单后重新提交:
// Gets a reference to the form element, assuming
// it contains the id attribute "signup-form".
var form = document.getElementById('signup-form');
// Adds a listener for the "submit" event.
form.addEventListener('submit', function(event) {
// Prevents the browser from submitting the form
// and thus unloading the current page.
event.preventDefault();
// Sends the event to Google Analytics and
// resubmits the form once the hit is done.
ga('send', 'event', 'Signup Form', 'submit', {
hitCallback: function() {
form.submit();
}
});
});