How to override the Page Tracking value in Google Analytics

Analytics_iconI came across with an undocumented functionality on Google Classic Analytics (ga.js) snippet that allows to override the page tracking when you use “_gaq.push([‘_trackPageview’]);“.

Note: The ga.js snippet is part of Classic Analytics. If you’re using Universal Analytics (analytics.js) please check: Page Tracking – Web Tracking (analytics.js)

Tracking Code Quickstart

The Analytics snippet is a small piece of JavaScript code that you paste into your pages. It activates Google Analytics tracking by inserting ga.js into the page. To use this on your pages, copy the code snippet below, replacing UA-XXXXX-X with your web property ID. Paste this snippet into your website template page so that it appears before the closing </head> tag.

<script type="text/javascript">// <![CDATA[
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
// ]]></script>

To override the default page value, you can pass to the _gaq.push method an additional parameter, in this case, the custom path that you want to track:

_gaq.push(['_trackPageview', customPath]);

In the next image you can see the difference between the custom tracking page views code vs the default way.

google_analytics_custom_pathNote: The _gaq global object can be used directly for asynchronous page tracking via the push(…) method.

I really hope you find this information useful.