Image Search Keyword Tracking With Google Analytics

Written by  March 25, 2011

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 ( != -1 && != -1) {
var regex = new RegExp("[^\/]+).*&prev=([^&]+)");
var match = regex.exec(ref);
_gaq.push(['_setReferrerOverride', ''+match[1]+unescape(match[2])]);


(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);

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 + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxxxx-x");
var ref = document.referrer;
if ( != -1 && != -1) {
var regex = new RegExp("[^\/]+).*&prev=([^&]+)");
var match = regex.exec(ref);
pageTracker._addOrganic(""+ match[1],"q");
pageTracker._setReferrerOverride("" + match[1] + unescape(match[2]));


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.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related articles other people have read:


5 Responses to “Image Search Keyword Tracking With Google Analytics”

  1. Big_Berny on April 25th, 2011 6:37 am

    Thanks a lot for that script! Works great.

    But unfortunately there’s a “bug”: Some image searches won’t be tracked correctly and still show as referred by Thats because the domain isn’t always* . So a better way to detect image serches is looking for imgres or imglanding in the url. Like stated here:

    Can your script be changed modified to work with the other detection?

  2. Eric Leuenberger on April 25th, 2011 10:22 pm

    Thanks for the info there. Yes, my script can be changed to accomodate. No new programmimg required. It does this out of the box if you use the ‘custom’ code section in the admin.

    Just include the new tracking criteria as part of your custom code and save. Should work just fine.

  3. Big_Berny on April 25th, 2011 6:38 am

    And a last thing: Is it possible to tell Google Analytics what the filename of the selected image was? That would be great as we have slideshows with multiple similar images on our page.

  4. Eric Leuenberger on April 25th, 2011 10:31 pm

    This I am not sure of. I would guess you might be able to accomplish it with some added triggers attached to each image (to indicate what the image is.)

  5. Big_Berny on April 26th, 2011 5:19 am

    Ok, I think I found a solution:
    var ref = document.referrer;
    if (|www)\.google\.([^\/]+)\/(images|imghp|imgres|imglanding)/) != -1 && != -1) {
    var regex = new RegExp(“google\.([^\/]+)/.*&prev=([^&]+)&”),
    var match = regex.exec(ref);

    _gaq.push([‘_setReferrerOverride’, ‘’+match[1]+unescape(match[2])]);

    Now the only thing I’m still looking for is tracking of the image file name which has been clicked on. Any idea?

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!