Google Analytics Async: Events Tracking Callback?

我怕爱的太早我们不能终老 提交于 2020-01-10 01:03:09

问题


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?


回答1:


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; });



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/8147065/google-analytics-async-events-tracking-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!