Image Search Keyword Tracking With Google Analytics

March 25, 2011 by  
Filed under Website Analytics

I recently had a question from a user of my Simple Google Analytics zen cart module about how to differentiate between searches and keywords that come from Google Image Search vs. those that come from other Google search channels. As of this writing, Google will automatically log referrers from its image search engine; however, it lumps all keywords into one lump source as ‘(not_set’).

To get a more detailed keyword breakdown you need to customize the code you use within your Google Analytics tracking script.

The method I will describe here works for anyone using Google Analytics on their site regardless of how it is implemented.

The first thing you have to do in order to get this to work is to be running either the ga.js or the Asychronous version of the tracking script.

Depending on which version of the tracking script you have chosen you’ll then need to customize it with one of the following snippets, adding it to what you already have. Be aware that the order and placement of this code within the original tracking script is very important.

If you are using the ga.js version of the tracking script you’ll need to add the following lines of code (highlighted in bold) to that.

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
var ref = document.referrer;
if (ref.search(/images.google/) != -1 && ref.search(/prev/) != -1) {
var regex = new RegExp("images.google.([^\/]+).*&prev=([^&]+)");
var match = regex.exec(ref);
_gaq.push(['_clearOrganic']);
_gaq.push(['_addOrganic','images.google.'+match[1],'q']);
_gaq.push(['_setReferrerOverride', 'http://images.google.'+match[1]+unescape(match[2])]);
}

_gaq.push(['_trackPageview']);

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

If you are using the ga.js tracking script your additions would look like this:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxxxx-x");
pageTracker._initData();
var ref = document.referrer;
if (ref.search(/images.google/) != -1 && ref.search(/prev/) != -1) {
var regex = new RegExp("images.google.([^\/]+).*&prev=([^&]+)");
var match = regex.exec(ref);
pageTracker._clearOrganic();
pageTracker._addOrganic("images.google."+ match[1],"q");
pageTracker._setReferrerOverride("http://images.google." + match[1] + unescape(match[2]));
}

pageTracker._trackPageview();
</script>

Doing this should breakout the individual searches by keyword so you have a more complete picture of what is happening with regard to the traffic that comes from Google Image Search.

Google help also offers more information on customizing search engines and referral traffic with your analytics tracking script.

Have you tried this method or have another way of doing it? I’d love to hear it. Post a comment below to join the discussion.