Skip to content

Using Redirects

Problem Description

If you use redirects or switching to another domain on your website, this may lead to incorrect statistics. Examples of such cases:

  • Redirection to the mobile version of the site: when visiting, you are redirected to

  • Using a human-readable URL (option available in some CMS): when visiting, you are redirected to

  • Redirection to another version of the page if A/B testing is used: when visiting, you are redirected to

  • Switching to a domain for another region if the website is designed for several regions: when visiting you are redirected to

  • Links to another domain for registration (typical for SaaS services where the website and the service itself are located on different domains): for example, a redirect from to In this case, a visit will be determined correctly, but for users registered on the source will be

All these cases have one thing in common: when visiting the original site, users are redirected, which leads to the loss of tags in the link. This can be seen in the address bar of the browser: if you open a link with tags (for example, by clicking on an ad), you will end up on a page where these tags are not present.

Possible Solutions

There may be different solutions depending on the situation, so you can choose the one that suits you best. After solving the problem in any of the ways listed below, all original sources of visits will be saved and this will not affect your statistics in any way.

Please note:

It is necessary to install the Roistat tracking code on all websites involved in the redirection process.

Disabling Redirection

If you can easily turn off redirects, this will be the easiest solution.

For example, you can disable redirects to human-readable URLs if this feature is used without any benefit.

Specifying the Destination Website in Ads

This solution is relevant when it is possible to send traffic directly to the destination page.

For example, you know that your Google Ads campaign has been launched only in Paris. In this case, it makes no sense to add a link to with a redirect to Instead, add a link directly to in your ads.

Setting Up a Single Domain for All Websites

If visits between certain domains should be considered visits of the same user within one Roistat project, you can store all cookies on one main website.

For example, in SaaS services, the main site is often located on the domain, while the registration page and the service itself are located on another domain, for example, (as done in Roistat).

In this case, you can specify the main domain. After that, transitions to next level domains will not be considered separate visits.

  1. Make sure that the tracking code from the same Roistat project is installed on all domains.

  2. On all website pages, add the line where the main domain is set. The line must be added above the tracking code.

    <script>window.roistatCookieDomain = '';</script> – instead of, specify the main domain.

    This line must be added to all domains. In the case of the example above, it must be added both to and

    The result is code like this (the following code is an example – don't copy it):

    <script>window.roistatCookieDomain = '';</script>
    (function(w, d, s, h, id) {
        w.roistatProjectId = id; w.roistatHost = h;
        var p = d.location.protocol == "https:" ? "https://" : "http://";
        var u = /^.*roistat_visit=[^;]+(.*)?$/.test(d.cookie) ? "/dist/module.js" : "/api/site/1.0/"+id+"/init";
        var js = d.createElement(s); js.async = 1; js.src = p+h+u; var js2 = d.getElementsByTagName(s)[0]; js2.parentNode.insertBefore(js, js2);
    })(window, document, 'script', '', '0');

If you have different first-level domains within the same project (e.g. and, this solution will not work for you. You will need to install the Roistat tracking code on each page.

The above solution suits you if:

  • You have one main domain;
  • Also, various subdomains are used;
  • There are transitions and redirects between these domains within the same project (,,

Keeping Tags when Redirecting

This method can be used if you want to keep the origin source after redirection to another domain. The difference between this method and the Keeping visit number when going to another domain method is that in this case, the Roistat tracking code has not yet been triggered during the redirection, so the value of the visit number is not yet known.

The method consists of two steps:

  1. It is necessary to keep all tags of the link when redirecting. For example, if the link leads to:{campaign_id}&utm_content={ad_id}&utm_term={keyword}

    And the redirect goes to:

    In this case, you need to set up redirection so that it leads to the following address:{campaign_id}&utm_content={ad_id}&utm_term={keyword}

  2. The original referrer must be stored in a cookie called roistat_referrer.

Example for redirecting from to

Please note that this is not a real code. It is an example for the developer to understand the algorithm of work.

// ..
// Above is your site's code before the redirect occurs 
// ..

// All tags that were in the link are saved here
$param = !empty($_SERVER['QUERY_STRING']) ? "?" . $_SERVER['QUERY_STRING'] : "";

// The roistat_referrer cookie stores the original referrer of the visit
setcookie("roistat_referrer", isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "");

// This line redirects to another domain with all tags saved

Keeping Visit Number when Going to Another Domain

You can use this method if you want the original source to be preserved when the user moves from one domain to another. The difference between this method and the Keeping Tags when Redirecting method is that the visit number is already known, since the Roistat counter has been triggered.

To keep the visit number when referring to another domain, open Settings → Tracking code → Show advanced settings and activate the Multi-domain mode.

In addition, you can save the visit number (roistat_visit cookie) when going from one domain to another using a link that passes the roistat_visit GET parameter. The tracking code from the same Roistat project must be installed on all domains.


The user visited, where the Roistat tracking code is installed. The visit number 12300 was assigned to this user. After that, there should be a transition to using the link.

The Roistat tracking code must be installed on the second website –

The following code is an example for the developer to understand the algorithm of work:

// ..
// Above is your site's code before the redirect occurs
// ..

// Getting the roistat_visit cookie value
$roistatVisitId = array_key_exists('roistat_visit', $_COOKIE) ? $_COOKIE['roistat_visit'] : ""

// Adding the roistat_visit value to the GET parameter
$queryString = "?roistat_visit=" . $roistatVisitId;

// Link to another domain
<a href="<?php echo $queryString; ?>">Link to another domain with keeping the origin source</a>

Using the Visit Source of the Page from Which the User Came to the Site

You can configure using the source of the page from which the user was redirected. To do this, you need to add the following cookie to the destination page: roistat_marker_from_referrer = 1


The user goes to and is redirected to the page from the A/B test: If the Roistat tracking code is installed on this page and the roistat_marker_from_referrer = 1 cookie is added, the system will record the visit from the source specified in the link.