用出站链接触发2个Google Analytics(分析)事件 [英] Firing 2 google analytics events with an outbound link

查看:137
本文介绍了用出站链接触发2个Google Analytics(分析)事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的html中有这样的出站链接:

 < a href =http://www.example .comclass =gaLink1
target =_ blankonCLick =ga_track_link('action','123','abcde','fghij')>
< img src =http://www.example.com/image.jpgalt =图片名称height =180style =max-width:153px; max-height:150px; />
< / a>

所以,当点击这张图片时,链接www.example.com应该打开一个新的标签,因为有target =_ blank。另外,onCLick事件将调用函数ga_track_link,它定义为:

 函数ga_track_link(action,id,name,source) {
_gaq.push(['_ trackEvent','category 1',action,id +':'+ name]);
_gaq.push(['_ trackEvent','category 2','example',source,15]);





该函数在html末尾的脚本部分定义身体部分)



我在GA中观察,两个事件都被跟踪(类别1和2),但是两个跟踪的次数不相等。类别2出现几乎一半的时间,这使我认为第二个事件并不总是被解雇。



我发现这个链接 http://support.google.com/googleanalytics/bin/answer.py?hl=zh_CN& ; answer = 55527
,建议将函数ga_track_link放在html头部,并在onClick函数中使用返回False。


根据其他一些答案,如什么时候以及为什么要在JavaScript中返回false?,返回false语句会告诉事件(onClick)不被触发,这不是我想要的,因为我确实希望它被解雇后,但在我的2个GA事件被解雇后。

所以,我有3个问题:
$ b $ 1)一次点击发射超过1个GA事件(使用_trackEvent)时是否有任何问题? 2)为什么谷歌分析链接上面说明,该函数应该放在HTML的头部分?



3)有人可以澄清return false语句的目标以及如何正确使用它?

解决方案


1)1次点击发射超过1个GA事件(使用_trackEvent)是否有问题?什么是最好的办法呢?


不,没问题,尽管你可以用一个按钮来完成。 一键式多命令


2)为什么上面的Google Analytics(分析)链接指出该函数应该放在html头部?


因为用户可能在JavaScript有时间加载页面之前点击链接。


3)有人可以澄清return false语句的目标以及如何正确使用它?

我的理解是它可以防止元素的默认行为,如果在函数调用后列出,它应该对该函数调用没有影响。这正如您在引用问题的答案之一中所述:

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

在您提供的Google Analytics支持链接中,返回false; code>正在停止将该用户立即从站点发送出去的链接。它会事先运行跟踪功能,然后在延迟后将用户重定向到外部站点。这允许跟踪代码在重定向之前需要发送的时间。

 函数recordOutboundLink(link,category,action){ 
_gat._getTrackerByName()._ trackEvent(category,action); // set tracking
setTimeout('document.location =''+ link.href +'',100); //延迟后重定向到外部网站
}


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) 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个Google Analytics(分析)事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