Changing of tracked URL

Extended Google Analytics measuremet.

Typical examples, where you need to change a tracked URL, is webs build on CMS. Drupal redirects the user who used a site search to URL /search/node/searched+term. Google Analytics cannot parse a query term from URL in this type and you (analyst) are deprived of valuable information. Some websites does not change a URL after submitting of a form, so you cannot measure the pageview as a target.

Another disadvantage is that you must change GA code in all templates, where you need to track a virtual pageview.

Routing in GA script solves both described problems.

Basics

URL functionality is done by two basic objects:

  • Route - is an object that represents the change of URL. In the simplest case is defined only the "URL real" and "URL changed". If the current URL is matching the "URL real", the "URL changed" is sent to Google Analytics server instead of the real URL. Route functionality can be more dificult - eg. it can search some string in "URL real" and replace it in "URL changed"
  • Router - is a container for all created objects of type route.

How does it work

You can create route objects and register it into router by a _ga.addRoute( route ) : void method. It is iterated through all registered route objects before trackPageview and it is checked, whether some of them match a current URL. If it is right, changed URL returned by matched route is tracked. Otherwise, a real URL is tracked.

Example

We have a two defined routes with this functionality:

  • If the current URL is /a, send as a pageview /b (instead of /a).
  • If the current URL is /c, send as a pageview /d (instead of /c).

Both of these routes we register to router. User comes to the URL /home. The router in this situation does the following steps:

  1. Is the URL /home same as the URL /a (ie. is matching the first route)? No -> continue.
  2. Is the URL /home same as the URL /b (ie. is matching the second route)? No -> continue.
  3. No other route is registered -> real URL is tracked, i.e. /home.

Tracked pageview is /home.

User continues to URL /c. Router on a new page performs the following steps:

  1. Is the URL /c same as the URL /a (i.e. is matching the first route)? No -> continue.
  2. Is the URL /c same as the URL /c (i.e. is matching the second route)? Yes -> track a virtual URL /d.

Tracked pageview is /d.

List of defined routes

Own route definition

You can create your own routes as well.