2012-08-13 265 views
0

我想從瀏覽器欄中刪除Google Analytics(分析)網址跟蹤代碼,以便當用戶複製/粘貼URL進行分享時,他們不會帶來所有跟蹤數據沒用,並且能夠歪曲道路上的數據。Google Analytics(分析)跟蹤

因此,我使用history.js來運行replaceState,以便在短暫暫停後基本消除URL中的跟蹤數據。

<script type="text/javascript"> 
setTimeout(function() { 
    if(window.location.search.indexOf("utm_campaign") >= 1) { 
     window.history.replaceState(null, document.title, window.location.pathname); 
    } 
}, 1000); 
</script> 

有沒有人看到任何可能的併發症或使用這種方法的問題?

+0

爲什麼你需要暫停? – 2012-08-13 18:34:16

+0

我可以看到一個巨大的問題。它僅適用於HTML5瀏覽器:https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history#The_replaceState().C2.A0方法 – Tchoupi 2012-08-13 18:36:41

+0

@MikeRobinson:我添加了暫停,以防GA遺傳跟蹤器當我運行它時,仍然從URL中提取信息。 – stevecomrie 2012-08-13 18:44:57

回答

1

您可能遇到的唯一問題是Google Analytics(分析)可能沒有在超時代碼運行時完全加載。

使用Google Analytics(分析)跟蹤工具,可以在GA數據發送給Google之後讓API函數排隊等待。

你可以做這樣的事情:

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'UA-XXXXX-X']); 
_gaq.push(['_trackPageview']); 
_gaq.push(function() { 
    var newPath = location.pathname + location.search.replace(/[?&]utm_[^?&]+/g, "").replace(/^&/, "?") + location.hash; 
    if (history.replaceState) history.replaceState(null, '', newPath); 
}); 

(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); 
})(); 

通知書線4條,其中一個功能是被推到了_gaq對象。

該功能將在GA請求發送後直接替換URL。

相關問題