Skip to main content

TG site tag - tracking single page applications (SPAs)

Written by TrafficGuard Support
Updated over a week ago

Single-page applications (SPAs) provide a fast user experience by loading content without a full page refresh. However, because the page doesn't "reload," standard tracking tags often miss these internal page changes.

This guide shows you how to use Google Tag Manager (GTM) to capture every SPA page view automatically using the TG site tag.


πŸ’‘ Prerequisites

  • Admin access to your GTM container.

  • The standard TG site tag is already installed and firing on the initial page load.


Setup guide: Google Tag Manager


1. Create the PageView tag

  • In GTM, create a New Tag named TrafficGuard - PageView.

  • Select Custom HTML as the tag type.

  • Paste the following code into the HTML box:

<script>
// Custom TrafficGuard PageView Event
window.dataTrafficGuard = window.dataTrafficGuard || [];
dataTrafficGuard.push(['event','pageview','{"evs":"spa"}']);
</script>

2. Configure the trigger

  • In the Triggering section of your new tag, click the + icon to add a trigger.

  • Name it TrafficGuard PageView Trigger and select Custom Event as the type.

  • In the Event name field, enter exactly: gtm.historyChange.

  • Ensure All Custom Events is selected and save the trigger.

3. Save and publish

  • Save the tag, then click Submit and Publish in GTM to make the changes live.


How to verify the setup

To ensure your SPA tracking is active:

  1. Open your website and your browser's Developer Tools (Right-click > Inspect).

  2. Go to the Network tab and type trafficguard in the filter box.

  3. Click through your site's navigation.

  4. Success: You should see a new event request appears in the list every time the URL changes.
    ​

Did this answer your question?