Page Views

Page view measurement allows you to measure the number of views you had for a particular page on your website. Pages often correspond to an entire HTML document, but they can also represent dynamically loaded content; this is known as "virtual pageviews".

This guide explains how to implement page view measurement with analytics.js.

Overview

The JavaScript tag includes a command to create a tracker object and then a command to send a pageview to Google Analytics. When the tracker gets created, several of the fields get set based on the browsing context. The title field is set to the value of document.title, and the location field gets set to the value of document.location, ignoring the anchor portion of the URL.

When the send command is executed, the title and location fields stored on the tracker get sent, and Google Analytics uses those values to show you which pages your users visited.

The default tracker does not set the page field, but if you set it manually, that value gets used as the page path in reports, overriding the location field's value.

Implementation

Pageview hits can be sent using the send command and specifying a hitType of pageview. The send command has the following signature for the pageview hit type:

ga('send', 'pageview', [page], [fieldsObject]);

Pageview fields

The following table summarizes the primary fields relevant to pageview measurement. For more details (and some additional fields) see the content information section of the field reference.

Field Name Value Type Required Description
title text no The title of the page (e.g. homepage)
location text no * URL of the page.
page text no * The path portion of a URL. This value should start with a slash (/) character.

*  though neither the page field nor the location field is required, one of them must be present or the hit will be invalid.

Examples:

The following command sends a pageview hit to Google Analytics and includes the path of the current page.

ga('send', 'pageview', location.pathname);

Note that as with all send commands, the fields passed in the convenience parameters may also be specified in the fieldsObject. The above command could be rewritten as:

ga('send', {
  hitType: 'pageview',
  page: location.pathname
});

Modifying page URLs

In some cases the URL you want to send to Google Analytics is different from the URL that appears in the address bar of the user's browser. For example, consider a site with a few pages where users can log in and view/edit their personal information. If this site has separate pages for personal information, account information, and notification settings, the URLs for these pages might look something like this:

  • /user/USER_ID/profile
  • /user/USER_ID/account
  • /user/USER_ID/notifications

If you want to know, in total, how many people visit each of these pages, including a unique user ID value in the URLs will make that much more difficult.

To solve this problem you can specify a page value with the user ID removed:

// Checks to see if the current user's userID is
// found in the URL, if it is, remove it.
// (Note, this assume the user ID is stored
// in a variable called `userID`)

if (document.location.pathname.indexOf('user/' + userID) > -1) {
  var page = document.location.pathname.replace('user/' + userID, 'user');
  ga('send', 'pageview', page);
}

This will send the following page values for all users:

  • /user/profile
  • /user/account
  • /user/notifications

If the current page is sending other hits (like events), you'll want to make sure every hit gets sent with the correct URL. In such cases, you should update the page field on the tracker instead of passing it in the send command.

Setting it on the tracker will ensure the new page value gets used for all subsequent hits:

if (document.location.pathname.indexOf('user/' + userID) > -1) {
  var page = document.location.pathname.replace('user/' + userID, 'user');

  // Sets the page value on the tracker.
  ga('set', 'page', page);

  // Sending the pageview no longer requires passing the page
  // value since it's now stored on the tracker object.
  ga('send', 'pageview');
}

Tracking virtual pageviews

Many websites today load content dynamically via AJAX without requiring a full page load for each "page". Such sites are commonly referred to as Single Page Applications (SPAs).

If your website loads page content dynamically and updates the document's URL, you'll usually want to send additional pageviews to measure these "virtual pageviews". For full implementation details, see the guide on Single Page Application Tracking with analytics.js.