使用jQuery在href链接中附加onClick分析跟踪代码 [英] Attach onClick analytics tracking code in a href link using jQuery
问题描述
我对JavaScript和jQuery非常陌生,并且很难将onClick Google Analytics(分析)跟踪附加到我们使用jQuery重新设计的网站的A链接中.
我知道我可以做到这一点:
< a href =#" onClick ="_ gaq.push(['_ trackPageview','URL']);;> link</a>
但是,每当我将SharePoint HTML字段放在其中时,它都会删除onClick代码,因此我需要一个脚本来为我执行此操作.
到目前为止,我有这个:
< a class ="tracking" href ="URL">阅读PDF</a>jQuery(document).ready(function($){$(.tracking").click(function(){_gaq.push(['_ trackPageview','url']);});});
当我检查时,单击的PDF不会显示在实时分析页面中.因此,我需要确保单击链接时它会实时显示在Google Analytics(分析)跟踪器中的内容.我可以使用在A链接内添加onClick跟踪事件的脚本吗?
请帮助.
谢谢!
您可以使用以下代码生成页面视图:
$(document).ready(function(){$(.tracking").click(function(){ga('send','pageview','my-pdf-document')});});
您实际上可以传递甚至更多的数据.请在此处查看官方文档: https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#send
ga('send','pageview',{'页面':'/my-pdf.pdf','title':'我的PDF标题'});
更新
您绝对可以像这样动态使用您的href
ga('send','pageview',$(this).attr('href'));
甚至使用一些HTML-5,您都可以像这样建立链接:
< a href ="/mypdf.pdf" class ="tracking" data-title ="mypdf-title"> link</a>
然后做
ga('send','pageview',{'页面':$(this).attr("href"),'title':$(this).data("title")});
编辑2
如果您仍在使用ga.js,则可以按照以下方式进行操作:
_gaq.push(['_ trackPageview',$(this).attr('href')]);
I'm pretty new with JavaScript and jQuery and i'm having a hard time attaching onClick Google Analytics tracking inside an A link in our website redesign using jQuery.
I know that I can just do this:
<a href="#" onClick="_gaq.push(['_trackPageview', 'URL']);">link</a>
However SharePoint html field deletes the onClick code whenever I put it there so I need a script that does that for me.
So far I have this:
<a class="tracking" href="URL">Read PDF</a>
jQuery(document).ready(function($) {
$(".tracking").click(function() {
_gaq.push(['_trackPageview', 'url']);
});
});
The clicked PDF doesn't show up in the real time analytics page when I checked. So I need something that would ensure that when the link is clicked, it shows up in the Analytics tracker in real time. Can I use a script that adds the onClick tracking event inside the A link.
Please help.
Thanks!
You can generate a page view with the following code:
$(document).ready(function() {
$(".tracking").click(function() {
ga('send','pageview','my-pdf-document')
});
});
You can actually pass even more data. See the official doc here: https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#send
ga('send', 'pageview', {
'page': '/my-pdf.pdf',
'title': 'My PDF Title'
});
UPDATE
You could definitely use your href dynamically like this
ga('send','pageview', $(this).attr('href'));
or even use some HTML-5, you can have your link like this:
<a href="/mypdf.pdf" class="tracking" data-title="mypdf-title">link</a>
and do
ga('send', 'pageview', {
'page': $(this).attr("href"),
'title': $(this).data("title")
});
EDIT 2
If you're still using ga.js, you can do it this way then:
_gaq.push(['_trackPageview', $(this).attr('href')]);
这篇关于使用jQuery在href链接中附加onClick分析跟踪代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!