« »
Feb 08
2010

Do you want to customize your user experience based on how many times visitors visited your site or how many pages they viewed?  Maybe you are designing a web page where the layout of which depends on the number of visits to the site or depends on whether the page was a landing page or a category page.

How about if you want to display a message on the landing page to all visitors who visited your site 5 times? Or you want to tag visits with custom variable after certain number of pageviews.

In this post I will walk you through a method that allows you to access the visit counter and the session pageview counter using Google Analytics. What you do with these two variables, I will leave it for you to decide based on your creativity and business requirements.

Google Analytics Visit Counter and Pageview Counter Technique

As you know, websites that have Google Analytics installed in them issue first-party persistent cookies that allow the site to uniquely identify visitors.
In this post we will focus on only two cookies utma and utmb and how can we get the visit count and the pageview count from them:

The utma cookie is also known as the visitor identifier. The last number in the cookie string is the visit counter, which increments by one every time the site visitor starts a new session. In the example below, the visitor visited the site 5 times.

How to extract the visit count?

I’m going to use JavaScript code to pull the “visit count” value off of the _utma cookie.

function get_visit_count(str)
{
var i, vc='-';
if (str != '-') {
   i = str.lastIndexOf(".");
   i++;
   vc = str.substring(i);
   }
return vc;
}

This script defines a function (get_visit_count) that receives the utma string and returns the visit count. If utma comes with no value due to the failure of executing the GATC for example, the function will return the ‘-’ character.

For example:
Function Input: 7113510.1552602301.1265052146.1265052146.1265060431.5
Function Output: 5

The utmb cookie is also known as the session identifier. The number that comes right after the domain hash is the pageview counter which increments by one every time the visitor refreshes the current page or views a new page. In the example below, 8 pages were viewed during that particular session.

How to extract the pageview count?

Similar to what we did in the previous section, this time I’m going to use JavaScript code to pull the “pageview count” value off of the _utmb cookie.

function get_pageview_count(utmb,utmc)
{
var i, j, pc='-';
if(utmb != '-' && utmc != '-'){
   utmc=utmc+'.';
   i=utmc.length;
   j=utmb.indexOf(".", i);
   pc=utmb.substring(i,j);
   }
return pc;
}

This script defines a function (get_pageview_count) that receives the utmb and utmc strings and returns the pageview count. If utmb comes with no value, the function will return the ‘-’ character

For example:
Function Input: 7113510.8.10.1265060431
Function Output: 8

How to implement this method on my site:

  1. Create a folder under your web server root and name it “Scripts”
  2. Download the “SessionTracker.js” by clicking here
  3. Add the “SessionTracker.js” file to the “Scripts” folder
  4. Add the following code right after the Google Analytics Tracking Code on every page you wish to track the session information [visit count and pageview count]
<script type="text/javascript" src=".../Scripts/SessionTracker.js"></script>

<script type="text/javascript">
alert("visit count= " + visit_count);
alert("pageview count= " + pageview_count);
</script>

Now that we have the visit count and the pageview count for every page in the site, we can use them for all kind of cool things. Share with us your case studies and nice ideas :)

Related Posts

Tags: , ,

16 Responses to “Customize Your User Experience with Google Analytics”

  1. Kim Nichols says:

    Excellent information. Thanks for the great explanation.

  2. Dave Hassall says:

    Feras

    Awesome – I’m just planning the build of a website that will encourage people to recommend a friend and get a great reward. Having this facility on the “Recommend A Friend” page will be amazing

    I’m guessing that customizing messages is really easy, e.g.

    “You’ve recommended X friends – recommend 10 and recieve our special bonus gift”

    Thanks for the info

    Dave

  3. Thank you Kim, I am glad you liked it

  4. Thanks Dave, let us know how it works out for you

  5. Nikki Rae says:

    This is great – can’t wait to try it!
    Nikki Rae

  6. Thanks Nikki, Please try it and share with us your findings… I am very interested to see what you build around this :)

  7. I have been using the same technique to force email capture before showing one block on my landing pages for visitors who came to the site 2 times only! In addition, I use _utmZ cookie to check visitor source and show the right telephone number which allows me to track calls and attrubute leads to the right marketing channel.

    Polly

  8. Rehan Asif says:

    Hi Polly,

    That is an excellent application of this technique!
    It certainly keeps the number of landing pages/form pages under control. :)

  9. I love it Polly! I am very interested to learn about different applications of this technique…

    Thanks for sharing,

  10. hallian says:

    Hi, i have implemented the code. Now plz tell me how can i get to display the number of pageviews and visits on the page.

  11. hallian says:

    Here is my test page code. Donno why its not working.

    test page

    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”));

    try {
    var pageTracker = _gat._getTracker(“UA-8646907-4″);
    pageTracker._trackPageview();
    } catch(err) {}

    alert(“visit count= ” + visit_count);
    alert(“pageview count= ” + pageview_count);

  12. hallian says:

    Sorry, the code did not come right in the previous post. You can view the code at the following address. Thanks.

    http://www.economistan.com/thepageviewtest.html

  13. Hi Hallian,

    Update the code to include a reference to the JS file
    script type=”text/javascript” src=”http://www.economistan.com/Scripts/SessionTracker.js”>

    Good Luck,

  14. hallian says:

    It worked. Thanks a lot.

  15. hallian says:

    Well the counter does not seem to be unique to each page. It just counts all the page visits on a given site i believe. Plus how can i have pageviews unique to each page and display total number of pageviews whether unique or not rather than the visits.

  16. hallian says:

    ok i get it. This code would allow the visitors to see how many times they have visited our web page. Great! Is there any way we can use Google Analytics for finding out how many times a particular page has been viewed just like in blogs. Thanks!

Leave a Reply