Tracking error pages from Wordpress

Categories: GA Hacks, GA specific Your Comments 5 »

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

GA Hacks My standard word of caution for all “GA Hacks” posts - This is a tech tip and requires you to have a knowledge of html and javascript to implement and use it…

Tracking error pages is something that page tag solutions cannot track out of the box - including Google Analytics. In Chapter 9 of the book I describe how to track all your error pages using Google Analytics. Essentially, you modify the server error template to include the GATC, then use an advanced filter to rewrite the URL string.

However, if you are a Wordpress user, there is a simpler method than tinkering with your web server…

Wordpress includes a template file called "404 template" (404.php). This is the file used to display an error message if a page is not found. By default it does not contain very much:

<?php get_header(); ?>
	<div id="content">
	  <h2>Error 404 - Not Found</h2>
	</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Modifying it to this (assuming you are using ga.js in your pages):

<?php get_header(); ?>
	<div id="content">
	  <h2>Error 404 - Not Found</h2>
	<script type="text/javascript">
	  pageTracker._trackPageview("/missing pages" _udl.pathname+_udl.search)
	  document.write("<p>The page: " +_udl.pathname+_udl.search+ " is not found</p>")
	</script>
	</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

allows you to to view the URLs of missing pages in the Google Analytics Content > Top Content report (see screenshot).

Google Analytics Top Content report

As a tip, use the inline filter as shown to bubble up these pages - otherwise they could be buried deep in your reports. Once you have identified the missing URLs, click through on the pages and then select "Navigation Summary". This tells you which links within your website point to these missing pages - that is, have broken links.

The code _udl.pathname and _udl.search are Google Analytics variables captured by the GATC, so it is important that the GATC is loaded first.

Tip: if you are still using the legacy urchin.js page tag (why?), subsitute the string urchinTracker for pageTracker._trackPageview in the above functions.

Obviously there is more than one way to skin a cat. Please let me know if you have used a different hack to acheive this by leaving a comment.

Tracking links to direct downloads - Automatically

Categories: GA Hacks, GA specific Your Comments 11 »

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5 out of 5)
Loading ... Loading ...

My standard word of caution - This is a tech tip and requires you to have a knowledge of html and javascript to implement and use it…
GA Hacks

Following on from my previous post Tracking banners and other outgoing links automatically, this GA hack allows you to track downloads automatically. As you may know, tracking download files such as PDF, EXE, DOC and XLS can be achieved quite easily with the modification of the link to include an urchinTracker call to log a virtual pageview. However, as for tracking outgoing links, manually modifying each download link becomes inefficient when there are large numbers of ever changing files to track. You can overcome this by applying the JavaScript code below:

<script type="text/JavaScript">
// Only links written to the page (already in the DOM) will be tagged
// Script can be called multiple times

function addExtDocEvents() {
var as = document.getElementsByTagName("a");
var extDoc = [".doc",".xls",".exe",".zip",".pdf",".js"];
// add further document types as required

for(var i=0; i<as.length; i++) {
	var tmp = as[i].getAttribute("onclick");

// Tracking electronic documents - doc, xls, pdf, exe, zip
if (tmp != null && tmp.indexOf('urchinTracker') > -1) continue;
for (var j=0; j<extDoc.length; j++) {
if (as[i].href.indexOf(extTrack[0]) != -1 &&
as[i].href.indexOf(extDoc[j]) != -1) {
		var splitResult = as[i].href.split(extTrack[0]);
		as[i].setAttribute("onclick",((tmp != null) ? tmp : "") +
			"urchinTracker('/downloads" +splitResult[1]+ "');");
		break;
	}
}
}
}
addExtDocEvents()
</script>

The script works by looking for links within the browser’s Document Object Model (DOM) that match the file extension given in the variable array extDoc. If so it is modified to include the urchinTracker call. By this method, all file downloads will be reported as:

/downloads/the-url-that-is-clicked-on

Where the-url-that-is-on clicked on is minus ‘http://’. You can modify the JavaScript to adjust the path as required,

Tip: As for the Tracking of banners and other outgoing links, the position of this code within your page is important. This must placed after your call to the GATC. Alternatively place the addExtDocEvents() call in an onLoad event handler and host the JavaScript in a separate file.

