@greenwood/plugin-google-analytics

0.29.2 • Public • Published

@greenwood/plugin-google-analytics

Overview

A Greenwood plugin adding support for Google Analytics JavaScript tracker. It assumes you already have your own Tracking ID(s) and can either filter out tracking for everything but your production environment so that local testing doesn't interfere with production data, or use a conditional based analyticsId using an environment variable, ex.

const analyticsId = process.env.NODE_ENV === 'xxx' ? 'UA-123...' : 'UA-345...';

For more information and complete docs about Greenwood, please visit the Greenwood website.

Installation

You can use your favorite JavaScript package manager to install this package. This package assumes you already have @greenwood/cli installed.

examples:

# npm
npm install @greenwood/plugin-google-analytics --save-dev

# yarn
yarn add @greenwood/plugin-google-analytics --dev

Usage

Use this plugin in your greenwood.config.js and pass in your Google Analytics ID, which can either be a

  • Measurement ID (recommended): ex. G-XXXXXX
  • Tracking ID (legacy): ex. UA-XXXXXX
import { greenwoodPluginGoogleAnalytics } from '@greenwood/plugin-google-analytics';

export default {
  ...

  plugins: [
    greenwoodPluginGoogleAnalytics({
      analyticsId: 'UA-XXXXXX'
    })
  ]
}

This will then add the Google Analytics JavaScript tracker snippet to your project's index.html.

Learn more about Measurement and Tracking IDs.

Options

  • analyticsId (required) - Your Google Analytics ID
  • anonymous (optional) - Sets if tracking of IPs should be done anonymously. Default is true

Outbound Links

For links that go outside of your domain, the global function getOutboundLink is available for you to use.

Example:

<a
  target="_blank"
  rel="noopener"
  onclick="getOutboundLink('www.mylink.com');"
  href="www.mylink.com">My Link
</a>

Package Sidebar

Install

npm i @greenwood/plugin-google-analytics

Weekly Downloads

6

Version

0.29.2

License

MIT

Unpacked Size

6.05 kB

Total Files

4

Last publish

Collaborators

  • thescientist13