Firing 2 google analytics events with an outbound link

让人想犯罪 __ 提交于 2019-12-11 00:04:37

问题


I have outbound links like this in my html:

<a href="http://www.example.com" class="gaLink1" 
target="_blank" onCLick="ga_track_link('action', '123', 'abcde', 'fghij')"> 
<img src="http://www.example.com/image.jpg" alt="image name" height="180" style="max-width:153px;max-height:150px;" />
</a>

So, When there is a click on this image, the link www.example.com should open in a new tab, since there is target="_blank". Also, the onCLick event will call the function ga_track_link which is defined as:

function ga_track_link(action, id, name, source) {
    _gaq.push(['_trackEvent', 'category 1', action, id+': '+name]);
    _gaq.push(['_trackEvent', 'category 2', 'example', source, 15]);
}

This function is defined in the script section at the end of the html (inside the body section)

I'm observing in GA, there both events are tracked (category 1 and 2), but the amount of times that both are tracked is not equal. Category 2 appears almost half of times, which makes me think that the 2nd event is not always being fired.

I found this link http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55527 that suggests to put the function "ga_track_link" in the head section of the html and use a return False in the onClick function.

According to some other answers like When and why to 'return false' in JavaScript? , the return false statement would tell the event (onClick) not to be fired, which is not what I want, since I do want it to be fired but after my 2 GA events were fired.

So, I have 3 questions:

1) Is there any problem firing more than 1 GA event (with _trackEvent) on 1 click? What is the best way to do it?

2) Why Google Analytics link above states that the function should be placed in the head section of the html?

3) Can someone please clarify the "return false" statement's goal and how to use it correctly?


回答1:


1) Is there any problem firing more than 1 GA event (with _trackEvent) on 1 click? What is the best way to do it?

No, no issue with that although you could do both with one push. One Push, Multiple Commands

2) Why Google Analytics link above states that the function should be placed in the head section of the html?

Because the user may click the link before the javascript has time to load on the page.

3) Can someone please clarify the "return false" statement's goal and how to use it correctly?

My understanding is that it prevents the default behavior of the element and if listed after your function call it should have no effect on that function call. This is just as stated in one of the answers to the question you cited:

<a href="#" onclick="doSomeFunction(); return false;">

In the Google Analytics support link you provided, the return false; is stopping the link from immediately sending the user off the site. It runs the tracking function before hand and then redirects the user to the external site after a delay. This allows the tracking code the time needed to be sent off before the redirect.

function recordOutboundLink(link, category, action) {
    _gat._getTrackerByName()._trackEvent(category, action); //set tracking
    setTimeout('document.location = "' + link.href + '"', 100); // redirect to external site after delay
}



回答2:


Do you have target="_blank" on all links using your ga_track_link() function?

If the link is opening in the same window, it's possible that the tracking pixel requests made by _trackEvent might not have time to complete before the new page starts being fetched. If the link is opening in a new window, it's not a problem.



来源:https://stackoverflow.com/questions/9412578/firing-2-google-analytics-events-with-an-outbound-link

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