English | Site Directory

Google Analytics Tracking Code

Tracking API: Domains and Directories

This reference describes the methods that you use for customizing how Google Analytics reporting works across domains, across different hosts, or within sub-directories of a website.

GATC Domain/Directory Methods

Method Details

_cookiePathCopy()

    _cookiePathCopy(newPath)
    Changes the paths of all GATC cookies to the newly-specified path. Use this feature to track user behavior from one directory structure to another on the same domain. In order for this to work, the GATC tracking data must be initialized (_initData() must be called).
    var pageTracker = _gat._getTracker("UA-12345-1");
    pageTracker._initData();
    pageTracker._trackPageview();  
    pageTracker._cookiePathCopy("/newSubDirectory/");

    parameters

      String   newPath New path to store GATC cookies under.

_link()

    _link(targetUrl, useHash)
    This method works in conjunction with the _setDomainName() and _setAllowLinker() methods to enable cross-domain user tracking. The _link() method passes the cookies from this site to another via URL parameters (HTTP GET). It also changes the document.location and redirects the user to the new URL.
    <a href="http://www.newsite.com" onclick="pageTracker._link('http://www.newsite.com');return false;">Go to our sister site</a>

    You must also enable linking on the target site (pageTracker._setAllowLinker(true);) in order for link to work properly.

    parameters

      String   targetUrl URL of target site to send cookie values to.
      String   useHash Set to true for passing tracking code variables by using the # anchortag separator rather than the default ? query string separator. (Currently this behavior is for internal Google properties only.)

_linkByPost()

    _linkByPost(formObject, useHash)
    This method works in conjunction with the _setDomainName() and _setAllowLinker() methods to enable cross-domain user tracking. The _linkByPost() method passes the cookies from the referring form to another site in a string appended to the action value of the form (HTTP POST). This method is typically used when tracking user behavior from one site to a 3rd-party shopping cart site, but can also be used to send cookie data to other domains in pop-ups or in iFrames.
    <form action="http://www.shoppingcartsite.com/myService/formProcessor.php" 
         name="f" method="post" onsubmit="pageTracker._linkByPost(this)">
    . . .
    </form>

    In addition, _setAllowLinker() must be set to true on the destination web page in order for linking to work.

    parameters

      HTMLFormElement   formObject Form object encapsulating the POST request.
      String   useHash Set to true for passing tracking code variables by using the # anchortag separator rather than the default ? query string separator.

_setAllowHash()

    _setAllowHash(enable)
    Sets the allow domain hash flag. By default, this value is set to true. The domain hashing functionality in Google Analytics creates a hash value from your domain, and uses this number to check cookie integrity for visitors. If you have multiple sub-domains, such as example1.example.com and example2.example.com, and you want to track user behavior across both of these sub-domains, you would turn off domain hashing so that the cookie integrity check will not reject a user cookie coming from one domain to another. Additionally, you can turn this feature off to optimize per-page tracking performance.

    parameters

      Boolean   enable If this parameter is set to true, then domain hashing is enabled. Else, domain hashing is disabled. True by default.

_setCampaignTrack()

    _setCampaignTrack(enable)
    Sets the campaign tracking flag. By default, campaign tracking is enabled for standard Google Analytics set up. If you wish to disable campaign tracking and the associated cookies that are set for campaign tracking, you can use this method.

    parameters

      Boolean   enable True by default, which enables campaign tracking. If set to false, campaign tracking is disabled.

_setAllowLinker()

    _setAllowLinker(enable)
    Sets the linker functionality flag as part of enabling cross-domain user tracking. By default, this method is set to false and linking is disabled. See also _link(), _linkByPost(), and _setDomainName() methods to enable cross-domain tracking.
    pageTracker._setAllowLinker(true);

    parameters

      Boolean   enable If this parameter is set to true, then linker is enabled. Else, linker is disabled.

_setCookiePath()

    _setCookiePath(newCookiePath)
    Sets the new cookie path for your site. By default, Google Analytics sets the cookie path to the root level (/). In most situations, this is the appropriate option and works correctly with the tracking code you install on your website, blog, or corporate web directory. However, in a few cases where user access is restricted to only a sub-directory of a domain, this method can resolve tracking issues by setting a sub-directory as the default path for all tracking. Typically, you would use this if your data is not being tracked and you subscribed to a blog service and only have access to your defined sub-directory, or if you are on a Corporate or University network and only have access to your home directory. In these cases, using a terminal slash is the recommended practice for defining the sub-directory.
    pageTracker._setCookiePath("/~username/");
    pageTracker._setCookiePath("/myBlogDirectory/");

    parameters

      String   newCookiePath New cookie path to set.

_setDomainName()

    _setDomainName(newDomainName)
    Sets the domain name for cookies. There are three modes to this method: ("auto" | "none" | [domain]). By default, the method is set to auto, which attempts to resolve the domain name based on the location object in the DOM.

    Set this method explicitly to your domain name if your website spans multiple hostnames, and you want to track visitor behavior across all hosts. For example, if you have two hosts: server1.example.com and server2.example.com, you would set the domain name as follows:

    pageTracker._setDomainName(".example.com");


    Be sure to use a leading "." in front of your domain name, as illustrated here. The leading period ensures that the cookie will be accessible across all hosts. Otherwise, the cookie is accessible only in example.com.

    Set this method to none in the following two situations:
  • You want to disable tracking across hosts.
  • You want to set up tracking across two separate domains. Cross- domain tracking requires configuration of the _setAllowLinker() and _link
  • methods.
    pageTracker._setDomainName("none");

    parameters

      String   newDomainName New default domain name to set.