A note on performance: Each time your page loads, this script will go through all links referenced on the page to see if it is for a download. Clearly the more links on your page, the harder the script must work. As long as the number of links on each page number in the hundreds and not thousands, performance should not be a problem. Also, pages with a large number of links, it is possible that visitors will click on a download link before the script has modified it. The result is that click through will not be tracked by Google Analytics.

I have combined both the Tracking of banners and other outgoing links - automatically with this hack into a single script that is available at: http://www.advanced-web-metrics.com/blog/scripts.

Did you find this tip useful? All tips are being grouped under the category GA Hacks. Please provide your feedback with a comment.

Customising the list of recognised search engines

Categories: GA Hacks, GA specific Your Comments 7 »

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

GA HacksMy standard word of caution - This is a tech tip and requires you to have a knowledge of html and javascript to implement and use it…

Google Analytics shows which search engine your visitors/customers have used in the Traffic Sources > Search Engines report. To view the list of all the search engines that Google Analytics currently identifies by default, simply load into your browser http://www.google-analytics.com/urchin.js. In this file you will see the section commented as:

//-- Auto/Organic Sources and Keywords

This section is where the organic search engines are defined that once captured by the Google Analytics Tracking Code (GATC) will be reported in the reports interface. By looking at this section, you will notice that the current number of organic search engines detected by default is 28 i.e. _uOsr[0] to _uOsr[27].

Of course Google recognises that there are a great deal more search engines in the world – language and regional specific as well as niche search engines such as price comparison and vertical portals. It is therefore possible to modify and append the array of recognised search engines and there are two methods.

1. The standard method of adding additional search engines to GA
Add the following code to your page GATC:

_uOsr[28]="search_engine_name";
_uOkw[28]="query_variable"; 

The value for _uOsr is the domain name (sub-domain or part of the domain to match) of the search engine and the value for _uOkw is the query variable which stores the keyword (replace search_engine_name and query_variable in the example above).

The number in square brackets should start at 28, or one more than what ever the last number is if the number has been updated, and increase in increments of 1 for each additional search engine added (29, 30, 31, etc).

For example, if someone searched for “motorcycle” and the search result URL is:
http://search.bbc.co.uk/cgi-bin/search/results.pl?q=motorcycle

you would add the following line to your tracking code on your pages:

<script src="http://www.google-analytics.com/urchin.js" type="text/JavaScript"></script>
<script type="text/JavaScript">
     _uacct = "UA-xxxx-x";
     _uOsr[28]="BBC"; _uOkw[28]="q";
     urchinTracker();
</script> 

Another example where this technique is useful is to add price comparison engines as a regular search engine:

<script type="text/JavaScript">
     _uacct = "UA-xxxx-x";
     _uOsr[29]="Kelkoo"; _uOkw[29]="siteSearchQuery";
     urchinTracker();
</script> 

By this method, Kelkoo would be listed in the Search Engine Marketing report along side other search engines. That is useful in itself, but what provides more insight is the corresponding Kelkoo search terms used by visitors will also be listed in the Keywords report. Without this little hack, Kelkoo would simply be listed as a standard referrer and no search terms would be logged.

Apart from adding additional search engines to the existing list provided by Google Analytics, you could also use this method to create more regional ones of the main players. For example, if you are based in the UK, being able to differentiate google.co.uk from google.com may be of importance. In which case you would add the following to your GATC of each page:

<script type="text/JavaScript">
     _uacct = "UA-xxxx-x";
     _uOsr[0]="google.co.uk"; _uOkw[0]="q";
     urchinTracker();
</script>

Note: When adding regional variations to the search engine list, the order or the _uOsr and _uOkw arrays become important. So google.co.uk must be listed before the “catch-all” match of google. That of course requires the re-numbering of the search engine list array.

2. A more robust method…
An example of a more complete and robust method of adding additional search engines to the list is shown at: www.advanced-web-metrics.com/scripts/custom_se.js. In this case, the list of custom/localised search engines is kept in a separate JavaScript file and referenced in each page within the GATC as follows:

<script src="http://www.google-analytics.com/urchin.js" type="text/JavaScript"></script>
<script src="http://www.mysite.com/custom_se.js" type="text/JavaScript"></script>
// custom_se.js must be called after urchin.js

<script type="text/JavaScript">
	_uacct = "UA-xxxx-x";
	urchinTracker();
</script>

This script overwrites the default search engine array of Google Analytics and uses the array length (_uOsr.length) to increment its index so that re-numbering is not required when adding new entries. A sample of the code is provided below:

