使用新的analytics.js语法进行跨域跟踪?

时间:2022-12-04 15:19:42

I am using Google Analytics and I am trying to set cross domain tracking for my website. I've read Google's cross domain tracking guide, but I am confused as to how to implement it properly.

我正在使用Google Analytics,我正在尝试为我的网站设置跨域跟踪。我已阅读Google的跨域跟踪指南,但我对如何正确实施它感到困惑。

The issue I am having is that the example code they give looks nothing like the tracking code I was given through my Google Analytics admin console.

我遇到的问题是,他们提供的示例代码与我通过Google Analytics管理控制台提供的跟踪代码完全不同。

My tracking code looks like this:

我的跟踪代码如下所示:

<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>

(My actual tracking ID and my domain have been censored out with MyTrackingID and MyDomain, respectively.)

(我的实际跟踪ID和我的域名已分别使用MyTrackingID和MyDomain进行审查。)

However, the example tracking code given in the guide looks like this:

但是,指南中给出的示例跟踪代码如下所示:

<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>

How do I add the _gaq.push(['_setDomainName', 'A.com']); "option" to my tracking code as instructed?

如何添加_gaq.push(['_ setDomainName','A.com']);根据指示对我的跟踪代码进行“选项”?

3 个解决方案

#1


3  

This code works for me. It is for Universal Analytics, not the older Google Analytics. Let's say you have two domains: source.com and destination.com and you want to track both domains:

这段代码适合我。它适用于Universal Analytics,而不是较旧的Google Analytics。假设您有两个域:source.com和destination.com,并且您想要跟踪这两个域:

On source.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>

On destination.com:

在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>

#2


1  

Script to add to the snippet provided by Google

要添加到Google提供的代码段的脚本

On your domain you need to add

在您的域上,您需要添加

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

on third party domain update the existing create function to

在第三方域上更新现有的创建功能

ga('create', 'UA-XXXXXX-X', {
  'allowLinker': true
});

Note by Google: While this feature is designed to work automatically for most websites, some pages may be scripted in ways that prevent Auto Linking from functioning properly.

Google的注意事项:虽然此功能旨在自动为大多数网站使用,但某些页面可能会以阻止自动链接正常运行的方式编写脚本。

#3


-2  

Correct implementation is following which is working perfect for myself -

正确实施以下是适合我自己的 -

<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>

For setting domain, you can add -

要设置域名,您可以添加 -

_gaq.push(['_setDomainName', 'A.com']);

below of

下面的

_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);

#1


3  

This code works for me. It is for Universal Analytics, not the older Google Analytics. Let's say you have two domains: source.com and destination.com and you want to track both domains:

这段代码适合我。它适用于Universal Analytics,而不是较旧的Google Analytics。假设您有两个域:source.com和destination.com,并且您想要跟踪这两个域:

On source.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>

On destination.com:

在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>

#2


1  

Script to add to the snippet provided by Google

要添加到Google提供的代码段的脚本

On your domain you need to add

在您的域上,您需要添加

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

on third party domain update the existing create function to

在第三方域上更新现有的创建功能

ga('create', 'UA-XXXXXX-X', {
  'allowLinker': true
});

Note by Google: While this feature is designed to work automatically for most websites, some pages may be scripted in ways that prevent Auto Linking from functioning properly.

Google的注意事项:虽然此功能旨在自动为大多数网站使用,但某些页面可能会以阻止自动链接正常运行的方式编写脚本。

#3


-2  

Correct implementation is following which is working perfect for myself -

正确实施以下是适合我自己的 -

<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>

For setting domain, you can add -

要设置域名,您可以添加 -

_gaq.push(['_setDomainName', 'A.com']);

below of

下面的

_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);