[METHOD] How to Track Keyword Rankings w/Google Analytics

proweb

Regular Member
Joined
Feb 24, 2011
Messages
291
Reaction score
147
Not sure if this has been shared before, but if you use Google Analytics (I know some of you do, many do not) this is an easy way to have Analytics record the rankings of your keywords (a bit better than Piwik does IMO). You can then use the Analytics reports to see the average rankings, # of keywords ranking on top 3, top 10, etc....the list goes on.

Here's the Pros:
- Tells you the ranking of a keyword
- Computes the average ranking of a keyword
- Records the landing page
- Sorts the keywords by country (i.e. google.com, google.co.uk, etc.)
- Copy-n-paste script

Here's the Cons:
- Must use Google analytics
- If someone is logged into a Google account, the keyword will be reported as "(not provided)"
- Only tracks Google rankings

Anyway, here's the script:

Code:
<script type="text/javascript">
if (document.referrer.match(/google\.([a-zA-Z]{2,5})/gi) && document.referrer.match(/cd/gi)) {
  var myString = document.referrer;
  var gglType  = myString.match(/:\/\/(www\.)?(.[^/:]+)/)[2];
  var r        = myString.match(/cd=(.*?)&/);
  var rank     = parseInt(r[1]);
  var kw       = myString.match(/q=(.*?)&/);
  
  if (kw[1].length > 0) {
    var keyWord  = decodeURI(kw[1]);
  } else {
    keyWord = "(not provided)";
  }

  var p        = document.location.pathname;
  var gCat     = (gglType + ' rankings');
  ga('send', 'event', gCat, keyWord, p, rank, {'nonInteraction': 1});
  //_gaq.push(['_trackEvent', gCat, keyWord, p, rank, true]);   //Uncomment if you're using ga.js
}
</script>

Instructions:
1) Copy-n-paste the code above on the pages you want to track.
2) If you're using ga.js analytics, comment out the ga('send') line and uncomment the _gaq.push line. I'm currently using this script for the analytics.js and haven't tested it for ga.js (older version of analytics).

How the script works
1) The javascript gets the referring URL from Google and parses the string to retrieve the keyword, search engine source, and ranking data.
2) The script then calls an analytics event which records the data to your analtyics account.

Source
I got this script from cutroni.com/blog/2013/01/14/a-new-method-to-track-keyword-ranking-using-google-analytics/, but I modified it to record the keyword ranking by Google domain and so it works for analytics.js.

Enjoy
 
Last edited:
Back
Top