Unique Pageview Tracking using GTM JS Variable

Posted by: Felix Kaboto Date Posted: August 29, 2024
Back to Case Studies

Issue

One common challenge when setting up tracking is having to setup conversions to fire on page views, such as a thank-you page after form submission without recording duplicates and discrepancies from multiple page loads or navigations that do not represent actual new conversions (e.g., page reloads, history navigation)

Solution

This case study explores one of the solutions implemented using GTM that can be used to better conversion tracking by using the NavigationType from the Performance API, this way we could specify our triggers to fire only for direct navigations to the thank-you page, excluding reloads and history navigations.

Achievement

Implementing this solution helped to improve the accuracy of conversion tracking. It aligned reported conversions more closely with unique user actions rather than mere page views, reducing discrepancies between reported data in Google Analytics/LMFX and actual CRM records.

Snippet/Screenshot/Link

Create a Custom JavaScript Variable in GTM using the below script.

function() {
// Check if PerformanceNavigationTiming Available in the window object
if ("PerformanceNavigationTiming" in window) {
var perfEntries = performance.getEntriesByType("navigation");

// Ensure there are entries to evaluate
if (perfEntries.length > 0) {
var navigationType = perfEntries[0].type;
return navigationType;
}
}

// Fallback for older browsers
if (performance.navigation) {
return performance.navigation.type;
}
}

Setup Trigger in GTM: Configure the trigger for the conversion tag to fire only when the custom JS variable returns “navigate,” indicating direct navigation.

Trigger Type: Page View: this trigger fires on Some Page Views

Conditions: {{c JS navigationType}} equals navigate