Measure Keyword Rank in Google Analytics with Custom Script

1 replies
  • SEO
  • |
I found this method in a post by Justin Cutroni, a notable author of many analytics books and I thought the folks here would find it useful. It details a way to measure your website's rank for certain keywords by installing custom code. It requires that you have Google Analytics installed on your site and a little programming knowledge (or at least the knowledge of how to place the code).

This method uses custom code and Google Analytics events to collect and report on keywords that people used to find your site.

Here is code that will look at the referring URL from organic Google search,

Code:
<script type="text/javascript">
if (document.referrer.match(/google\.com/gi) && document.referrer.match(/cd/gi)) {
  var myString = document.referrer;
  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;
  _gaq.push(['_trackEvent', 'RankTracker', keyWord, p, rank, true]);
}
</script>
Note that the above section of code will only pull keywords from referring URL's from Google Organic search.

An explanation of the code

This section of code parses the keyword out,

Code:
var myString = document.referrer;
var r = myString.match(/cd=(.*?)&/);
var rank = parseInt(r[1]);
var kw = myString.match(/q=(.*?)&/);
and this section of code checks to see if the keyword is (not set)or (not provided),

Code:
if (kw[1].length > 0) {
var keyWord = decodeURI(kw[1]);
} else {
keyWord = "(not provided)";
}
And this snippet sends the keyword data to Google Analytics using an event,

Code:
_gaq.push(['_trackEvent', 'RankTracker', keyWord, p, rank, true]);
All of this code goes AFTER your standard analytics tracking code which should be installed in the head of your web pages.

If you are unfamiliar with event tracking you can learn more about it here,

About Events - Analytics Help

Tracking events is pretty simple once you understand how to do it. These are user actions that can be tracked separately and they include things as simple as a click on an external link to downloads or video views. Events are a great way to track extra things on your site that might otherwise have no data collected about them. Using event tracking is a preferred method over tracking virtual pageviews.

When you set up events, each one will have 5 parts.
Category: In the snippet above, the Category is called RankTracker. Note that this can be called anything you want, just be sure and change it in your code snippet before installation. This is how you will identify data in your analytics reports under content -> events -> top events. If you change it, just remember to make it unique and something that will be easy for you to identify later.
  • Action: The action in the code snippet above is KeyWord.
  • Label: In the snippet, this is the "p" in the snippet above and identifies the landing page. Note that this value is optional. You can use it to provide additional information about the event. In this case it may be helpful to know what page a visitor landed on to see what pages are ranking for specific key terms in search. For instance maybe you are optimizing a certain page for a particular keyword. The label will help you identify your progress.
  • Value: In the snippet, this is the SERP rank. The rank of the search engine result will be recorded as the value of the event in your reports.
  • Non-interactive: set this to TRUE

This is a truly innovative technique as it gives you real data about the keywords that are driving people to your site through organic search. One thing you have to remember is that this only works when people are visiting your site through organic search. If you don't receive a lot of organic search traffic, you may not get much use out of the code.

Some important tweaks and notes

Instead of using document.referrer.match(/google\.com/gi) to detect the referring URL you can use document.referrer.match(/google\./gi) to match foreign versions of Google as well as google.com

If you had to install the basic analytics tracking code on all pages of your site because you aren't using a template or any kind of include file, you will have to do the same with the code snippet to track keywords.

This script cannot be loaded using the Google tag manager

You can find the original post with comments here, A New Method to Track Keyword Ranking using Google Analytics

What innovative ways do you use for tracking your website's rank in SERP's?
#analytics #custom #google #google analytics #keyword #measure #rank #script
  • Profile picture of the author yukon
    Banned
    That's interesting, thanks for the link.
    {{ DiscussionBoard.errors[7928026].message }}

Trending Topics