Cross-Domain Tracking in Google Analytics

Cross-Domain Tracking

A default setup of Google Analytics is designed to track content and visitor data for a single domain, such as dogs.example.com or www.example.com.

However, suppose you want to use Analytics for more complex situations, such as tracking

  • all visitors to both dogs.example.com and www.example.com, with data for both showing in the same report profile
  • only visitors to the www.example.com/dogs subdirectory in a separate report as if it were a single site
  • all visitors to two domains that you own, such as www.example.com and www.example2.com, with data for both showing in the same report profile
  • visitor and pageview data for content in an iFrame of another domain.

In any of these cases, you’ll need to do some customization to the tracking code in order to correctly track visitors. Before doing this, it’s a good idea to read about Domains and Directories as they are defined in a default Analytics installation.

Use the following table to view sections in the rest of this document, and as a reference for the tracking code modifications suggest for each method. Once you have decided on a tracking method, be sure to also read the Site Linking Considerations at the end of this document.

Type of TrackingExample DomainsConfiguration Needed
Restricted to a single sub-directorywww.example.com/myStore
_setCookiePath("/myStore/");
Across two sub-directories on the same domain
  • www.example-commerce-host.com/myStore
  • www.example-commerce-host.com/myCart
_setCookiePath("/first/Path/");
_cookiePathCopy("/new/Path/");
Between a domain and a sub-directory on another domain
  • www.example-petstore.com
  • www.example-commerce-host.com/petStoreCart
_setDomainName("none");
_setAllowLinker(enable);
_setAllowHash(false);
_link();
_linkByPost();
Across a domain and its sub-domains
  • www.example-petstore.com
  • dogs.example-petstore.com
  • cats.example-petstore.com
_setDomainName(".example-petstore.com");
_setAllowHash(false);
Across multiple domains and/or sub-domains
  • www.example-petstore.com
  • www.my-example-blogsite.com
  • dogs.example-petstore.com
_setDomainName("none");
_setAllowLinker(enable);
_setAllowHash("false");
_link();
_linkByPost();
Tracking an iFrame hosted in another domain
  • www.example-parent.com
  • www.my-example-iframecontent.com
iframe.src = pageTracker._getLinkerUrl("http://www.my-example-content.com");

Restricting Tracking to a Single Subdirectory

There are two common cases for restricting Analytics tracking to a single sub-directory of a larger domain:

  • You have access only to a sub-directory of a larger website, such as when you use an ecommerce hosting service
  • You want to limit tracking to a sub-directory of a larger website, such as when you launch a project on your company’s website, which also uses Analytics tracking

Suppose you have a profile in your Analytics account where you track visitor data to your online store. The store itself is hosted on www.example.com/myStore/. You only want to record visitor and campaign data for the /myStore subdirectory, and not for any other page on the example.com domain. In this case, you also only have access to those web pages included in that sub-directory.

In order to set up tracking for your store, you will need to use the _setCookiePath() function in your tracking code, like this:

var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setCookiePath("/myStore/");
pageTracker._trackPageview();  

Once this is set up, your reports will correctly display visitor and campaign data only for your sub-directory. This is because the cookie path is set to your sub-directory rather than to the root directory of www.example.com and your profile will read and write to a unique set of cookies for your store only.

It is considered a best practice to use the _setCookiePath() function in this scenario. If you do not, there are two possible impacts to your reports:

  • Other Analytics account cookies set on www.example.com can be shared by your account.This means that visitor statistics like new vs returning, time on site, and number of visits might be incorrect, since your visitors’ session cookies might also reflect activity on other parts of www.example.com.
  • Your reports might reflect campaign details from another store.For example, suppose a visitor clicks on an email campaign that goes to another store on the same domain: www.example.com/shoeStore. In this case, the referral information in the visitor’s cookie is set to the campaign named Fall Shoe Sale. Later, that same visitor searches on Google for pet supplies and clicks on a search result to your store and purchases a dog collar. Since ad and email campaign referrals persist over search results, you will see the campaign Fall Shoe Sale generating sales for dog collars. This is because the _utmz cookie for your online store was not set uniquely for your path.

