我正在使用Google Analytics,我正在尝试为我的网站设置跨域跟踪。我已经阅读了Google的cross domain tracking guide,但我对如何正确实施它感到困惑。
我遇到的问题是,他们提供的示例代码与我通过Google Analytics管理控制台提供的跟踪代码完全不同。
我的跟踪代码如下所示:
<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', 'MyTrackingID', 'MyDomain');
ga('send', 'pageview');
</script>
(我的实际跟踪ID和我的域名已分别使用MyTrackingID
和MyDomain
进行审查。)
但是,指南中给出的示例跟踪代码如下所示:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_setAllowLinker', true]);
_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>
如何按照说明将_gaq.push(['_setDomainName', 'A.com']);
“选项”添加到我的跟踪代码中?
答案 0 :(得分:3)
此代码适用于我。它适用于Universal Analytics,而不是较旧的Google Analytics。假设您有两个域:source.com和destination.com,并且您想要跟踪这两个域:
在source.com上:
<!-- Universal Analytics -->
<script type="text/javascript">
(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-XXXXXXXXX-X', 'source.com', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['destination.com']);
ga('send', 'pageview');
</script>
在destination.com上:
<!-- Universal Analytics -->
<script type="text/javascript">
(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-XXXXXXXX-X', 'source.com',{'allowLinker': true});
ga('send', 'pageview');
</script>
答案 1 :(得分:1)
要添加到代码段provided by Google
的脚本在您的域名上,您需要添加
ga('require', 'linker'); // Load the plugin.
// Define which domains to autoLink.
ga('linker:autoLink', ['3-party.com', '3-party-domain.com']); //add as many as you need third party sites
在第三方域上将现有的create
功能更新为
ga('create', 'UA-XXXXXX-X', {
'allowLinker': true
});
Google注意:虽然此功能旨在自动适用于大多数网站,但某些网页可能会以防止自动链接无法正常运行的方式编写脚本。
答案 2 :(得分:-2)
正确实施以下是适合我自己的 -
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_setAllowLinker', true]);
_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>
要设置域名,您可以添加 -
_gaq.push(['_setDomainName', 'A.com']);
以下
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);