Track SEO rankings with Google Analytics

September 30th, 2010 – 59 Comments

Mike Pantoliano of Distilled had a good post over at SEOmoz about how to turn Google Analytics into your own rank tracker. We have been playing with the cd= parameter at Yoast before to track SEO rankings, as Mike also notes, and his improvement on the idea is a welcome one.

In the comments, my buddy Richard Baxter immediately suggested this would be a good addition to the Google Analytics for WordPress plugin. The “problem” was/is that Mike’s example was in PHP, doing the rank recognition server side. This won’t work, as we all use caching plugins or the caching options of our hosting provider to cache our pages, right? And then, we’d be storing the same rank over and over again, or no rank at all, or, well you get it, mayhem ensues.

So, I had to come up with a javascript version that does the same. Luckily, that’s not too hard. You simply need to do two things: check that the referrer is coming from google and that it contains the cd= parameter. If it does, get the value out of it and store it in a custom variable. Here’s an example using the asynchronous tracking code, which you could, if you wanted to, drop into your site’s footer already if you have asynchronous tracking running:

if ([\?|&]cd=/) != -1
  &&\./) != -1) {
	var rank=document.referrer.match(/[&|\?]cd=([\d]+)/);

This is now running on my blog as a test, when it works as I expected I’ll add it to the plugin. The code sample above uses custom variable “index” or “slot” 1 to store the ranking. I’m using another “slot” one here on the blog, but my plugin would take care of that. I’d love to hear your feedback and ideas on other stuff to do with this!

59 Responses to Track SEO rankings with Google Analytics

  1. Jody May-Chang
    By Jody May-Chang on 19 October, 2010

    I am having the same issue as Todd Donald with Google Analytics not showing up in my WP 3.0.1 dashboard. Never used it before so I have nothing to compare it to but it looks cool and I would like very much to use it.

  2. Todd Donald
    By Todd Donald on 16 October, 2010

    I installed the Plugin on client site running WP V 2.9.2 and it works fine, but when I tried to install it on my site which is V 3. whatever (the newest WP version), it does not appear on the dashboard. I have tried to deactivate and reactivate, I’ve tried refreshing and logging off, etc and yet it still will not show up. It does not show up as “Stats” in the view options checkbox area either. The plugin works great with WP V 2.9.2, so I would love to get this Plugin working on my site which is running the newer 3. version of WordPress, any suggestions?

  3. Rob
    By Rob on 15 October, 2010

    Great stuff Joost. Thanks for all the info and plugins. I have more and more clients asking for guidance on SEO and I am pointing them in the direction of your great film presentation, which I found really useful.

    I still have alot of changes to make to some of our sites, and I’ll be giving this plugin a try once I have implemented all the other great advice I have gleened from your content.

    If I am honest, I am commenting so I can see the redirect functionality after a post that you mention in the film!

  4. Alex
    By Alex on 14 October, 2010

    I just doanloaded and installed your WordPress Analytics and I have to say thank you, this is really awesome. I love the added features for tracking. I can’t wait to explore some of your pther plugins.

  5. Michael
    By Michael on 13 October, 2010

    this is great! But tell me how would you recommend proceeding if you have a large list of keywords that you want to track – is there an automated way to load up the keywords?

    would appreciate suggestions on this!

    Thanks Yoast!!

  6. Ann Donnelly
    By Ann Donnelly on 12 October, 2010

    I need to pay more attention to your feed! I was just thinking if I could get someone to integrate this sort of thing with data from Google Analytics, it’d make my life 100% easier.

    OK – do you know anything that will integrate this with Webmaster Tools and Adwords Keyword Tool data and I’d be working half days!

    (Is your Amazon wishlist up to date?)

  7. Miguel
    By Miguel on 7 October, 2010

    Is this functionality added to the Google Analytics for WordPress plugin yet? How can I get notified once it is?


  8. amos
    By amos on 7 October, 2010

    Tried it for past 2 days, doesn’t seem to work

    • Joost de Valk
      By Joost de Valk on 8 October, 2010

      Hmm it works for me amos :)

  9. Claudia
    By Claudia on 7 October, 2010

    Nice post, I am going to try it. Thanks Yoast!!

  10. Brad Davis
    By Brad Davis on 7 October, 2010

    That will be a great addition to the plugin, look forward to seeing it action!

  11. frank
    By frank on 6 October, 2010

    Great! Will try it soon!

  12. Mikal
    By Mikal on 6 October, 2010

    Super great. Thanks for the tip. I always loves your tweaks and tips for SEO.

  13. Martijn Beijk
    By Martijn Beijk on 6 October, 2010

    Hé Joost,

    As you know the cd number will be a bit flawed by universal search listings. a way to differentiate between these is to look out for sa=X or sa=T (x being extended, t being traditional/text). So when you see a ranking position 13 in use with sa=X it might be a very common issue that in fact the ranking position is still on page 1 instead of 2 which most people might think. Maybe it can be of use for the people storing it in the custom variable.

  14. Andy Max Jensen
    By Andy Max Jensen on 6 October, 2010

    Great! As usual!

    :-) Andy

  15. arnout h
    By arnout h on 6 October, 2010

    Nice one Joost, thanks!

  16. Steve
    By Steve on 6 October, 2010

    Wow. Given that I’m obsessed with Google Analytics, I’m looking forward to the update. Keep on rockin’ Joost.

  17. Morten
    By Morten on 6 October, 2010

    Hi Yoast,

    This is really a cool feature :) I’m looking forward to see it live!

    If it is possible to see the highest rankings in the wordpress admin panel, that would be a cool feature.

  18. Stijn
    By Stijn on 6 October, 2010

    Do you expect working this would be easier than using the search query report in Google Webmaster Tools?

    • Joost de Valk
      By Joost de Valk on 8 October, 2010

      It’s mostly just another validation of any data GWT might show :)

  19. Iwan Uswak
    By Iwan Uswak on 6 October, 2010

    You should know that if the clicker has “Starred results for [keyword]” sitting before the organic results, they will influence the number/rank in the cd= parameter.

  20. Udo Kempen
    By Udo Kempen on 6 October, 2010

    Sounds great, looking forward to compare the positions reported in GA with the new enhance rankings stats just added to the Google Webmaster Tools

  21. Karl
    By Karl on 6 October, 2010

    Nice work, cant wait for the plugin! When do you think you’ll release the update?

  22. Roger
    By Roger on 6 October, 2010

    I can’t wait until it gets pushed into the plugin. Thanks for the innovation.

  23. Roie
    By Roie on 5 October, 2010

    Bruno Hug seems to have the easier solution and this way the reports can be viewed directly within regular reports instead of the custom variables report

  24. Bruno Hug
    By Bruno Hug on 5 October, 2010


    I’m not sure to have perfectly understood you code, if so…

    What I get with the filter is things like : Keyword (Position).
    It seems to be the same as your code, or am I misunderstanding something?

  25. Home of cats
    By Home of cats on 5 October, 2010

    Will try it soon! :)

  26. Viktor
    By Viktor on 5 October, 2010

    as i understood, you are only tracking SERP Position from a clicked Person, not from Positions were “impressed” (from just impressions), isn’t it??

  27. Andy
    By Andy on 5 October, 2010

    But in this way you will track only clicks. How will you track the positions, that are deeper in SERP? (like on page 5 f.e.)

    • Chris Abernethy
      By Chris Abernethy on 5 October, 2010
    • Chris Abernethy
      By Chris Abernethy on 5 October, 2010

      This method only tracks position as revealed by the referer[sic] string from google when it contains the “cd” parameter, so it only works when a user clicks on a search result listing. If you want to track your pages search result position for particular keywords in general without having to wait for a user to actually click on it, you would need to use software designed for that purpose, e.g., a scraper.

      A google search for “track keyword ranking” will direct you to several options.

  28. Bruno Hug
    By Bruno Hug on 5 October, 2010

    I’ve used another technic to find the rank in Google for a visitor, by creating a filter.

    Choose Custom filter -> advanced, then:
    Field A : Campaign term : (.*)
    Field B : User defined : (\?|&)(cd)=([0-9]*)&
    Output to : Campaign term : $A1 ($B3)

    Field A required : Yes
    Filed B required : No
    Override Output field : Yes
    Case sensitive : No

    Let me know if it does not the same thing.

  29. Frank
    By Frank on 5 October, 2010

    So does this trick only work with google instant? Other ‘normal’ serps don’t pass the cd parameter do they?

    • Chris Abernethy
      By Chris Abernethy on 5 October, 2010

      I’ve seen an increase in the number of google referrals that include the “cd” parameter lately, but it was definitely in use before google rolled out “instant”.

  30. Massoutreach
    By Massoutreach on 5 October, 2010

    Plugin looks great. I think it will gonna help a lot in SEO.

  31. Eliko
    By Eliko on 4 October, 2010

    Thanks Joost. Would you mind helping me configuring my analytics advanced segment for this?
    I used your code (and Andre Scholten’s from seomoz), and setup a new GA Advanced Segment with “Custom Variable (Key 1)” / “Matches Exactly” / “Google_Ranking” (I also changed the custom-var-key-string in my website as you may see in the homepage source code).
    When I test the segment (after using the code for about 32 hours in my website), GA returns “0 visitors” :(
    Do you know why?

  32. Chris Abernethy
    By Chris Abernethy on 3 October, 2010

    You can also do this completely in GA with a custom profile and Filters, check this out for info on how to do it:

  33. Joan Stewart
    By Joan Stewart on 2 October, 2010

    Thanks Joost,

    I will wait for the plugin, it sounds like this could really come in useful.

    As always your tips are always looked forward to and one of the first emails I always open.

  34. master GO master
    By master GO master on 2 October, 2010

    I will be waiting for great results and you will give me a Track SEO rankings with Google Analytics plugin. thanks for information

  35. Oyunyurdum
    By Oyunyurdum on 1 October, 2010

    Well, thank you. Has been a great application.

  36. TrafficColeman
    By TrafficColeman on 1 October, 2010 are always looking to help the little guy.

    “TrafficColeman “Signing Off”

  37. Kenneth Berkley
    By Kenneth Berkley on 1 October, 2010

    Thanks Yoast for sharing this us i will test this immediately it sound great and very useful.

  38. Gregorio Luis!Como Ganhar Dinheiro
    By Gregorio Luis!Como Ganhar Dinheiro on 1 October, 2010

    I am looking forward to your plugin update,can’t wait,will test this immediately

  39. Grün Weiss
    By Grün Weiss on 1 October, 2010

    nice artcile i test it and i wil see thats works

  40. lastactionseo
    By lastactionseo on 1 October, 2010

    I will test this immediately it sound great. Great Job!

  41. oblomov
    By oblomov on 1 October, 2010

    This would be awsome. But how about extracting the tld of the google referall and also store this in a custom variable?

    • Joost de Valk
      By Joost de Valk on 1 October, 2010

      great suggestion, let me ponder how to do that for a bit ;)

  42. Dan
    By Dan on 1 October, 2010

    Thanks Yoast can’t wait.

  43. Allan Collins
    By Allan Collins on 30 September, 2010

    WOW! I am looking forward to your plugin update. Tracking rankings in Analytics will save me a lot of time and will allow me to analyze the amount of traffic for the given ranking.

  44. Billy Girlardo
    By Billy Girlardo on 30 September, 2010

    I haven’t updated W3TC since 8.5.2 (or OpenID since last – 3.3.2), because they give me the WSOD (whoa, I just ran outside your textarea via FF 3.6.10), but I see they aren’t yours anyway :-)

    Looking forward to the update for your GA plugin, and thanks for everything!

  45. John Cisar
    By John Cisar on 30 September, 2010

    See I knew I’d learn something uber cool GA related today ! Thank’s for sharing.

  46. Luc De Brouwer
    By Luc De Brouwer on 30 September, 2010

    Seems like a solid solution!

  47. Mayur Somani (AgentWP)
    By Mayur Somani (AgentWP) on 30 September, 2010

    Looking forward to the updated plugin. I’ll also start testing the script until then. Thanks.

  48. Navin Poeran
    By Navin Poeran on 30 September, 2010


    Will look forward to your updates, regarding this post :)

Check out our must read articles about Analytics