If you restrict visitor cookies to only your path, campaign and session data for another part of www.example.com will not be included in your reports, since the cookie path is restricted. (Read about Cookies for more background info on how cookies are read by web browsers.)

If you are tracking visitor and referral traffic for a sub-directory, you might also want to set up an Include filter to the sub-directory to ensure that only content for that sub-directory shows up in your report. See the pre-defined filter article in the Help Center for more information.

Tracking Across Two Sub-directories on the Same Domain

You might want to track multiple subdirectories on a domain where you do not have access to the pages for the larger domain. For example, suppose you have both an online store and a shopping cart on a service provider’s site:

  • www.example-commerce-host.com/myStore/
  • www.example-commerce-host.com/myCart/

If you want to track user sessions and campaign data from your store to the shopping cart, you can use the _cookieCopyPath() method to copy the cookie information to another directory on the same domain. In this case, the key parts of the tracking code snippet for all pages would look like:

var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setCookiePath("/myStore/");
pageTracker._cookiePathCopy("/myCart/");
pageTracker._trackPageview();  

In this case, a unique cookie is set for the domain, limited to the first directory provided, and the same cookie data is also copied over to the second directory provided.

Tracking Across a Domain and a Sub-directory of Another Domain

Another common tracking scenario is to track visitor and traffic data between a single domain and a sub-directory of a different domain. This typically occurs when you have your online store on one domain, but you use a 3rd-party website for your shopping cart, where you are limited to a sub-directory of that site. The follow example URLs each contain recommended customizations for the tracking code on their pages.

  • Primary: www.example-petstore.com
    var pageTracker = _gat._getTracker("UA-12345-1");
    pageTracker._setAllowLinker(true);
    pageTracker._setAllowHash(false);
  • Secondary: www.example-commerce-host.com/petStoreCart
    var pageTracker = _gat._getTracker("UA-12345-1");
    pageTracker._setDomainName("none");
    pageTracker._setAllowLinker(true);
    pageTracker._setAllowHash(false);

For any form submission from www.example-petstore.com to the shopping cart website, you would use the _linkByPost() method to copy traffic and visitor cookie data via HTTP POST to the shopping cart site. The _setAllowLinker() method directs the target site to read cookie data from the POST data rather than from the regular user session information. In this way, you can pass cookie data set on one domain to another and thereby retain the visitor session from your online store to your shopping cart.

onSubmit="pageTracker._linkByPost('www.example-commerce-host.com/petStoreCart/begin.php');"

Similarly, use the _link() function to transfer visitor data on any links going from the online store to the shopping cart.

<a href="pageTracker._linkByPost('www.example-commerce-host.com/petStoreCart/legalTerms.php');"

Tracking Across a Domain and Its Sub-domains

As mentioned above, a default installation of the tracking code automatically tracks visitor data to a specific domain or sub-domain. This means that even if you manage both a domain and a sub-domain, you must make modifications to the tracking code in order to share visitor data across both of them.

Suppose you have the following URLs that you want to track as a single entity:

  • www.example-petstore.com
  • dogs.example-petstore.com
  • cats.example-petstore.com

For this setup, you would use the following key customizations for the tracking code on all pages.

  var pageTracker = _gat._getTracker("UA-12345-1");
  pageTracker._setDomainName(".example-petstore.com");
  pageTracker._trackPageview(); 

In this example, notice the use of the leading “.” in the declaration of the domain. Since the Analytics tracking code creates a hash value from the string that you provide in the _setDomainName() method, it’s recommended that you follow standard conventions and use the leading “.” when setting your domain name in this way. See the documentation on _setDomainName() for more details.

Tracking Across Multiple Domains and Sub-domains

