使用出站链接触发 2 个谷歌分析事件
我的 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>
因此,当单击此图像时,链接 www.example.com 应该在新选项卡中打开,因为有 target="_blank"。另外,onCLick事件将调用函数ga_track_link,其定义为:
function 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 的 head 部分,并在 onClick 函数中使用 return False。
根据其他一些答案,例如 何时以及为何在 JavaScript 中“返回 false” ? , return false 语句会告诉事件 (onClick) 不要被触发,这不是我想要的,因为我确实希望它被触发,但在我的 2 个 GA 事件被触发之后。
所以,我有 3 个问题:
1) 1 次点击触发超过 1 个 GA 事件(使用 _trackEvent)是否有问题?最好的方法是什么?
2) 为什么上面的 Google Analytics 链接指出该函数应放置在 html 的 head 部分?
3)有人可以澄清“return false”语句的目标以及如何正确使用它吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这没有问题,尽管您可以一键完成这两项操作。 一键执行多个命令
因为用户可能会在 javascript 有时间加载到页面上之前单击链接。
我的理解是,它会阻止元素的默认行为,如果在函数调用之后列出,它应该对该函数调用没有影响。这正如您引用的问题的答案之一所述:
在您提供的 Google Analytics 支持链接中,
return false;
阻止该链接立即将用户引导出该网站。它预先运行跟踪功能,然后在延迟后将用户重定向到外部站点。这使得跟踪代码有时间在重定向之前发送出去。No, no issue with that although you could do both with one push. One Push, Multiple Commands
Because the user may click the link before the javascript has time to load on the page.
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:
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.您是否在使用
ga_track_link()
函数的所有链接上都有target="_blank"
?如果链接在同一窗口中打开,则在开始提取新页面之前,_trackEvent 发出的跟踪像素请求可能没有时间完成。如果链接在新窗口中打开,则没有问题。
Do you have
target="_blank"
on all links using yourga_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.