Steven Mileham – General Geekery

October 23, 2009

Google Analytics – Tracking page categories

Filed under: Technology — Tags: , , — smileham @ 2:30 pm

I’ve recently been looking at how I can use Google Analytics to track website usage of content based on the category it has been assigned.  We are using META tags in the page headings to store the category name and title of the content.

Google have just rolled out a bunch of excessively handy features allowing custom variables in their most recent update to the analytics package, unfortunetly my Analytics account hasn’t yet been updated to include them.  As a fall back I’ve used the Event Tracking functionality instead.

At the bottom of each page which I want to track the category of, added the following code;

  1.  
  2. <script type="text/javascript">
  3.   var pageTracker = _gat._getTracker("<%= googleAnalyticsId %>");
  4.  
  5.   function getMeta(name) {
  6.     var metas = document.getElementsByTagName(‘META’);
  7.     for (var i = 0; i < metas.length; i++){
  8.       if (metas[i].getAttribute(‘NAME’) == name){
  9.         return metas[i].getAttribute(‘CONTENT’);
  10.       }
  11.     }
  12.     return "null";
  13.   }
  14.  
  15.   try {
  16.     var cat = getMeta("CATEGORY");
  17.     var title = getMeta("ARTICLE_NAME");
  18.  
  19.     if(cat != "null" &amp;&amp; title != "null"){
  20.       pageTracker._trackEvent(‘Category’,cat,title);
  21.     }
  22.   }catch(ex){
  23.     /* do nothing */
  24.   }
  25.   pageTracker._trackPageview();
  26. </script>
  27.  

Using this codes means that I can put these meta tags into the headings of any page;

  1.  
  2. <meta name="CATEGORY" content="Analytics" />
  3. <meta name="ARTICLE_NAME" content="Custom Variables" />
  4.  

And Google Analytics will track the my each of my articles based on the category they have been tagged with.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment


Powered by WordPress