Image search keyword tracking with the new Google Analytics

While rewriting my Google Analytics for WordPress plugin, I found out that in the new Google Analytics tracking with ga.js, tracking the keyword people used in image search is a lot easier than it was in the old version, as changing the referrer info doesn’t involve rewriting an entire function anymore. You can simply change the referrer after the data has been initialized, so this small bit of code will suffice:

// Change the referrer to have the right keyword in it
if ( != -1 && != -1) {
regex = new RegExp("[^\/]+).*&prev=([^&]+)");
var match = regex.exec(; = "" + match[1] + unescape(match[2]);

For this to work you do need to add, and possibly local versions as a search engine. To do that read my small info on how to add extra search engines in the new Google Analytics.

So what I will do in my plugin, is check the referrer in PHP, and if that has in it, I’ll add in the middle three lines of code (startting with the regex). That’s an awful lot easier than the old version, isn’t it?

Tags: , runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

16 Responses

  1. LemonzingerBy Lemonzinger on 18 December, 2007

    Looking forward to the updated plugin!
    Using the current version on a couple of my blogs, keep up the great work!
    Any scheduled release date?


  2. Joost de ValkBy Joost de Valk on 18 December, 2007

    Any day now :)

  3. RhodaBy Rhoda on 30 December, 2007

    I was excited to read this post about doing image tracking in the new Google Analytics. However, I have a few questions. Here is what I am currently using:

    _uacct = “UA-xxxxx-x”;

    I have already converted my custom_se.js file to the new format. However, I’m wondering if the following two files need to be converted for the new format:

    I know you’ve worked on the update for the imagesearch but what about the others? Are they ok to use as is?

    Here is how I think this should be laid out for the new Google Analytics:

    var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
    document.write(“\\” );

    var pageTracker = _gat._getTracker(“UA-xxxxxx-x”);
    // Change the referrer to have the right keyword in it
    if ( != -1 && != -1) {
    regex = new RegExp(“[^\/]+).*&prev=([^&]+)”);
    var match = regex.exec(; = “” + match[1] + unescape(match[2]);}
    var exitTracker = pageTracker._createEventTracker(“Outbound clicks”);

    I have a few outgoing links which I have coded already. Do I need to remove the coding for the page tracker to work?

    I noticed another difference from what you have on your web pages:

    var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
    document.write(“\\” );

    from what Google offers:

    var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
    document.write(unescape(“%3Cscript src=’” + gaJsHost + “’ type=’text/javascript’%3E%3C/script%3E”));

    I’m sorry to have so many questions. It’s those little things that make me stop and think before doing. Tracking more search engines and the images searches has really been a learning experience. So I would hate to move to the new way before making sure everything will work the same.

    Thanks so very much for all you work on this.

    Rhoda Schueller

  4. RhodaBy Rhoda on 30 December, 2007

    The script information doesn’t copy so I removed all of that and left the three files I use. (already converted ready to use) (Is this one ok to use as is?) (is this one ready to use as is?)


  5. Joost de ValkBy Joost de Valk on 30 December, 2007

    @Rhoda: those are all old and won’t work… The first and second can be found in the right version within my WordPress plugin, the latter you should ask Brian Clifton about, who wrote that script… I can drop him a line if you want me to.

  6. GerhardBy Gerhard on 3 January, 2008

    Hi Joost,

    I have played with your code a bit but I’m a little puzzled with this variable Where is the .qa set? Will I have to initialize this first or is this set automatically?

    Kind regards

  7. rachelBy rachel on 1 March, 2008

    hi, thanks for the helpful posts – i get almost half my hits from image searches recently so i am trying to add this to my GA stats to see what the keywords are. i have managed to add the new search engines, but when attempting to add this bit of code, the following error comes up :
    “Your template could not be parsed as it is not well-formed. Please make sure that all XML elements are closed properly.
    XML error message: The entity name must immediately follow the ‘&’ in the entity reference.”
    any advice? thanks :)

  8. MicroAngeloBy MicroAngelo on 6 May, 2008

    Hi Joost,

    Great info! You mention that you may need to add all the local engines in as search engines, but won’t this just cover them all?


    Coupled with your rewrite, I think this will do it, no? If not, could you perhaps give a demo of how you would add in?



  9. SEO GTABy SEO GTA on 1 November, 2008

    I am not sure if that is the complete code or I am missing anything here, but you are trying to apply a search function to this variable:

    Fine but this variable is not defined anywhere yet, the other question is where is the code that tells GA to use as a referrer instead of the original referrer

    I spent hours trying this code and other code but didn’t work, the only reliable way that worked for me is reformating the referrer URL and insert it into a user defined field which is not perfect

  10. Tomas KaplerBy Tomas Kapler on 18 March, 2009

    don’t you know if it is possible to measure the search engines with rewriten url, like
    I am unable to find it

  11. Ion de la PaclisaBy Ion de la Paclisa on 9 July, 2009

    I’m using the wordpress plugin..
    I really don’t think it’s working. I don’t know to much about Anallytics, but after waisting 3 hours to check and recheck that everything was done as you said it seems to me that after 3 days nothing new appeared in my analytics. I’ve created a new profile and there is data until now …
    I do not know how did you manage to make it work, but with all that links in your “tutorial” post it’s hard for some one to read it… not saying to make it work…

    I know that SEO means links but not like this… I thought that that post was for humans not SE.

  12. AivarBy Aivar on 2 August, 2009

    Your script is not very good, because every time Google updates its scripts all variables get new names. For example variable qa ( is today yB.

    I wrote better and more reliable script for fixing image search refferer:

    var pageTracker = _gat._getTracker("YOUR TRACKER ID");

    var refferer = document.referrer;
    if ( != -1 && != -1) {
    var regex = new RegExp("[^\/]+).*&prev=([^&]+)");
    var match = regex.exec(refferer);
    pageTracker._setReferrerOverride("" + match[1] + unescape(match[2]));


    • Joost de ValkBy Joost de Valk on 6 August, 2009

      thx a ton for that, I’ll have a look at incorporating that into my GA for WordPress plugin :)

  13. JACKBy JACK on 24 October, 2009

    Is the code posted by Aivar better? The change that I notice is that it uses refferer instead of ref.

    I have tried to use the code that you posted in the google hep docs, but it does not seem to work.

    Do you need to do anything else but put the code in the analytics file? I am confused.

    Thank you for the help.