Google Analytics | How do I see previous page views?
Purpose
This article will demonstrate how to capture previous page (referrer) data, and make it available in Google Analytics reports.
This may be especially useful if you have destination-based triggers.
Example scenario: tracking page views of a /thank-you page. Sometimes we do not know all of the ways a User may arrive at the page so we need to collect data on the preceding page(s) for debugging. Or we may wish to fire a tag only when Users arrive from a specific page.
Demonstrated below are two Variables that may be implemented via GTM as a solution.
- Previous Page - for basic debugging and trigger conditions
- Previous Page Stack - collect data on multiple pages for advanced debugging
Previous Page Variable
For the basic Previous Page Variable,
- create a JavaScript Variable via GTM
- add the variable as a Custom Dimension to a Tag
Variable
Copy/paste the code below into a JavaScript Variable in GTM.
document.referrer
Tag - GA4 Configuration
In order to view Previous Page in GA4 reports, the custom dimension should be configured as below.
In GA4, navigate Configure > Custom definitions > Create custom dimensions
Tag - GTM Configuration
Add the Previous Page Variable to a Tag as an Event Parameter. Your tag may be of any type but usually a page view tag makes the most sense.
GA4 Reports
The two configuration steps above make the previous_page parameter available in your GA reports. For the given Tag (in this case, page_view), we see the page a User was on immediately before the Tag fired.
Previous Page Variable as a Trigger Condition
You may have a trigger that should fire on a page view of /thank-you but only with to fire when Users come from a specified page. If you have the Previous Page Variable you may add it as a Trigger condition as demonstrated below:
Previous Page Stack
Through this implementation, we will create a string to store page paths in browsers Session Storage. Then we will retrieve that value with a Custom JavaScript Variable and pass it to Google Analytics via Tag.
Custom HTML Tag - to store previous page values
In GTM create a Custom HTML Tag and copy/paste the following code:
<script>
var stack_limit = 3; // how may page paths to store
var stored_stack = "previous_page_stack";
if (sessionStorage.getItem(stored_stack)) {
// retrieve the stored stack
var stackArray = sessionStorage.getItem(stored_stack).split("|");
for (var i = 0; i < stackArray.length; i++) {
if (stackArray[i] === "" || stackArray[i] == "undefined" ) {
stackArray.splice(i, 1);
}
}
// add the current page path
stackArray.push(document.location.pathname);
// manage the length of the stack according to the stack_limit
if (stackArray.length > stack_limit) { stackArray.shift(); }
// convert to string and store
sessionStorage.setItem(stored_stack, stackArray.join("|"));
} else {
sessionStorage.setItem(stored_stack, "home");
}
</script>
Custom JavaScript Variable - to retrieve stored values
In GTM create a Custom JavaScript Variable and copy/paste the following code:
function() { return sessionStorage.getItem("previous_page_stack"); }
previous_page_stack
The value stored in Session Storage and returned by the previous_page_stack Variable looks like this:
home|/platform/dashboard-templates/|/lets-talk/
Where page paths are added to the string, separated by "|".
The stack_limit variable included in the script limits the number of page paths included in this string and may be adjusted as needed. With the limit of 3 set, the next page view will delete the oldest page path ("home") from the string to add the latest, keeping no more than three at a time.
Previous Page Stack Conclusion
At this point, you may add this Variable to any Tag as a Custom Dimension, as previously demonstrated with the Page View Variable. You may want to do this in order to see how any User arrived at a page or conversion. At times there may be edge cases where Users unexpectedly arrive at a site location. You can use this variable to help understand those interactions.