Custom variables route

Extended Google Analytics measuremet.

Usage

Is used at times when you need to put into tracked URL other texts then current URL contains. It can be useful in cases when your website use an URLs with ID of product instead of name - you do not need to search for all names by their ID when you analyze the website. For example:

www.toys.com/category/53/product/25 // WHAT DOES IT MEAN?
        www.toys.com/category/teadys/product/pink-teddy-bear // CLEAR

Documentation

It is needed to create routu and pass to object _gas via addRoute method. Following code solves a problem described above:

<script type="text/javascript" src="/analytics.js"></script>
<script type="text/javascript">
    var loadParams = function(index) {
        switch (index) {
            case 3:
                return 'teady'; // will be replaced as $3
            case 4:
                return 'pink-teddy-bear'; // will be replaced as $4
            default:
                return '';
        }
    }

    var productRoute = new _gas.route.CustomVariable('/category/$1/product/$2', '/category/$3/product/$4', loadParams);
    _gas.addRoute(productRoute);
</script>

Parameters

Constructor has three parameters, the first and second one are required.

  • Original URL - real URL address. It may include tags starting with char $, which is followed by numbers - e.g. $1, $2. They represent strings without chars /, ? and &. These parts are parsed and used for as substring of "replaced URL".
  • Replaced URL - virtual URL, which is sent to Google Analytics server instead of original ULR. You can use the same tags as in the original URL. This tags are replaced with corresponding values obtained from the original URL and the function (third parameter).
  • Function which loads additional values - if tag $x is not found in "original URL", route calls this function and its return value uses as replacement.
    Header has format function (int index) : string. Function parameter is tag number - e.g. for tag $123 is passed 123. Function returns a string, which will be replaced in "replaced URL".
  • Function - anonymous function, which is executed only on the URL corresponding to the original URL (first parameter) before sending _trackPageview. Function is executed only once even if more trackers are created.

Use cases

Loading category and product names into URLs

Web has a URL on product pages in format /category/123/product/456. You need to find an IDs and replace it with real names. This situation following this code. You need to include a jQuery library.

<script type="text/javascript" src="/analytics.js"></script>
<script type="text/javascript">
    var loadParams = function(index) {
        switch (index) {
            case 3:
                return escape( $('#name-of-category').text() ); // will be replaced as $3
            case 4:
                return escape( $('#name-of-product').text() ); // will by replaced as $4
            default:
                return '';
        }
    }
    var productRoute = new _gas.route.CustomVariable('/category/$1/product/$2', '/category/$3/product/$4', loadParams);
    _gas.addRoute(productRoute);
    _gas.push(['_setDomainName', '.ga-script.org']);
    _gas.create('UA-123456-7', 't1');
</script>