Page Tracking in Google Analytics

This article is in conjunction with the article Introduction to Google Analytics Library – analytics.js’ and ‘Introduction to Google Analytics Commands

Technically speaking, Google Analytics is a JavaScript library called ‘analytics.js’ and Google Analytics commands are used for setting up Google Analytics implementations like ecommerce tracking.

Therefore it is important, that you understand both analytics.js and GA commands before implementing page tracking on your website.

In page tracking, a page represents a web page. The page can be a static or dynamic (dynamically created) web page.

Through page tracking you can measure the number of times a page was shown to your website visitors in GA. This can be done by sending a pageview hit, each time a page is shown.

The command method named ‘send’ is used to send pageview hit to Google Analytics:

For example:

 ga(‘send’, {‘hitType’: ‘pageview’});  // send a page view hit

You can also write this line of code without using hitType field as:

 ga(‘send’, ‘pageview’); // send a page view hit

This line of code is already included in your Google Analytics tracking code:

ga tracking code

However by default, GA tracking code is executed only on a page load. If a page does not load in your web browser, then the tracking code is not executed and hence no pageview data is sent to GA server.

There could be situations, in which you need to send a pageview hit manually because:

#1 The event (user’s interaction/activity) that you want to track does not generate a pageview when it occurs. For example loading of Flash/Ajax content, downloading a file, clicking on an external link etc.

#2 You want to rewrite request URIs for advanced segmentation. For example you may want to rewrite the request URI: /men/shop/sales/shirts to say /men/shop/sales2015/shirts in order to segment the 2015 sales data in GA without actually rewriting the URLs on the website.

For pageview hit, you can set only one field called ‘page’. However you can set many field objects like: ‘title’ and ‘location’.

Syntax:

ga(‘send’, ‘pageview’, [page], [fieldsObject]);

If you are not sure, what is the difference between a field and field object, then read this article: Introduction to Google Analytics Commands

Here, page is the path portion of the url. Its value should start with forward slash ‘/’. Its default value is document.location.pathname.

If you want to supply your own URI (or better say rewrite URI in your GA reports), then you can do that by supplying a custom value to the ‘page’ field. For example:

ga(‘send’,{

‘hitType’: ‘pageview’,

‘page’: ‘/wommen/bags/sales2015/’

});

You can also write the above code as:

ga(‘send’,‘pageview’,’/wommen/bags/sales2015/’});

The page field is also used to send virtual pageviews.

Syntax for sending virtual pageview:

ga(‘send’, {

‘hitType’: ‘pageview’,

‘page’: ‘name of your virtual page’,

});

For example:

<a href=”https://www.conversions.com/CRO-beginners-guide.pdf” onClick=”ga(‘send’, ‘pageview’, ‘/virtual/guides/download/CRO-beginners-guide.pdf’);”> Download CRO Beginners Guide</a>

Here I am sending a virtual pageview when a user clicks on the ‘Download CRO Beginners Guide’ link.

 

Following are the field objects relevant to tracking pageviews:

#1 title – it is the title of the web page being tracked. Its default value is document.title.

If you want to supply your own title (or better say rewrite titles in your GA reports), then you can do that by supplying a custom value to the ‘title’ field object:

ga(‘send’,{

‘hitType’: ‘pageview’,

‘title’: ‘new york’

});

 

#2 location – it is the full URL (excluding anchor) of the web page being tracked. Its default value is document.location.

If you want to supply your own URL (or better say rewrite URL in your GA reports), then you can do that by supplying a custom value to the ‘location’ field object:

ga(‘send’,{

‘hitType’: ‘pageview’,

‘location’: ‘https://www.vb.com/men/shop/sales2015/shirts’

});

Here is how you can set all of the pageview fields in one go:

 ga(‘send’,{

‘hitType’: ‘pageview’,

‘title’: ‘new york’,

‘location’:’https://www.vb.com/men/shop/sales2015/shirts’,

‘page’: ‘/wommen/bags/sales2015/’

});

The complete code can look like the one below:

<script>

 (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){

 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)

 })(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

 ga(‘create’, ‘UA-1445352-16’, ‘auto’);

 ga(‘send’,{

‘hitType’: ‘pageview’,

‘title’: ‘new york’,

‘location’:’https://www.vb.com/men/shop/sales2015/shirts’,

‘page’: ‘/wommen/bags/sales2015/’

});

</script>

You can verify whether these new pageview fields’ values, are being sent to GA server, through Google developer console.

Before any changes to the pageview fields:

before

After:

after

Note: The default tracker does not set the ‘page’ field by default. The value of the ‘page’ field is extracted from ‘document.location’. So when you set the value of ‘page’ field manually, that value is used (and reported by developer console), instead of the ‘document.location.pathname’

 

The following code will change the URL path but won’t change the title and location field of the tracked web page:

ga(‘send’,’pageview’,’/wommen/bags/sales2015/’,’https://www.vb.com/men/shop/sales2015/shirts’,’new york’);

still the same

The location and title are not changed because, ‘location’ and ‘title’ can’t be used as fields while sending the pageview hit.

However they can be used as field objects while sending the pageview hit. For example:

ga (‘send’,’pageview’,’/wommen/bags/sales2015/’,{

‘title’:’new york’,

‘location’:’https://www.vb.com/men/shop/sales2015/shirts’

});

If you are not sure, what is the difference between a field and field object, then read this article: Introduction to Google Analytics Commands

If you want to use ‘title’ and ‘location’ as fields instead of field objects while sending the ‘pageview’ hit, you can do that by using the ‘set command method. For example:

ga(‘set’, ‘title’, ‘new york’);

ga(‘set’, ‘location’, ‘https://www.vb.com/men/shop/sales2015/shirts’);

ga (‘send’,’pageview’,’/wommen/bags/sales2015/’);

Announcement about my books

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books:

error: Content is protected !!