项目作者: swup

项目描述 :
:chart_with_upwards_trend: Swup Google Analytics plugin.
高级语言: JavaScript
项目地址: git://github.com/swup/ga-plugin.git
创建时间: 2019-04-30T22:06:47Z
项目社区:https://github.com/swup/ga-plugin

开源协议:MIT License

下载


Swup Google Analytics Plugin

A swup plugin for integrating Google Analytics.

  • Trigger pageview events after each page change
  • This event is not triggered on intial load, so the first page view must be triggered elsewhere
  • However, the page view event is by default triggered in the Javascripts tracking snippet used for embedding Google Analytics

Simplified code run by this plugin on the page:view hook:

  1. // On sites using gtag.js
  2. window.gtag('config', GA_MEASUREMENT_ID, {
  3. page_title: title,
  4. page_path: url
  5. });
  6. // On sites using analytics.js
  7. window.ga('set', 'title', document.title);
  8. window.ga('set', 'page', window.location.pathname + window.location.search);
  9. window.ga('send', 'pageview');

Installation

Install the plugin from npm and import it into your bundle.

  1. npm install @swup/ga-plugin
  1. import SwupGaPlugin from '@swup/ga-plugin';

Or include the minified production file from a CDN:

  1. <script src="https://unpkg.com/@swup/ga-plugin@2"></script>

Usage

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

  1. const swup = new Swup({
  2. plugins: [new SwupGaPlugin()]
  3. });

Options

gaMeasurementId

This option is only required on sites using gtag.js, usually loaded like this:

  1. <script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

The window.gtag function the measurement ID to trigger page views.

  1. const swup = new Swup({
  2. plugins: [
  3. new SwupGaPlugin({ gaMeasurementId: GA_MEASUREMENT_ID })
  4. ]
  5. });