Google Analytics cross-domain tracking is now very easy to set up using Google Tag Manager.
However, depending on the structure of the site, there are cases where it is not possible to set up cross-domain tracking successfully. In this article, I would like to explain four patterns that do not work properly with normal cross-domain tracking and how to deal with them.
Trap 1: Redirect 1
phenomenon
The first trap event is a server-side redirect that leaves out parameters for cross-domain tracking. In addition to redirecting when the URL is different between PCs and smartphones, there are cases where 301/302 redirects are performed due to site renewal and page consolidation, and page display content is controlled by login/non-login. This may occur depending on how the redirect is implemented, such as when redirecting is performed for some reason.
To check if you are addicted to this trap, when you click on a cross-domain link, “At first, a query parameter is given to the page URL of the transition destination”, “A redirect is performed at the time of page transition, Let’s check if the two conditions of “The query parameter disappears after the redirect” are satisfied.
solution
The solution to redirects is the use of “hash parameters”. Specifically , please refer to the previous article on this site, ” For advanced users, cross-domain tracking using hash format .” Just quote the URL.
Trap 2: Redirect 2
phenomenon
There is also a different pattern of traps for redirects. It is a pattern that “the link destination URL on HTML is the URL of the same domain, but the link destination URL is redirected to an external domain by server-side redirection.” Since the link destination URL is the same as your own domain, even if you set the normal cross-domain “auto link domain”, you will not get the desired result.
solution
In the auto-link domain setting, if the domain of the current page and the domain of the click destination match, the cross-domain tracking function will not be enabled regardless of the setting. The solution is to use Google Tag Manager and use the Google Analytics tag’s “decorate link” feature. “Decorate link” is a type of “tag” that can be set using Google Tag Manager, similar to page view tags and event tags.
This tag is intended to be used with link click triggers and is responsible for adding query parameters for cross-domain tracking to the triggered link. And importantly, it doesn’t rely on autolink domain settings and adds parameters for cross-domain tracking, even if the domain of the current page and the domain of the destination URL are the same. .
Trap 3: GA tag of transition destination page is in iframe
phenomenon
There is a possibility that you may encounter a case that corresponds to this phenomenon when using an ASP inquiry form. Specifically, as shown in the figure below, it is a case where the GTM/GA tag cannot be set on the transition destination page body of cross-domain tracking, and the GTM/GA tag can only be set in the iframe of the transition destination page.
In this case, the page in the iframe where the GTM/GA tag is installed does not have parameters for cross-domain tracking, so cross-domain tracking cannot be established as it is.
solution
If you can place the Google Analytics tag outside the iframe, that’s the surest solution. However, in cases like this, it would be difficult to set the Google Analytics tag outside the iframe.
A workaround is to get the calling URL from within the iframe and use the cross-domain parameters set in that URL. Specifically, when I get the referrer using JavaScript inside the iframe, I can get the URL of the parent Window, which has the query parameters set.
Set the following HTML tag as a setup tag for the Google Analytics tag on the relevant page. This JavaScript code is supposed to be called inside an iframe, extracts the cross-domain tracking parameters from the referrer URL (which is assumed to have cross-domain tracking parameters here), and returns the current page I’m setting it to the hash of (the page in the iframe). If you set it as a query parameter, the inside of this iframe will be reloaded again, so I set it as a hash.
<script>
(function() {
var _ga = document.referrer.match(/[?&]?_ga=([^&]*)/g)[0];
location.hash = '#_ga=' + _ga;
})();
</script>
As a result, the cross-domain tracking parameter is passed to the URL of the page where GTM / GA is installed, so the cross-domain tracking will be successful.
Trap 4: External domain transition by form
phenomenon
In a normal cross-domain tracking setup, the autolinker plugin performs parameter addition processing when transitioning to a specified domain with <a> tags (strictly speaking, <area> tags also). However, in addition to the <a> tag, there is also a form transition method for page transition. The default configuration does not support form transitions, which can cause cross-domain tracking to fail.
solution
Form transitions are not supported by default, but it is possible to configure support for form transitions using the Auto Linker plugin options.
In settings using Google Tag Manager, simply set the “Decoration Form” setting to True as shown below to solve the problem.
Trap 5: Page transition using JavaScript location.href etc.
phenomenon
Page transition methods are not limited to transitions using <a> tags (or <area> tags) and transitions using <form> tags. In addition, there are methods of page transition by directly rewriting location.href in JavaScript and methods of page transition using History API.
If you use such location.href or History API, you can’t get the auto-linker plugin to work. Recently, History API is mostly used in single-page applications using Vue.js (Nuxt.js), AngularJS, React, etc., so you can take measures in advance. However, location.href is often used abruptly, so there are times when it doesn’t work well and after further investigation it turns out that location.href was the cause.
The point where this trap is easy to fall into is that <a> tags and forms are used on HTML, so you might think that cross-domain tracking can be done easily, but deep in the JavaScript code, <a> tags and forms are used. By canceling the original movement and using location.href, it often does not behave as expected.
solution
For these events, I don’t think there is a way to easily perform cross-domain measurement at this time. No automatic linker plugin is available, so you have to manually add the linker. For manual linker addition, it is written on the official help page for developers, so it is a good idea to refer to it. Strictly speaking, there are two ways to add it manually: using the decorate method or extracting from the linkerParam field and adding it to the URL. The method of using the decorate method is the same as the behavior of the “Decorate Link” tag presented in the solution for Trap 2. Therefore, for transitions such as location.href or History API, a method of manually retrieving the linker parameter from the linkerParam field is used.
Below is a sample code (image). Please test it thoroughly before using it.
document.getElementById('site-search-btn').addEventListener('click', function() {
var query = document.getElementById('query').value;
var tracker = ga.getAll()[0];
location.href = "https://mysite.search.com?q=" + query + "&_ga=" + tracker.get('linkerParam');
});
summary
In this article, we have explained 5 patterns of traps that are easy to fall into in cross-domain tracking. If it is basic cross-domain tracking, it has been covered in various articles, and by using Google Tag Manager, it can be realized without writing program code, so I think that more and more people are familiar with it. However, if you want cross-domain tracking to succeed in any case, you need to be able to deal with these traps.