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]
 

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

Message Sent

Thank you for registering.

Cardinal Path hosted a live session to connect with you and answer all your questions on Google Analytics.
Get all the expertise and none of the consultancy fees in this not-to-be-missed, rapid-fire virtual event.

Thank you for submitting the form.

Thank you for submitting the form.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you.

Click here to download access the tool.

Message Sent

Thank you for registering.

Message Sent

Thank you.

Message Sent

Thank you.

Message Sent

Thank you

Message Sent

Thank you

Message Sent

Thank you.

Message Sent

Thank you

Message Sent

Thank you.

Message Sent

Success!
Your message was received.

Thank you.

Thank you for registering.

Cardinal Path is continuing with its series of free training. Next we are conducting training on Google Data Studio. Check it out here.

Message Sent

Thank you for registering.

Thank you for your submission.

Your request has been submitted and a rep will reach out to you shortly.

Message Sent

Thank you for your interest.

Thank you for registering.

You should receive a confirmation email from GoToWebinar with your unique webinar login information. If you do not receive this email or have trouble logging in to the event, please email asmaa.mourad@cardinalpath.com.

Thank you for subscribing!

You're now looped into the world's largest GMP resource hub!

Thank you for your submission.

Thank you for your submission.

Thank you for your submission.

Thank you for your submission.

Message Sent

Thank you for registering.

Message Sent

Thank you for your submission.

Thank you for your submission.

Message Sent

Thank you for registering.

Thank you for registering.​

Paid media spend by Government websites increased a whopping 139% YoY in 2020.

2020 Online Behavior Live Dashboard

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

2020 Online Behavior Live Dashboard

Thank you for your submission.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Success! Thank you
for reaching out.