Google Analytics Cross-Domain Tracking with Multip

2019-08-02 21:25发布

问题:

I have a network of sites that are all on different domains and subdomains (domain1.com, sub.domain1.com, domain2.com, sub.domain2.com). Some of them already have Google Analytics trackers installed. Some do not have GA trackers at all.

I'd like to create a way to see a single GA account for the whole network, across domains, without disrupting existing single-domain GA tracking. Additionally, I need to be able to track visits and conversions across sites in the network as though they were a single site. Basically, you could think of it as a totally separate, independent GA account for the network.

I've seen that it's possible to auto-link domains (https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#autolink), thereby enabling network-wide analytics and conversion tracking, but is there a way to do this that won't disrupt the existing trackers already on the sites? Perhaps there's a way to make GA create two trackers with different cookies, one of which is cross-domain?

Ideally, I could give each site's developer a snippet of code to add to their site that would enable the network tracking. Is it possible?

回答1:

Yes, this is possible. A couple of things to keep in mind though:

I have a network of sites that are all on different domains and subdomains (domain1.com, sub.domain1.com, domain2.com, sub.domain2.com). Some of them already have Google Analytics trackers installed. Some do not have GA trackers at all.

This shouldn't be a problem. Its simple to add a second tracker to an existing page. I'll show you how below.

I'd like to create a way to see a single GA account for the whole network

The auto-linker plugin actually only works at the property level, so these will not only have to share the same account, but they'll have to share the same property. But this is probably what you want.

I need to be able to track visits and conversions across sites in the network as though they were a single site. Basically, you could think of it as a totally separate, independent GA account for the network.

Exactly, you'll create a brand new property just for this purpose, to track all four of these "sites" as a single "site".

Perhaps there's a way to make GA create two trackers with different cookies, one of which is cross-domain?

You can create multiple trackers running on the same site, but they must share cookies. This is because analytics.js only stores the client ID in the cookie, and obviously the client is the same.

For one of your existing sites that already has analytics installed, the code probably looks something like this:

(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-XXXX-Y', 'auto');
ga('send', 'pageview');

To add another tracker to that page that is tracking a different property, you can just add a few more lines of JavaScript to the snippet. In this example, you're giving your new tracker a the name t2 so it doesn't interfere with the existing tracker.

ga('create' 'UA-YYYY-Z', 'auto', {name: 't2'});
ga('t2.send', 'pageview');

This creates a new tracker and sends a pageview with the new tracker, but it doesn't set up autolinking. To do that you'll have to modify it slightly (assume this code is for domain1.com):

ga('create', 'UA-YYYY-Z', 'auto', {name: 't2', allowLinker': true});
ga('t2.require', 'linker');
ga('t2.linker:autoLink', ['domain2.com']);
ga('t2.send', 'pageview');

And then for domain2.com you'll just have to change the code slightly to autolink to domain1.com:

ga('create', 'UA-YYYY-Z', 'auto', {name: 't2', allowLinker': true});
ga('t2.require', 'linker');
ga('t2.linker:autoLink', ['domain1.com']);
ga('t2.send', 'pageview');

You don't need to list the subdomains because analytics.js tracks those by default.

In addition to the link you provided, here's some more helpful information about cross-domain tracking:
https://support.google.com/analytics/answer/1034342?hl=en