npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jgantunes/gatsby-plugin-google-gtag

v1.0.0

Published

Gatsby plugin to add google gtag onto a site

Downloads

7

Readme

gatsby-plugin-google-gtag

Easily add Google Global Site Tag to your Gatsby site.

This release is a fork of the original version within the gatsby repo with a feature by @imreFitos applied (https://github.com/gatsbyjs/gatsby/pull/35929)

The global site tag (gtag.js) is a JavaScript tagging framework and API that allows you to send event data to Google Analytics, Google Ads, Campaign Manager, Display & Video 360, and Search Ads 360.

Global site tag (gtag.js) is meant to combine multiple Google tagging systems and can replace older ones such as analytics.js (gatsby-plugin-google-analytics).

For more general information on gtag you can read Google's official documentation on the subject: https://developers.google.com/gtagjs/.

If you're migrating from analytics.js (gatsby-plugin-google-analytics) you can read about the subtle API differences in more depth at: https://developers.google.com/analytics/devguides/migration/ua/analyticsjs-to-gtagjs.

Please note: This plugin only works in production mode! To test your Global Site Tag is installed and firing events correctly run: gatsby build && gatsby serve.

Install

npm install gatsby-plugin-google-gtag

How to use

// In your gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-google-gtag`,
      options: {
        // You can add multiple tracking ids and a pageview event will be fired for all of them.
        trackingIds: [
          "GA-TRACKING_ID", // Google Analytics / GA
          "AW-CONVERSION_ID", // Google Ads / Adwords / AW
          "DC-FLOODIGHT_ID", // Marketing Platform advertising products (Display & Video 360, Search Ads 360, and Campaign Manager)
        ],
        // Optional - if you want to set default consent settings
        // this example disables ad and analytics cookies
        gtagConsent: {
          ad_storage: 'denied',
          analytics_storage: 'denied'
        },
        // This object gets passed directly to the gtag config command
        // This config will be shared across all trackingIds
        gtagConfig: {
          optimize_id: "OPT_CONTAINER_ID",
          anonymize_ip: true,
          cookie_expires: 0,
        },
        // This object is used for configuration specific to this plugin
        pluginConfig: {
          // Puts tracking script in the head instead of the body
          head: false,
          // Setting this parameter is also optional
          respectDNT: true,
          // Avoids sending pageview hits from custom paths
          exclude: ["/preview/**", "/do-not-track/me/too/"],
          // Defaults to https://www.googletagmanager.com
          origin: "YOUR_SELF_HOSTED_ORIGIN",
          // Delays processing pageview events on route update (in milliseconds)
          delayOnRouteUpdate: 0,
        },
      },
    },
  ],
}

Custom Events

This plugin automatically sends a "pageview" event to all products given as "trackingIds" on every Gatsbys route change.

If you want to call a custom event you have access to window.gtag where you can call an event for all products:

window.gtag("event", "click", { ...data })

or you can target a specific product:

window.gtag("event", "click", { send_to: "AW-CONVERSION_ID", ...data })

In either case don't forget to guard against SSR:

typeof window !== "undefined" && window.gtag("event", "click", { ...data })

<OutboundLink> component

To make it easy to track clicks on outbound links the plugin provides a component.

To use it, simply import it and use it like you would the <a> element e.g.

import React from "react"
import { OutboundLink } from "gatsby-plugin-google-gtag"

export default () => (
  <div>
    <OutboundLink href="https://www.gatsbyjs.com/plugins/gatsby-plugin-google-gtag/">
      Visit the Google Global Site Tag plugin page!
    </OutboundLink>
  </div>
)

The optional "gtagConsent" options

If you want to set the default Consent options, e.g. disable ad or analytics cookies, this is where you can put them. All these options are passed directly to the gtag consent command.

The "gtagConfig.anonymize_ip" option

Some countries (such as Germany) require you to use the _anonymizeIP function for Google Site Tag. Otherwise you are not allowed to use it. The option adds the block of code below:

function gaOptout() {
  ;(document.cookie =
    disableStr + "=true; expires=Thu, 31 Dec 2099 23:59:59 UTC;path=/"),
    (window[disableStr] = !0)
}

var gaProperty = "UA-XXXXXXXX-X",
  disableStr = "ga-disable-" + gaProperty
document.cookie.indexOf(disableStr + "=true") > -1 && (window[disableStr] = !0)

If your visitors should be able to set an Opt-Out-Cookie (No future tracking) you can set a link e.g. in your imprint as follows:

<a href="javascript:gaOptout();">Deactivate Google Tracking</a>

The "gtagConfig.optimize_id" option

If you need to use Google Optimize for A/B testing, you can add this optional Optimize container id to allow Google Optimize to load the correct test parameters for your site.

Other "gtagConfig" options

The gtagConfig is passed directly to the gtag config command, so you can specify everything it supports, e.g. gtagConfig.cookie_name, gtagConfig.sample_rate. If you're migrating from the analytics.js plugin, this means that all Create Only Fields should be snake_cased.

The "pluginConfig.respectDNT" option

If you enable this optional option, Google Global Site Tag will not be loaded at all for visitors that have "Do Not Track" enabled. While using Google Global Site Tag does not necessarily constitute Tracking, you might still want to do this to cater to more privacy oriented users.

The "pluginConfig.exclude" option

If you need to exclude any path from the tracking system, you can add it (one or more) to this optional array as glob expressions.

The "pluginConfig.delayOnRouteUpdate" option

If you need to delay processing pageview events on route update (e.g. to wait for page transitions with gatsby-plugin-transition-link), then this option adds a delay before generating the pageview event.