[英]Add Google analytics event tracking to javascript function
[英]Add Google Analytics Click (event) tracking code to Javascript window.open
我腦子里有以下Javascript:
// Google Analytics code required for EventTracking <script type="text/javascript"> function trackOutboundLink(link, category, action) { try { _gaq.push(['_trackEvent', category , action]); } catch(err){} setTimeout(function() {document.location.href = link.href; }, 100); }
以下是我的身體:
// required to activate social bookmarks
$(".btn_fb").click(function() {
window.open("");
return false;
});
$(".btn_tw").click(function() {
window.open("http://twitter.com/home?status=Webpage Title"+document.URL+"", "Twitter", "width=660,height=400,scrollbars=no;resizable=no");
return false;
});
$(".btn_li").click(function() {
window.open("http://www.linkedin.com/shareArticle?mini=true&url="+document.URL+"&title=Webpage Title;summary=Webpage summary", "LinkedIn", "width=660,height=400,scrollbars=no;resizable=no");
return false;
}); $('.btn_go').click(function() {
window.open("http://plus.google.com/share?url="+document.URL, 'GooglePlus', 'width=660,height=500,scrollbars=no,resizable=no');
return false;
});
$(".btn_ma").attr("href", "mailto:?subject=Webpage Subject&body=Webpage summary" + window.location); </script>
//GA Code
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-NNNNNN-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
以下是我的HTML:
<div class="btn_fb"></div>
<div class="btn_tw"></div>
<div class="btn_li"></div>
<a class="btn_ma"></a>
<div class="btn_go"></div>
鏈接功能正常,但因為它們不是標簽(.btn_ma除外)我無法根據文檔添加Google Analytics事件跟蹤: https : //support.google.com/analytics/answer/1136920?hl = zh-CN。
有關最佳方法的建議嗎?
您可以將事件跟蹤添加到jQuery單擊事件。
// required to activate social bookmarks
$(".btn_fb").click(function() {
window.open("");
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});
$(".btn_tw").click(function() {
window.open("http://twitter.com/home?status=Webpage Title"+document.URL+"", "Twitter", "width=660,height=400,scrollbars=no;resizable=no");
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});
$(".btn_li").click(function() {
window.open("http://www.linkedin.com/shareArticle?mini=true&url="+document.URL+"&title=Webpage Title;summary=Webpage summary", "LinkedIn", "width=660,height=400,scrollbars=no;resizable=no");
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});
$('.btn_go').click(function() {
window.open("http://plus.google.com/share?url="+document.URL, 'GooglePlus', 'width=660,height=500,scrollbars=no,resizable=no');
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});
$(".btn_ma").click(function() {
window.location = "mailto:?subject=Webpage Subject&body=Webpage summary" + window.location
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});
您會注意到我已將點擊事件跟蹤添加到每個點擊事件,因此當用戶點擊div時,將采取操作,然后跟蹤分析。 我還編輯了您的郵件點擊事件,以便跟蹤它。 另一個想法是使用像Share This或AddThis這樣內置分析的服務,也可以鏈接到Google Analytics
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.