简体   繁体   English

Google Analytics:跟踪链接点击需要延迟?

[英]Google Analytics: delay needed for tracking link clicks?

We're trying to track various click events on our pages to see how users are navigating our site.我们正在尝试跟踪页面上的各种点击事件,以了解用户如何浏览我们的网站。

It's possible to arrive at given page via different links (eg via a link in the top of the originating page vs one in the footer).可以通过不同的链接到达给定页面(例如,通过原始页面顶部的链接与页脚中的链接)。 For this reason it's not sufficient to merely track that the destination page loaded;出于这个原因,仅仅跟踪目标页面加载是不够的; we need to tag and track the click events.我们需要标记和跟踪点击事件。

The Google Analytics documentation recommends adding a 100ms delay for clicks on "outbound links", in order for the tracking code to complete before loading the link target. Google Analytics 文档建议为“出站链接”上的点击添加 100 毫秒的延迟,以便跟踪代码在加载链接目标之前完成。 Is this because the _gaq.push(['_trackEvent', category , action]) code is asynchronous, and needs time to complete before the page is unloaded?这是因为_gaq.push(['_trackEvent', category , action])代码是异步的,需要时间才能完成页面卸载吗?

If so, wouldn't this also be required for "on site" links?如果是这样,“现场”链接是否也需要这样做? I fail to see how this is different from a link to a new page on the same site;我看不出这与指向同一站点上的新页面的链接有何不同; in both cases the current page is unloaded.在这两种情况下,当前页面都被卸载。

Edit : I've since discovered Google's hitCallback mechanism for firing your page load events via callback.编辑:此后我发现了 Google 的hitCallback机制,用于通过回调触发页面加载事件。 This obviates the need to use a delay.这避免了使用延迟的需要。

Any tracking that is needing to be done just before a new page should include a slight ( < 200ms) delay.任何需要在新页面之前完成的跟踪都应该包含一个轻微的(< 200 毫秒)延迟。 Offsite, onsite, form submit, etc. This allows the request to the analytics servers to be completed.异地、现场、表单提交等。这允许完成对分析服务器的请求。

As far as internal link tracking, have you looked at the In-Page Analytics report & Enhanced Link Attribute plugin?至于内部链接跟踪,您是否查看了页面内分析报告和增强链接属性插件? It could help you out a bit without needing to do extra coding.它可以帮助您,而无需进行额外的编码。

Google Analytics provides a hitCallback hook for such cases.谷歌分析为这种情况提供了一个hitCallback钩子。

However, there are some cases where the event won't fire, so it's a good idea to also add a fallback redirect using a delay.但是,在某些情况下不会触发事件,因此最好还使用延迟添加回退重定向。

// Pretend that all of this preceding code
// is within a link's click handler.
var _this = this;
var eventHit;

ga('send', 'event', 'Outbound Links', 'click', _this.href, {
  hitCallback: function () {
    eventHit = true;
    document.location = _this.href;
  }
});

// Set delayed fallback to your liking. This example is 1 second.
setTimeout(function () {
  if (!eventHit) {
    document.location = _this.href;
  }
}, 1000);

Here is the Jquery to create the delay:这是创建延迟的 Jquery:

$("a").click(function (e) {        
    e.preventDefault(); //cancel the link click
    var url = $(this).attr('href');//get the destination url of the link        
    _gaq.push(['_trackEvent', 'Links', 'Clicked', 'Buy']); //do your tracking
    //go to the original destination url after a delay
    setTimeout(function () { window.location.href = url; }, 250); 
});

Yes, delay is needed to make sure GA request is finished before page is reloaded.是的,需要延迟以确保在重新加载页面之前完成 GA 请求。

Here is the Vanilla JS code to implement the tracking and delay:这是实现跟踪和延迟的 Vanilla JS 代码:

document.getElementById('ID').addEventListener('click', function(event) {
    event.preventDefault();
    _gaq.push([ '_trackEvent', 'category', 'action', 'label' ]);
    setTimeout( function() {
        document.location = event.target.href;
    }, 200);
});

Don't delay clicks.不要延迟点击。 Even the 250ms delay wont guarantee a successful tracking.即使是 250 毫秒的延迟也不能保证成功的跟踪。 If the target url is inside your domain, just store the tracking info on the local.storage and check on every page if there's something on the storage and trigger the ga event for the clicked button on page load instead.如果目标 url 在您的域内,只需将跟踪信息存储在 local.storage 上,并检查每个页面是否有存储空间,并在页面加载时为单击的按钮触发 ga 事件。 You should also validate if there's local.storage available on the client, and if not you can then use, in those cases, the click delay.您还应该验证客户端上是否有 local.storage 可用,如果没有,您可以在这些情况下使用单击延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM