You are browsing the docs for swup 3.
Click here for the latest version.

swup v3 swup v3 GTM Plugin
GitHub swup on GitHub

GTM Plugin

Google Tag Manager Plugin triggers VirtualPageview event on swup:contentReplaced (on each page change) which can be associated with a page view within GTM. Event object also includes virtualPageURL holding the url of the page and virtualPageTitle holding the title of the page. Note that this event is not triggered at the first load, so the first page view must be triggered elsewhere. Simplified code run by this plugin on contentReplaced event:

window.dataLayer.push({
  event: 'VirtualPageview',
  virtualPageURL: window.location.pathname + window.location.search,
  virtualPageTitle: document.title
});

Installation

This plugin can be installed with npm

npm install @swup/gtm-plugin

and included with import

import SwupGtmPlugin from '@swup/gtm-plugin';

or included from the dist folder

<script src="./dist/SwupGtmPlugin.js"></script>

Usage

To run this plugin, include an instance in the swup options.

const swup = new Swup({
  plugins: [new SwupGtmPlugin()]
});