Route with additional condition

Extended Google Analytics measuremet.

Usage

This route works similarly as a BaseRoute, to reach a route match must be defined additional function and this function must return true. Most commonly is used to form submission check, when the URL does not change, only confirmation message is printed. Such action cannot be set as Google Analytics goal. In that case Base route with additional condition enables checking if success message in displayed and enables change a tracked URL from

www.toys.com/contact

to

www.toys.com/contact/sent

after submitting the form. And this URL can be set as a goal in Google Analytics.

Documentation

It is needed to create a route 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 func = function(currentUrl) {
        return true|false;
    }
    var categorySearch = new _gas.route.ConditionOnUrl('/search/term/$1/category/$2', '/search?term=$1&category=$2', func);
    
    _gas.addRoute(categorySearch);
    _gas.push(['_setDomainName', '.ga-script.org']);
    _gas.create('UA-123456-7', 't1');
</script>

This route tracks pageview afrer DOM ready event.

Parameters

Constructor has four parameters, first, second and third 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.
  • Condition function - return true if URL should be matched, false otherwise. Route is matched only is matched "original URL" and "condition function" returns true.
  • 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

Change of URL of sent form

User is on page with URL /contact. Ahter he or she sends a form, a confirm message is displayed, but an URL does not change. To set a form submit as a goal in Google Analytics we change an URL to /contact/sent.

<script type="text/javascript" src="/analytics.js"></script>
<script type="text/javascript">
    var func = function(url) {
        return jQuery('p.message-ok').length > 0; // or other selector
    }
    var contactRoute = new _gas.route.ConditionOnUrl('/contact', '/contact/sent', func);
    _gas.addRoute(contactRoute);
    _gas.push(['_setDomainName', '.ga-script.org']);
    _gas.create('UA-123456-7', 't1');
</script>