How to measure the previous page in GA4

One of the things I miss from GA4 with respect to Universal Analytics is the “previous page path” dimension, as well as “next page path”. In this post we are going to recover this level of tracking through custom dimensions + a GTM script, going up to a depth of three pages prior to the current one where the user is browsing.

Why measure the previous page in GA4 with custom dimensions?

Currently, GA4 has the “page referrer” dimension, which has several drawbacks:

  1. It mixes pages of the own website with external pages, when the current page is the first one that the user navigates within our website.
  2. This dimension does not work when the user makes multitab. That is, when he opens several tabs of our website in parallel in his browser to view and compare products, for example.
  3. This dimension also does not respond when our website is operated as a SPA (Single Page Application).

Imagine for example this use case: you place a quiz or wizard on several pages of your website to guide the user’s preferences with a few questions and finally capture their email to generate leads. This solution that we are going to see would allow you to see from which locations it is initiated more frequently and with greater success.

The script and the implementation in GTM

The implementation of this functionality in Google Tag Manager is done as follows:

Custom HTML tag

We create this tag that you see below (I leave a .zip file to download at the end of the article) to run on all pages. In case your page is a SPA, add triggers like “history change”.

This script does the following:

  1. Generates the following variables:
    • nav_current_page, which indicates the current page the user is on.
    • nav_page_views, which indicates the number of pages the user has viewed during his session.
    • nav_previous_page, which indicates what the previous page was before the current page. If the current page was the first page the user saw of our website during their session, this dimension would set the value to “(entrance)”.
    • nav_two_pages_ago, which indicates which page was the page before the previous page. If the current page was the second page the user views of our website during their session, this dimension would set the value “(entrance)”.
    • nav_three_pages_ago, which indicates which was the page before the “nav_two_pages_ago”. If the current page was the third page the user views of our website during his session, this dimension would set the value “(entrance)”.
  2. Store variables as first-party cookies hosted on the root domain being browsed: if your website has several subdomains, this will be useful.
  3. Detects if there are page or SPA history loads to update the values.

Some considerations:

  • This tag assumes that the getCookie function, which is widely used in JavaScript, already exists. In case you don’t have it, add it.
  • The snippet can be incorporated directly into the source code of your website, without relying on GTM.

Configuration of variables in GTM

These variables should be included in the Tag Manager, before sending them to GA4:

To do this, we create the variables one by one as “1st party cookies”.

And we add them to the set of Google variables to incorporate in the GA4 tags:

Configuration in GA4

Now we would only have to register the dimensions in Google Analytics 4. To do this we go to the administration interface, in Data Display -> Custom Definitions:

And we configure the dimensions individually with event scope, associating it to the variable that we have configured in the GA4 tag in the Tag Manager:

Finally, the configuration would look like this:

Solution testing

We would have to test that the solution works. Taking the example of my eCommerce, let’s follow the following navigation pattern:

  1. We start the navigation in the home page: https://www.lalomamarket.es/
  2. Then we go to: https://www.lalomamarket.es/el-mejor-aceite-de-jaen-online/
  3. Next, we go to the offers section: https://www.lalomamarket.es/comprar-aceite-de-oliva-virgen-extra-barato-ofertas-precio/.
  4. And we finish in the section of gift packs: https://www.lalomamarket.es/packs-regalo-aceite-de-oliva/

In GA4 real time, our new dimensions would also be seen:

Tras publicar tu contenedor de GTM con estos elementos, ya tendrías dimensiones nuevas para explotar en informes y exploraciones de GA4, así como en Looker Studio.

Otra implicación es que podrías utilizar las cookies que has generado para acciones de activación: popups o banners en la web cuando el usuario sigue una secuencia de páginas concretas, o ve un número de páginas concreto, etc.

Code snippet for downloading

Here you have the code for the Custom HTML that we commented at the beginning. I hope you find it useful.

How do you measure the previous pages in GA4?

Leave a Comment