In this scenario, you might want to enable tracking across a given domain and its sub-domains, and include a separate domain in the mix. For example, suppose you have an online store and you also want to track visitor and campaign data to your blog. The following shows key tracking code customizations for three example URLs.

  • Primary: www.example-petstore.com
    var pageTracker = _gat._getTracker("UA-12345-1");
    pageTracker._setDomainName(".example-petstore.com");
    pageTracker._setAllowLinker(true);
    pageTracker._setAllowHash(false);

    In addition, you would use the _link() and _linkByPost() for any form or links to www.my-example-blogsite.com. This is not required for links to the sub-domain of this site, since the _setDomainName() function defined .example-petstore.com as the domain to enable cookie access for any other subdomains.

  • Secondary: dogs.example-petstore.com
    var pageTracker = _gat._getTracker("UA-12345-1");
    pageTracker._setDomainName(".example-petstore.com");
    pageTracker._setAllowLinker(true);
    pageTracker._setAllowHash(false);

    In addition, you would use the _link() and _linkByPost() for any form or links to www.my-example-blogsite.com. as well as to the primary domain.

  • Terciary: www.my-example-blogsite.com
    var pageTracker = _gat._getTracker("UA-12345-1");
    pageTracker._setDomainName("none");
    pageTracker._setAllowLinker(true);
    pageTracker._setAllowHash(false);

Tracking Across iFrames

In a site where the transfer between domains is done by opening a new window or by including content in an iFrame, you will need to to use the _getLinkerUrl() method to transfer visitor and campaign cookies from one domain to another. For example, suppose you a form in an iFrame hosted on www.my-example-iframecontent.com and you want to transfer visitor information from the parent page that hosts the iFrame on www.example-parent.com.

You would use JavaScript to load the iFrame and pass in the cookie information using the _getLinkerURL() method.

var iframe = document.getElementById("myIFrame");
iframe.src = pageTracker._getLinkerUrl("http://www.my-example-iframecontent.com/");

Site Linking Considerations

When setting up tracking in the various scenarios described above, keep in mind the following:

  • A session from one domain can only be transferred to another domain via a click, a form submission, or via _getLinkerURL.The passing of the cookie data via these three methods links two page views under a single session. If a user independently visits two sites that are tracking in the same profile, such as through a bookmark, these visits will still be counted under separate sessions. In this scenario, the linking methods are not invoked, and thus there is no way to determine the initiating session for a given user.
  • Choose a primary profile for multi-domain tracking.Enable multi-domain tracking by first establishing a profile for your primary domain. After you have set up a profile and retrieved the tracking code for your primary domain, use the same tracking code for all other installations and configure the necessary linking functions. In this way, your configuration will be set up correctly before data is recorded, and you will have all data historically located in one profile.

    If you have already established separate profiles for each website installation, you can still enable linking of the separate web properties, but you will need to choose one existing profile to act as the primary one for aggregating the data from both sites. The data collected in each profile prior to linking cannot be merged into a primary profile, so the historical data for the secondary profile will remain separate.

  • Content reports always show the request URIFinally, once you have domain linking established, you will see only the request URI in the content reports, and not the top-level or sub-domain for a given page. So for example, if you have a page on http://www.example.com/index.php and another page on http://www.example2.com/index.php, you will see listings in the reports for two distinct index.php pages, and will not be able to distinguish which page is from which domain. You can include the referring domain in the page reports by setting a filter that will include all components of the page URL in the content reports, as follows:
    1. In the Profile Settings page, click the Add Filter link.
    2. Choose Add New Filter and provide the filter a name.
    3. Choose Custom Filter and select Advanced on the Filter type settings.
    4. Under Advanced settings, choose Hostname for FieldA and Request URI for FieldB.
    5. Set the values for each of these fields to (.*), which is an expression that captures all characters.
    6. Set the Output To –> Constructor option to Request URI and provide $A1$B1 as the value for that choice.

    This will capture the www.example.com portion of your URL and include that at the beginning of your page URL in the content reports section.

Leave a Reply

Your email address will not be published.