Customize Your User Experience with Google Analytics

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.

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.

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]

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

16 thoughts on “Customize Your User Experience with Google Analytics”

  1. 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


  2. 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.


  3. 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.

  4. 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 + “’ type=’text/javascript’%3E%3C/script%3E”));

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

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

  5. Hi Hallian,

    Update the code to include a reference to the JS file
    script type=”text/javascript” src=””>

    Good Luck,

  6. 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.

  7. 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

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">