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:
- Create a folder under your web server root and name it “Scripts”
- Download the “SessionTracker.js” by clicking here
- Add the “SessionTracker.js” file to the “Scripts” folder
- 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
- A/B & Multivariate Testing w/ Google Website Optimizer
- Conversion Optimization Session @ SMX West