var _uOsr=new Array();
var _uOkw=new Array();

// Google EMEA Domains
_uOsr[_uOsr.length]="google.co.uk";	_uOkw[_uOsr.length]="q";
_uOsr[_uOsr.length]="google.es";	_uOkw[_uOsr.length]="q";
_uOsr[_uOsr.length]="google.pt";	_uOkw[_uOsr.length]="q";
_uOsr[_uOsr.length]="google.it";	_uOkw[_uOsr.length]="q";

… etc.

So by this method, you simply maintain a separate file for your list of search engines and you don’t need to worry about renumbering each time you update/append. Feel free to copy and use the one listed: http://www.advanced-web-metrics.com/scripts/custom_se.js.

Many thanks for the guys at GA-Experts.co.uk for the help with building and testing the custom_se.js file.

Did you find this tip useful? All tips are being grouped under the category GA Hacks. Please provide your feedback with a comment.

Tracking banners and other outgoing links - Automatically

Categories: GA Hacks, GA specific Your Comments 17 »

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...

ga-hacks.gifA word of caution - This is a tech tip and requires you to have a knowledge of html and javascript to implement and use it…

Your site may offer a visitor a link to click through to an external web site such a subsidiary, an affiliate, advertiser or an trade organisation. For Google Analytics, a visit leaving your web site requires an edit to the page in order to track it. This is achieved by modifying your outbound links to call urchinTracker and is extremely easy to do. However, what if your web site has hundreds of separate outgoing links that are constantly evolving and being appended to? Clearly manually modifying each link becomes laborious and inefficient. To overcome this you can apply the example JavaScript code below to your web site:

<script type="text/JavaScript">
// Only links written to the page (already in the DOM) will be tagged
// Script can be called multiple times

function addExtLinkerEvents() {
var as = document.getElementsByTagName("a");
var extTrack = ["mysite.com"];
// replace mysite.com with your web site domain

for(var i=0; i<as.length; i++) {
	var tmp = as[i].getAttribute("onclick");
	// Track links off site – i.e. no GATC
	if (tmp != null && tmp.indexOf('urchinTracker') > -1) continue;
	for (var j=0; j<extTrack.length; j++) {
		if (as[i].href.indexOf(extTrack[j]) == -1) {
			var splitResult = as[i].href.split("//");
			as[i].setAttribute("onclick",((tmp != null) ? tmp : "") +
			  "urchinTracker('/ext/" + splitResult[1]+ "');");
			// the above must be on one line - from as[i]... to ");
			break;
		}
	}
}
}
addExtLinkerEvents()
</script>

The script works by looking for links within the browser’s Document Object Model (DOM) that do not match the domain value given in the variable array extTrack. If the link does not match extTrack then it is considered an external link and so is modified to include the urchinTracker call. By this method, all external links will show in the Google Analytics reports as:

/ext/the-url-that-is-clicked-on

Where the-url-that-is-on clicked on is minus ‘http://’. You can modify the JavaScript to adjust the path as required,

Tip: The position of this code within your page is important. The code must be placed after your call to the Google Analytics Tracking Code (GATC). Alternatively place the addExtLinkerEvents() call in an onLoad event handler and host the provided JavaScript in a separate file. As an example I show this below, assuming the javascript is hosted in a file called trackExternal.js, as follows:

<script src="http://www.google-analytics.com/urchin.js" type="text/JavaScript">
</script>
<script src="/trackExternal.js" type="text/JavaScript"></script>
<script type="text/JavaScript">
	_uacct = "UA-XXXXX-Y";
	urchinTracker();
</script>

<body onLoad=”addExtLinkerEvents()”>
	...your remaining web page content...
</body>

A note on performance: Each time your page loads, this script will go through all links referenced on the page to see if it is external. Clearly the more links on your page, the harder the script must work. As long as the number of links on each page number in the hundreds and not thousands, performance should not be a problem.

Also note that for pages with a large number of links, it is possible that visitors will click on an external link before the script has modified it. The result is that click through will not be tracked by Google Analytics which is an accuracy consideration that effects all web analytics vendors.

Did you find this tip useful? I am considering writing more of these tech tips if you feel they are useful. Please provide your feedback with a comment.

Copyright Advanced Web Metrics by Brian Clifton | Privacy | Contact:
Post Feed Comments Feed Log in