Adding Page Count To Google Analytics 4

There are a lot of things from Google Analytics (Universal Analytics) that I miss in Google Analytics 4. Although page engagement metrics are much improved, I was frustrated by the lack of any ability to gauge the Page Depth of my visitors. Do they look at 1 page, 3 pages, or 20?  The averages just don’t tell the whole story. Here is how I added a Page Count dimension and a couple of early observations I made with the data.

The Need For A Missing Dimension

To create something like a page depth measure, I realized that there needed to be a session-based page counter, and that was missing. This counter needed to be a dimension, not a metric, since it would be used to slice and dice in my reports, not total or average (GA4 can already do that with page_view events).

If I add a Page Count dimension (a ‘page_count’ event parameter), I could see how many visitors viewed 1, 2, or 3 pages simply by counting page_view events with a page_count of 1, 2, or 3. It is not the same as the Page Depth dimension in UA, which is session-based, but it would be easy to do and better than nothing.

But how do you add a page_count parameter to the page_view event? Most of the examples of custom dimensions are about adding new event types — I didn’t want a new event type, I wanted to piggyback on the page_view. Turns out it was simple…

Building a Page Counter

Caution: this is not a detailed GTM instruction guide; I am not GTM-knowledgeable (i.e. I hacked this together with a fair amount of trial and error). If you try to repeat what I did and it destroys your tracking, don’t blame me.

I happened upon an article by Julien Coquet that shows how you could add a page counter via GTM, which I use, so I adapted his technique, See Julien’s article for details.

Start by adding a User-Defined Variable — a 1st Party Cookie type. I added the 1st Party Cookie as per Julien’s instructions (I named mine ‘aePageCount’, because, well, I did this). Note the convert null/undefined to 0 settings. First party cookies get dumped when a session closes, which is perfect in this application.

I then added a Taga Custom HTML type to populate the cookie as Julien’s instructions suggest. Make sure the tag fires only Once per page:

Here is the script you can copy and paste (and modify with your domain):

  var cc = {{Cookie - Page Count}}-0+1;
   var dn = ""; // prefix with dot to include all subdomains

The last step in GTM is to add the page_count to the Google Analytics 4/App+Web Configuration tag. There may be a better way to get it added only to the page_view event, but I couldn’t find it, so I think this adds it to all GA4 events, but that might provide some interesting analysis options later, so why not?

That’s it for GTM — you should test that before you deploy ;-)

NOTE: I realize that this creates a page_count that starts at 0 and counts up from there. I am OK with that. If you want yours to start at 1…well…you figure it out.

Last Step: Create a Custom Dimension

It is one thing to populate the data into a new custom parameter, but if you want to report on it, you need to add a custom dimension as well. Back in Google Analytics 4, go to Custom definitions and Create a new Custom Dimension. I called mine ‘Page Count’:

Now You Wait…

Nothing happens right away….the data should be populating and the dimension will be created by tomorrow. If you want a quick check to see if it is actually working, you can peek at the Realtime reports — in the Event count panel, click the page_view entry to see the event parameters being recorded. You might see page_count starting to appear…success! Now go to bed.


Reporting Observations

Now that you have a little bit of data, you should be able to create a report with your new custom dimension. In my case, I created a few reports with the Analytics Edge Google Analytics Pro connector to see what I collected, and aside from the expected histogram-like data:

I found you could also view the Page Count by Page (filtered for Event Name of page_view). If you ignore the top pages, it lets you see how far people got in their journey through your site before they found that specific page.

I also looked at the other event types, and at a quick glance you could see how many pages people viewed before downloading a file, or viewed a video.

I am just beginning to play with this new capability, so I leave it to your imagination to figure out what else you might be able to garner from it.


Mike Sullivan