Dec 15 2009

Google Analytics: How to Track Image Referrals

*The guest tip was provided by Gaurav Sharma*

Google Analytics classifies traffic from Google Images as referral traffic. Websites which get a lot of traffic from Google Images are not able to analyze the keywords they get the traffic from. There is a way to force GA to treat Google Images as a search engine. Below is how GA code will look like:

<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">
try {
var pageTracker = _gat._getTracker("YOUR-GA-ID");
var refferer = document.referrer;
if (refferer.search(/images.google/) != -1 && refferer.search(/prev/) != -1) {
var regex = new RegExp("images.google.([^\/]+).*&prev=([^&]+)");
var match = regex.exec(refferer);
pageTracker._setReferrerOverride("http://images.google." + match[1] + unescape(match[2]));
}
pageTracker._trackPageview();
} catch(err) {}
</script>


The script by Yoast

29 Responses to “Google Analytics: How to Track Image Referrals”

  1. RT @mercylivi: Google Analytics: How to Track Image Referrals http://dailyseotip.com/google-analytics-how-to-track-image-referrals/458/

  2. RT: @mercylivi: Google Analytics: How to Track Image Referrals http://dailyseotip.com/google-analytics-how-to-track-image-referrals/458/

  3. RT @mercylivi: Google Analytics: How to Track Image Referrals http://dailyseotip.com/google-analytics-how-to-track-image-referrals/458/

  4. Google Analytics: How to Track Image Referrals http://dailyseotip.com/google-analytics-how-to-track-image-referrals/458/

  5. steveplunkett says:

    good code.. thanks

  6. chan says:

    it’s means the images also help to improve the ranking ?

  7. Kurt says:

    That, or get Mint Analytics – since it already does this as a core function. I’m glad someone figured it out for GOOG users.

  8. kumaresan says:

    Ya its good news, Now we can optimize a page with images too..

  9. Tina says:

    How will the result look like in Google Analytics?
    Can you post a screen shot?

  10. Tad Chef says:

    While I appreciate that there is a way to track image searches in GA this code looks quite big and messy. I can’t really imagine putting this on every single page of mine.
    I still use the one line GA inclusion where I can.

  11. Dipali says:

    Hi Gaurav,

    Thanks for providing GA code for Tracking image referral. but it could be more better if it provided with some snap shot of result after implementation of this code. I’ll appreciate your efforts.

  12. Gaurav says:

    @Tina & @Dipali – Here is the screenshot on how ‘images.google’ will show up as traffic sources http://farm3.static.flickr.com/2655/4195909530_88db996002.jpg

    Once you click the ‘images.google’ you will see exactly same interface like you see on clicking Google, Yahoo or Bing.

    This will skew your reports – drop in referral traffic and increase in search engine traffic. So, it might be better to implement at the end of a reporting cycle.

    Also, it relies on Google Images’ referral URL. If Google changes that in future, like they are doing for web search, this code will need to be updated.

    @Tad – Yes, the code will slow down things (haven’t benchmarked), but the stats outweigh performance if a website gets a lot of traffic from Google images.

    @Kurt – Didn’t know Mint supported that, good to know.

  13. Formatix says:

    It would have been even nicer if you had highlighted the difference between original code and modified code. I know i’m lazy ;)

  14. Tina says:

    Thanks Gaurav for this image!

  15. film seyret says:

    Thanks. It’s very useful for my image gallery website…

  16. sumit says:

    Great and useful post, thanks for sharing.

  17. Phil says:

    Comments…

    * Google has changed the value of &prev=/ so that it no longer shows a domain name, thus you can removed from expression.
    * RegEx missing a couple of escapes e.g. images\.google\.
    * https: searches will not be matched
    * www.google.com/images or www.google.com/imglanding will not be matched, but can be fixed with this filter e.g…

    ^https?://(images|www)\.google\.(.{2,8})/(images|imghp|imgres|imglanding).*(%3F|%26|%23)q(%3D)(.+)%26

    AND

    ^https?://(images|www)\.google\.(.{2,8})/(images|imghp|imgres|imglanding).*([?&#])q=([^&]+)&

    Updated script…

    try {
    var pageTracker = _gat._getTracker(“YOUR-GA-ID”);
    var ref = document.referrer;
    if (ref.search(/images.google/) != -1 {
    var regex = new RegExp(“images\.google\.([^\/]+)/”); var match = regex.exec(ref);
    pageTracker._setReferrerOverride(”http://images.google.” + match[1] );
    }
    pageTracker._trackPageview();
    } catch(err) {}

    Also here is an alternative version…

    try {
    regex = new RegExp(“^https?://images\.google\.([^/]+)/”);
    var match = regex.exec(document.referrer); if (match != null)
    {_gaq.push(['_addOrganic', 'images.google.'+match[1], ‘q’, true],['_setReferrerOverride','http://images.google.'+match[1] ]);}
    } catch(err) {}

    Thanks

    Phil.

  18. stevo604 says:

    Has anyone tried Phil’s updated tracking code? This seems to be a hot topic on the GA blog but nobody seems to have a solid solution for this…Web Analytics TV sites this blog post so it must be legit…

    Thanks in advance

  19. kaminomoto says:

    wow, thanks for helping about google analytic bro

  20. Planking says:

    Interesting stuff, but I gotta agree that posting this on every page would be a bit much.

  21. regalos originales says:

    Gracias, excelente blog.

  22. Blackfoot says:

    I know no one who wants to go can not. So no one “stay” I did not say. Put inmind what to go with me for a heart

  23. pockie ninja says:

    thanks for this analytic code… I will try this one..

  24. ben 10 says:

    very useful post, Thanks a lot admin..

  25. y8 says:

    I should say the best DAILY SEO TIP.

  26. Furniture Repair says:

    Daha fazla bilgi cevap insanlar varsay?yoruz.
    Onlar?n tavsiyelerini takip etmek thanks admin very good.

  27. diseño zaragoza says:

    Thank you for sharing this info, it is really useful and interesting.

  28. cines madrid says:

    It´s great to learn how we can optimize a page with images too. Thank you indeed.

  29. Y8 says:

    Don’t even know what to say on this topic. So much ideas are mixed in my head after reading it. Very problematic article I think