The older universal analytics implementation of Google Analytics has two metrics that are commonly used to track pageviews on a site:
Unfortunately the concept of Unique Pageviews has been removed entirely Google Analytics 4, the metric doesn’t exist. GA4 provides two similar metrics instead:
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.
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.
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:
unique_page_view
each time a unique pageview happensNow 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.
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.
Next we’ll create a new Google Analytics: GA4 Event tag to send a unique_page_view
to Google Analytics 4 for tracking.
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.
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.