Base Route

Extended Google Analytics measuremet.

Usage

Route is used when it is needed to send to Google Analytics URL with shuffled paths. Eg. in e-shop with category search, where URL after search look as follows:

www.toys.com/search/term/pink+teddy+bear/category/bears

Google Analytics cannot handle such address. Using BaseRoute you can send a virtual pageview shaped like this:

www.toys.com/search?term=pink+teddy+bear&category=bears

And Google Analytics can handle this URL now.

Documentation

It is needed to create a route object and pass to object _gas via addRoute method. Example is here:

<script type="text/javascript" src="/analytics.js"></script>
<script type="text/javascript">
    var categorySearch = new _gas.route.Base('/search/term/$1/category/$2', '/search?term=$1&category=$2'); // CREATE ROUTE
    _gas.addRoute(categorySearch); // ADD IT INTO
    _gas.setDomainName('.ga-script.org');
    _gas.create('UA-123456-1', 't1');
    </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 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. These tags are replaced with corresponding values obtained from the original 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

Changing the search route

User is redirected on your website after search to URL /search/query+term. URL is needed to modify to the shape, which can be processed by Google Analytics. This code solves the problem.:

<script type="text/javascript" src="/analytics.js"></script>
<script type="text/javascript">
    var search = new _gas.route.Base('/search/$1', '/search?q=$1');
    
    _gas.addRoute(categorySearch);
    _gas.push(['_setDomainName', '.ga-script.org']);
    _gas.create('UA-123456-7', 't1');
</script>

In Google Analytics profile configuration it is necessary to set a search term parameter to q.

I need to set a variable to users after search

We use a the example above with search. In addition we pass also the third parameter - function called before _trackPageview.

<script type="text/javascript" src="/analytics.js"></script>
<script type="text/javascript">
    var setMyVar = function() {
        //executed only when URL is shaped like /search/some+string
        _gas.push(['_setCustomVar', 1, 'Searched term', 'some term here', 2]);
    }
    
    var search = new _gas.route.Base('/search/$1', '/search?q=$1', setMyVar);
    
    _gas.addRoute(categorySearch);
_gas.push(['setDomainName', '.ga-script.org']); _gas.create('UA-123456-7', 't1'); </script>