📜 ⬆️ ⬇️

Cross-domain tracking with Google Analytics

What is cross-domain tracking and why is it needed?


image The essence of cross-domain tracking using Google Analytics web analytics is that when you switch from one site to another via a link or through a form, information from the previous site’s cookies is transmitted, with the result that the user's session would not terminate, but allow you to track the user's movement across multiple monitored sites.

In fact, it turns out that the visitor does not leave the site through an external link, but simply goes to another page, although in reality he is already on a different domain. Thus, we can trace the user's path from the beginning to the end of his visits to a group of sites.

Another important result of this implementation for commercial sites is the ability to track the initial source that led, for example, to the order of goods on the site.
')
In Google Analytics, there is a section “Return path to the goal”, with which we can trace the visitor’s path before making an order.

Here's how to use it in practice.

For example, the company has 2 sites: first.ru and second.ru. If the user first goes to the first.ru website, and then follows the link from him to the second.ru website and places an order for it, then the return path to the target without the use of cross-domain tracking will look like this:

image

When using cross-domain tracking in reports, we can find the initial source of this visit:

image

In addition, we can find out the keyword, by which the visitor came first to the site first.ru, subsequently ordering the goods on second.ru:

image

All this expands the possibilities of data collection by Google Analytics, which allows for a more in-depth analysis of sites.

Implementing cross-domain tracking


To implement this tracking, follow these steps:
  1. Make some changes to the Google Analytics tracking code for each domain
  2. Adding onClick and onSubmit events to outgoing links and / or forms
  3. Add filter to Google Analytics web analytics site profile


1. Changes to the Google Analytics tracking code


In order to track the overall statistics for all domains in one profile, you should create a new profile and for all domains in the Google Analytics code register the same ID, i.e. "UA-XXXXX-X". With this option of collecting information, statistics for all sites will be collected and stored in one profile.

As a result, we will be able to collect statistics and at the same time observe which page of another site we are tracking followed the user, which way went to this page of the site, etc.

Next, you need to add the following code lines to the Google Analytics standard tracking code for each site:

pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);


For example, the real code will look like this:




By adding these lines of code, we are activating the methods of the Google Analytics system to transfer the user's cookies (save cookies from visiting the previous site, instead of creating new ones) from one domain to another.

In some cases, in addition to the domains themselves, we need to take into account their subdomains.

Suppose we have sites site1.ru and site2.ru and their subdomains poddomen.site1.ru and poddomen.site2.ru.

In this case, the Google Analytics code on these two sites and subdomains will look like this:

For site1.ru and its subdomains:
pageTracker._setDomainName(".site1.ru");
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);


For site2.ru and its subdomains:
pageTracker._setDomainName(".site2.ru");
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);


2. Adding onClick and onSubmit events to outgoing links and forms


The next step is to add the _link method to all outgoing links leading to the required domains.

Example:

need to replace the link
  <a href="http://www.vneshniy-say.ru/"> Link to an external site </a> 


on the link
  <a href="http://www.vneshnii-say.ru/" onclick="pageTracker._link(this.href); return false;"> Link to external site </a> 


Thus, the same onClick event is added to all external links.
onclick="pageTracker._link(this.href); return false;"

Now, when we switch to the second site we are tracking, the user’s cookies will not be recreated, but transferred from the previous domain, and the visitor will not be considered new to Google Analytics for this site: it will have its own history (where it came from, what keywords looked at the previous site and so on). The result is that the visitor just moved to another section of the site.

If the information is transferred between domains using forms, the _linkByPost method should be used .

Example of use:
  <form action = "XXX" onSubmit = "pageTracker._linkByPost (this)"> 


3. Adding a filter to the site profile


By default, all data on sites is collected in one profile, respectively, in Google Analytics reports only site pages will be displayed (without domains). For example pages of two domains site1.ru/news and site2.ru/news will be displayed as / news. In order to add a domain to the pages (instead of / news there will be site1.ru/news and site2.ru/news), you need to create the following filter in your profile:

Filter Type: Custom Filter> Advanced
Field A: Host Name
Extract A: (. *)
Field B: Request URI
Extract B: (. ​​*)
Output to: Request URI
Constructor: / $ A1 $ B1

As a result, in Google Analytics reports instead of such records:

image

we get these:

image

As a result of this work, we get a working tool for tracking groups of sites.

Source: https://habr.com/ru/post/88039/


All Articles