Kevinleary.net, LLC.

Unique Pageviews in Google Analytics 4

How to track a "Unique Pageviews" metric in Google Analytics 4

Pageviews & Unique Pageviews vs. Views & Users

The older universal analytics implementation of Google Analytics has two metrics that are commonly used to track pageviews on a site:

  • Pageview represents a view of a page on your site. If a person refreshes or navigates back to the same page, more views will be counted. It’s similar to an entry in an access log: one count per pageview, no matter how the pageview happened.
  • Unique Pageview tracks a view of a page without counting duplicates from the same user during the same session.

Unfortunately the concept of Unique Pageviews has been removed entirely Google Analytics 4, the metric doesn’t exist. GA4 provides two similar metrics instead:

  • Views represents a view of a page on your site, and is identicle to the UA Pageview you’re familiar with
  • Users represents the number of users that visited a specific page

GA4 still tracks sessions that default to 30 mins, but they don’t provide a Views/session metric.

But we can create one ourselves using a custom event. In GA4 the concept of an event is used for everything, including “Views” which represent the number of page_view events. These events are sent in when a new page is loaded, so they’re functionally the same as a pageview but the terminology has changed a bit.

Adding Unique Pageviews to Google Analytics 4

If you’ve installed Google Analytics 4 using Google Tag Manager then you can follow these steps to create a Unique Pageviews metric for your site.

Code Level Changes

The process requires making some basic code level changes to your website. The snippet below should provides everything you need and will track a session using a 1st-party cookie with an expiration of 30 minutes. 30 minutes is the default that Google Analytics 4 uses for a session TTL, which is why I’m using it. Check out the Google doc about sessions for more information.

To handle the cookie in this demo I’m using the js-cookie library, which provides a simple API for working with client-side HTTP cookies. If you use what I’ve provided here you’ll need to install this package, or load it from a CDN like jsdelivr.com. More details on ways to work with it can be found on the projects GitHub readme. You’ll also need to load this script on every page in your site.

/**
 * Analytics Tracking
 *
 * Enhancements for Google Analytics 4
 */
const dataLayer = window.dataLayer;

(function uniquePageview() {

  const cookie = Cookies.withAttributes({ path: '/', domain: location.hostname, secure: true, expires: 30 })

  // Record unique pageviews in an array
  let uniquePageviews = cookie.get("unique-pageviews");
  if (uniquePageviews) {
    uniquePageviews = JSON.parse(uniquePageviews);
  } else {
    uniquePageviews = [];
  }

  // Don't continue if the current page has already been viewed in this session
  if (uniquePageviews.indexOf(location.href) !== -1) {
    return;
  }

  // Track a pageview for a newly visited URL in this session
  uniquePageviews.push(location.href);
  uniquePageviews = JSON.stringify(uniquePageviews);
  cookies.set("unique-pageviews", uniquePageviews);

  // Send "unique_page_view" event to GA4
  const properties = {
    event: "unique_page_view",
    page_location: location.href,
    page_referrer: document.referrer,
  };
  dataLayer.push(properties);

  // Debug helper (optional)
  console.log(`"unique_page_view" tracked in GA4`, properties);
})();

This script will do the following:

  1. Record all unique pageviews in an array for up to 30 minutes (session)
  2. Push a custom event into Google Tag Manager called unique_page_view each time a unique pageview happens
  3. Log a message to the browser console for testing/verification

Google Tag Manager → Google Analytics 4 Event

Now that we have data for our new event pushed into GTM we’ll need to configure a few things to get it sent to GA4. Here are the next steps you’ll need to take in Google Tag Manager.

Step 1: Live Unique Pageview Trigger

Create a new custom event Firing Trigger called Live Unique Pageview. This allows us to fire tags when the dataLayer receives our custom unique_page_view event.

Google Tag Manager unique_page_view Trigger

Step 2:

Next we’ll create a new Google Analytics: GA4 Event tag to send a unique_page_view to Google Analytics 4 for tracking.

Google Tag Manager unique_page_view Event Tag for Google Analytics 4

In the Event Name field enter unique_page_view, and add the following Event Parameters using built-in variables as the values:

  • page_location with a value of {{Page URL}}
  • page_referrer with a value of {{Referrer}}

If you don’t see {{Page URL}} or {{Referrer}} as options, you can create them by adding a new variable and then selecting “Built-in”. You’ll find them in the list of available options.

That’s it!

You’re now tracking Unique Pageviews’s in Google Analytics 4, you’ll start seeing them appear as the unique_page_view event on your GA4 dashboard. This approach isn’t a 100% match to Google Analytics UA, but it should be pretty close and will provide you with a metric to track as a replacement.

Meet the Author

Kevin Leary, WordPress Consultant

I'm a freelance web developer and WordPress consultant in Boston, MA with 15 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.