joshuaHallee / swup-gtag-plugin

:chart_with_upwards_trend: Swup Google Analytics plugin. Updated for Global Site Tag (gtag.js).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swup Gtag Plugin

Google Analytic plugin for Global Site Tag (gtag.js). Gtag plugin triggers pageview event on contentReplaced (on each page change). Note that this event is not triggered at the first load, so the first page view must be triggered elsewhere. However, it should be noted that pageview event is called when gtag.js is installed.

Simplified code run by this plugin on contentReplaced event:

window.gtag("config", "GA_MEASUREMENT_ID", {
  page_title: pageTitle,
  page_path: pageUrl
})

Installation

This plugin can be installed with npm

npm install swup-gtag-plugin

and included with import

import SwupGtagPlugin from 'swup-gtag-plugin';

or included from the dist folder

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

Usage

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

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

Options

gaMeasurementId

gaMeasurementId defines your GA_MEASUREMENT_ID and is required.

new SwupGtagPlugin({
  gaMeasurementId: "GA_MEASUREMENT_ID"
});

About

:chart_with_upwards_trend: Swup Google Analytics plugin. Updated for Global Site Tag (gtag.js).

License:MIT License


Languages

Language:JavaScript 100.0%