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 site.com, you are redirected to m.site.com.

  • Using a human-readable URL (option available in some CMS): when visiting site.com/?cat=23&item_id=4242, you are redirected to site.com/furniture/table.

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

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

  • 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 site.com to register.site.com. In this case, a visit site.com will be determined correctly, but for users registered on register.site.com the source will be site.com.

All these cases have one thing in common: when visiting the original site site.com, 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 site.com with a redirect to paris.site.com. Instead, add a link directly to paris.site.com 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 site.com domain, while the registration page and the service itself are located on another domain, for example, cloud.site.com (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 = 'site.com';</script> – instead of site.com, 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 site.com and cloud.site.com.

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

    <script>window.roistatCookieDomain = 'site.com';</script>
    <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', 'cloud.roistat.com', '0');
    </script> 
    

If you have different first-level domains within the same project (e.g. site.eu and site.com), 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 (site.com, up.site.com, down.site.com).

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:

    http://site.com/?utm_source=ads&utm_medium=cpc&utm_campaign={campaign_id}&utm_content={ad_id}&utm_term={keyword}

    And the redirect goes to:

    http://city.site.com

    In this case, you need to set up redirection so that it leads to the following address:

    http://city.site.com/?utm_source=ads&utm_medium=cpc&utm_campaign={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 site.com to city.site.com

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
header("Location: http://city.site.com/{$param}");

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.

Example

The user visited test.com/?utm_source=ads, where the Roistat tracking code is installed. The visit number 12300 was assigned to this user. After that, there should be a transition to site.com using the site.com/?roistat_visit=12300 link.

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

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="http://site.com/<?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

Example

The user goes to test.com/?utm_source=ads and is redirected to the page from the A/B test: test.com/new/?utm_source=ads. 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.