Base Route

Extended Google Analytics measuremet.


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:

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

And Google Analytics can handle this URL now.


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.create('UA-123456-1', 't1');


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.push(['_setDomainName', '']);
    _gas.create('UA-123456-7', 't1');

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.push(['setDomainName', '']); _gas.create('UA-123456-7', 't1'); </script>