Google Analytics 异步:事件跟踪回调?

发布于 2024-12-16 18:33:18 字数 529 浏览 0 评论 0原文

我希望使用事件跟踪来记录对另一个网站的特定类型链接的点击。我正在使用 jQuery,我当前拥有的代码是:

$('a.website').click(function(event) {
    var href = $(this).attr('href');
    try {
        _gaq.push(['_trackEvent', 'website', 'click', href]);
    } catch(err) {}
});

但是,在看到来自其他站点的引用信息后,我不相信这能够准确跟踪点击,可能是因为 _gaq.push 是异步的,并且在浏览器导航到该 url 之前尚未收到请求,并终止当前页面上运行的任何 javascript。

有什么方法可以检测 _gaq.push 函数已成功完成,以便我可以使用 event.preventDefault()document.location在事件记录后导航到链接?

I wish to use event tracking to record clicks on a specific type of link to another website. I am using jQuery, and the code I currently have is:

$('a.website').click(function(event) {
    var href = $(this).attr('href');
    try {
        _gaq.push(['_trackEvent', 'website', 'click', href]);
    } catch(err) {}
});

However, after seeing referrer information from the other site, I don't belive this is accurately tracking the clicks, probably because _gaq.push is asynchronous, and the request has not been received before the browser navigates to the url, and terminates any javascript running on the current page.

Is there any way to detect that the _gaq.push function has completed successfully, so I can use event.preventDefault() and document.location to navigate to the link after the event has been recorded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丶情人眼里出诗心の 2024-12-23 18:33:18

使用 Google Analytics hitCallback

您可以使用以下方法在跟踪器对象上设置自定义回调:

_gaq.push(['_set', 'hitCallback', function(){}]);

我在这里更深入地描述了它的代码:点击表单提交后在 Google Analytics 中跟踪事件

Use Google Analytics hitCallback

You can set a custom callback on the tracker object by using:

_gaq.push(['_set', 'hitCallback', function(){}]);

I described the code for it a little more in-depth here: Track event in Google Analytics upon clicking form submit

扛起拖把扫天下 2024-12-23 18:33:18

这个答案的解决方案怎么样?

  // Log a pageview to GA for a conversion
  _gaq.push(['_trackPageview', url]);
  // Push the redirect to make sure it happens AFTER we track the pageview
  _gaq.push(function() { document.location = url; });

How about solution from this answer?

  // Log a pageview to GA for a conversion
  _gaq.push(['_trackPageview', url]);
  // Push the redirect to make sure it happens AFTER we track the pageview
  _gaq.push(function() { document.location = url; });
故人如初 2024-12-23 18:33:18

我在外部链接跟踪方面也遇到过类似的问题。我的救赎是使用 OnMouseDown 事件而不是 OnClick

I've faced similar issue with the external link tracking. My salvation was using OnMouseDown event instead of OnClick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文