[英]Add Google analytics event tracking to javascript function
是否可以將Google Analytics(分析)事件跟蹤添加到以下javascript函數?
$("#wScratchPad3").wScratchPad({
scratchDown: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';},
scratchMove: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';},
scratchUp: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';}
});
我使用以下分析代碼:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xx', 'xx');
ga('send', 'pageview');
</script>
是的,有可能。 您應該使用最新版本的Google Analytics(分析)代碼段。 根據Google的說法:
analytics.js代碼段是Universal Analytics的一部分,Universal Analytics目前處於公開測試階段。 新用戶應使用analytics.js。 現有的ga.js用戶應為analytics.js創建一個新的網絡媒體資源,並對其網站進行雙重標記。 在同一頁面上同時包含ga.js和analytics.js代碼段是絕對安全的。
在您的頁面上調用了分析代碼段后,您就可以在要跟蹤特定事件的任意位置的代碼中調用以下函數。 您可以為其指定自定義類別,操作和標簽名稱。
ga('send', 'event', 'category', 'action', 'label', value); //label and value are optional
您可以在這里閱讀所有相關信息: https : //developers.google.com/analytics/devguides/collection/analyticsjs/events
在您的示例中,您可以執行以下操作:
$("#wScratchPad3").wScratchPad({
scratchDown: function(e, percent) {
if(percent > 80) {
ga('send', 'event', 'ScratchPad', 'scratchDown');
window.location.href = 'http://www.url.com';
}
},
scratchMove: function(e, percent) {
if(percent > 80) {
ga('send', 'event', 'ScratchPad', 'scratchMove');
window.location.href = 'http://www.url.com';
}
},
scratchUp: function(e, percent) {
if(percent > 80) {
ga('send', 'event', 'ScratchPad', 'scratchUp');
window.location.href = 'http://www.url.com';
}
}
});
根據您使用的是哪種Google Analytics(分析)腳本,您也許可以擺脫這種情況。 我還對您的代碼進行了一些格式化,以使其更易於閱讀。
$("#wScratchPad3").wScratchPad({
scratchDown: function(e, percent) {
if(percent > 80) {
_gaq.push(['_trackPageview', 'http://www.url.com']);
window.location.href = 'http://www.url.com';
}
},
scratchMove: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';},
scratchUp: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';}
});
您可以發布正在使用的分析代碼嗎? 確保刪除UA代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.