Can I send a Google Analytics event and immediately navigate away, like so?
我是否可以发送一个谷歌分析事件并立即导航,就像这样?
_gaq.push(['_trackEvent', 'foobar']);
window.location = "/";
If Google Analytics does some kind of AJAX request when this is called then it should work whether we stay on the page or not. My concern is that it seems it may sometimes just be putting stuff in an array for later processing. I'm thinking this only happens initially, when Google Analytics hasn't had time to be initialized yet, but I'd like to be certain of this.
如果谷歌Analytics在调用时执行某种AJAX请求,那么无论我们是否停留在页面上,它都应该正常工作。我担心的是,它有时可能只是把东西放在数组中以便以后处理。我认为这只是在谷歌分析还没有时间初始化时才会发生,但我想确定这一点。
I did a test with GA debug, and it seemed to have worked, but I'm not sure if that means it always will depending on loading speed and what not.
我用GA debug做了一个测试,它似乎是有效的,但是我不确定这是否意味着它总是依赖于加载速度。
Can I do this and never lose any events?
我能做到这一点而不失去任何事件吗?
4 个解决方案
#1
7
The way I've done it is like so:
我是这样做的:
_gaq.push(['_trackEvent', '...', '...', '...']);
_gaq.push(function(){
// do stuff here
});
$('#logo').on('click', function(){
var curPage = window.location.href;
_gaq.push(['_trackEvent', curPage, '#logo']);
_gaq.push(function(){
window.location.href = '/';
});
});
The second push call will always run after the first one, because Google queues all push calls, so that the first one will run and complete, then the second one will run and complete. Google lets you put functions in the push method so you can queue them.
第二个push调用总是在第一个之后运行,因为谷歌队列中所有的push调用,所以第一个将运行并完成,然后第二个将运行并完成。谷歌允许将函数放入push方法中,以便对它们进行排队。
Source: https://developers.google.com/analytics/devguides/collection/gajs/#PushingFunctions
来源:https://developers.google.com/analytics/devguides/collection/gajs/ PushingFunctions
#2
5
I add a slight delay (via setTimeout
) if the new page isn't being opened in a new window.
如果新页面没有在新窗口中打开,我将添加一个轻微的延迟(通过setTimeout)。
I haven't had a chance to try this yet, but Google's new Universal Analytics has a hitCallback
function that is executed after the data has been sent.
我还没有机会尝试这个,但是谷歌的新Universal Analytics有一个hitCallback函数,它在数据发送后执行。
#3
1
@mike mentions hitCallback
method which is described in analytics.js documentation:
@mike提到了hitCallback方法,这是在分析中描述的。js文件:
In some cases, like when you track outbound links, you might want to know when the tracker is done sending data. That way you can send a user to their destination only after their click has been reported to Google Analytics. To solve this, the send command allows you to specify a hitCallback function in the field name object that will execute as soon as analytics.js has completed sending data.
在某些情况下,比如跟踪出站链接时,您可能想知道跟踪器何时完成发送数据。这样,只有当用户的点击被报告给谷歌Analytics后,才能将用户发送到他们的目的地。要解决这个问题,send命令允许您在字段名对象中指定一个hitCallback函数,该函数将在分析之后立即执行。js已经完成发送数据。
Which is fantastic, except the snippet is still in public beta. If for some reason (ahem technophobic policies ahem) you're limited to ga.js functionality, you can use this workaround:
这很好,只是代码片段还在公测中。如果因为某种原因(ahem技术恐惧政策),你被限制在ga。js功能,您可以使用以下解决方案:
_gaq.push(['_set', 'hitCallback', function(){
document.location='someOtherPage.html';
}]);
_gaq.push(['_trackEvent', 'category', 'event', 'value']);
Another sensible suggestion is to have a fallback to have the callback executed immediately if _gaq
is not defined.
另一种明智的建议是,如果没有定义_gaq,那么应该立即执行回调。
#4
0
It looks like your concern about losing the event is legitimate, see this question. One answer there seems a little fragile with regards to Google changing their code, but would let you confirm the event tracking before navigating to the link. That would probably be "near-immediate".
看起来你对失去事件的担心是合理的,看看这个问题。对于谷歌更改其代码,有一个答案似乎有点脆弱,但可以让您在导航到链接之前确认事件跟踪。这很可能是“立即的”。
#1
7
The way I've done it is like so:
我是这样做的:
_gaq.push(['_trackEvent', '...', '...', '...']);
_gaq.push(function(){
// do stuff here
});
$('#logo').on('click', function(){
var curPage = window.location.href;
_gaq.push(['_trackEvent', curPage, '#logo']);
_gaq.push(function(){
window.location.href = '/';
});
});
The second push call will always run after the first one, because Google queues all push calls, so that the first one will run and complete, then the second one will run and complete. Google lets you put functions in the push method so you can queue them.
第二个push调用总是在第一个之后运行,因为谷歌队列中所有的push调用,所以第一个将运行并完成,然后第二个将运行并完成。谷歌允许将函数放入push方法中,以便对它们进行排队。
Source: https://developers.google.com/analytics/devguides/collection/gajs/#PushingFunctions
来源:https://developers.google.com/analytics/devguides/collection/gajs/ PushingFunctions
#2
5
I add a slight delay (via setTimeout
) if the new page isn't being opened in a new window.
如果新页面没有在新窗口中打开,我将添加一个轻微的延迟(通过setTimeout)。
I haven't had a chance to try this yet, but Google's new Universal Analytics has a hitCallback
function that is executed after the data has been sent.
我还没有机会尝试这个,但是谷歌的新Universal Analytics有一个hitCallback函数,它在数据发送后执行。
#3
1
@mike mentions hitCallback
method which is described in analytics.js documentation:
@mike提到了hitCallback方法,这是在分析中描述的。js文件:
In some cases, like when you track outbound links, you might want to know when the tracker is done sending data. That way you can send a user to their destination only after their click has been reported to Google Analytics. To solve this, the send command allows you to specify a hitCallback function in the field name object that will execute as soon as analytics.js has completed sending data.
在某些情况下,比如跟踪出站链接时,您可能想知道跟踪器何时完成发送数据。这样,只有当用户的点击被报告给谷歌Analytics后,才能将用户发送到他们的目的地。要解决这个问题,send命令允许您在字段名对象中指定一个hitCallback函数,该函数将在分析之后立即执行。js已经完成发送数据。
Which is fantastic, except the snippet is still in public beta. If for some reason (ahem technophobic policies ahem) you're limited to ga.js functionality, you can use this workaround:
这很好,只是代码片段还在公测中。如果因为某种原因(ahem技术恐惧政策),你被限制在ga。js功能,您可以使用以下解决方案:
_gaq.push(['_set', 'hitCallback', function(){
document.location='someOtherPage.html';
}]);
_gaq.push(['_trackEvent', 'category', 'event', 'value']);
Another sensible suggestion is to have a fallback to have the callback executed immediately if _gaq
is not defined.
另一种明智的建议是,如果没有定义_gaq,那么应该立即执行回调。
#4
0
It looks like your concern about losing the event is legitimate, see this question. One answer there seems a little fragile with regards to Google changing their code, but would let you confirm the event tracking before navigating to the link. That would probably be "near-immediate".
看起来你对失去事件的担心是合理的,看看这个问题。对于谷歌更改其代码,有一个答案似乎有点脆弱,但可以让您在导航到链接之前确认事件跟踪。这很可能是“立即的”。