2012-08-13 263 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。

相关问题