[英]Google Analytics tracking
我想從瀏覽器欄中刪除Google Analytics(分析)URL跟蹤代碼,以便當用戶復制/粘貼URL進行共享時,他們不會隨身攜帶所有跟蹤數據,這既無用又會歪曲數據。
因此,我使用了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>
有沒有人看到這種方法可能引起的並發症或問題?
您可能遇到的唯一問題是,在運行超時代碼時,Google Analytics(分析)可能尚未完全加載。
借助Google Analytics(分析)跟蹤器,有一個API,可以在將GA數據發送給Google之后使函數排隊。
您可以執行以下操作:
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.