Page views

Measure page views

There are two ways to measure a page view:

  1. Use the default behavior of the gtag.js snippet.
  2. Send a page_view event.

Default snippet behavior

When you add gtag.js to your site, the snippet includes a config command that by default sends a pageview. You can include additional parameters to configure this behavior:

gtag('config', 'GA_MEASUREMENT_ID', <parameters>);

<parameters> is an object used to configure how Google Analytics is initialized. When customizing the pageview behavior, the following optional parameters can be set:

Name Type Default value Description
page_title string document.title The title of the page.
page_location string location.href The URL of the page.
send_page_view boolean true Whether or not a pageview should be sent. Set to false to Disable the default page_view measurement behavior.

Example: Modify the page URL and title

In the following example, the page URL and title of the page view are modified:

gtag('config', 'GA_MEASUREMENT_ID', {
  page_title : 'My Profile',
  page_location: 'https://example.com/me' // The full URL is required.
});

Send a page_view event

Send a page_view event and modify page parameters as needed. Default values will be used for any page parameters not set.

gtag('event', 'page_view', {
  page_title: 'My Profile',
  page_location: 'https://example.com/me',  // The full URL is required.
  send_to: '<GA_MEASUREMENT_ID>'
});

Disable pageview measurement

The default behavior of this snippet is to send a page_view event to Google Analytics. This is the desired behavior in most cases; page_view events are automatically recorded once you add the snippet to each page on your site. However, if you don’t want the snippet to send a page_view event to Google Analytics, set the send_page_view parameter to false:

gtag('config', 'GA_MEASUREMENT_ID', {
  send_page_view: false
});

Measure virtual pageviews

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 Google Analytics 4, you have the following options to measure virtual page views:

  • Option 1: Rely on Enhanced measurement, which is enabled by default. Enhanced measurement will automatically send a page view event when the browser history state changes.
  • Option 2: Manually measure virtual pageviews in code by relying on the Default snippet behavior or the page_view event to send a pageview.

    • If you manually send page view events, make sure Enhanced measurement is configured correctly to avoid double counting page views on history state changes. Typically this means disabling Page changes based on browser history events under the advanced settings of the Page views section.
    • When a page view is sent to Google Analytics, the default page parameter values are used, unless modified. This means you do not need to modify page_title or page_location parameters if updates to window.location (e.g. via the History API) and document.title are made prior to the event being sent.
      • If your app relies on a library to handle page updates it's recommended that you review the behavior to confirm the library sets window.location and document.title as expected. Alternatively, you can always explicitly set page parameters when sending a page view event to ensure accuracy and clarity.