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

gatsby-plugin-google-marketing-platform-partytown

v0.2.2

Published

Gatsby plugin to add different components of the Google Marketing Platform: Tag Manager, Analytics, Optimize

Downloads

12

Readme

Google Marketing Platform for Gatsby

All Contributors

Easily add these GMP products in a cohesive way to your Gatsby site:

  • Google Tag Manager
  • Google Analytics
  • Google Optimize

The installation is based on Google's recommendations and best practices.

What This Does

This provides the ability to configure the Google Marketing Platform base tools, including Google Analytics, Google Optimize, and Google Tag Manager.

This plugin also allows the ability to preset dataLayer variables. This is useful in cases where you need GA, configured within GTM, to fire with particular variables on the initial pageview, such as dynamically setting your Google Analytics Property ID, instead of requiring to maintain it in multiple locations.

What this does NOT do

This will not provide any functionality around specific tracking or conversion tags. These responsibilities should be handled in Google Tag Manager.

Getting Started

Install

yarn add gatsby-plugin-google-marketing-platform

or

npm install gatsby-plugin-google-marketing-platform

Basic Setup

// In your gatsby-config.js
plugins: [
  {
    resolve: 'gatsby-plugin-google-marketing-platform',
    options: {
      dataLayer: {
        gaPropertyId: '[Google Analytics ID]',
      },
      tagmanager: {
        id: '[Google Tag Manager ID]'
      },
      analytics: {
        id: '[Google Analytics ID]'
      }
    },
  }
]

Configuration

| Key | Type | Default | Description | | ----------------------------------------- | ------- | ---------| -------------------------------------------- | | dataLayer | object | {} | See Below | | tagmanager | object | {} | See Below | | analytics | object | {} | See Below | | optimize | object | {} | See Below | | includeInDevelopment | boolean | false | Scripts will be bundled in development mode |

Data Layer

The dataLayer key is a freeform object that will allow you to pass data through to the globally available dataLayer array that Google Tag Manager utilizes to store and watch data. There is no set functionality for this, but you can see below for some usage examples.

Examples

| Key | Type | Default | | ------------------------- | ------- | ---------| | gaPropertyId | string | None |

Tag Manager

Configuration specifics for Google Tag Manager.

| Key | Type | Default | Description | | ------------------ | ------ | -------- | --------------------------------------------------- | | id | string | None | Google Tag Manager ID | | params | object | {} | Additional parameters to be added on GTM script URL |

Analytics

Configuration specifics for Google Analytics.

| Key | Type | Default | Description | | ------------------ | ------ | -------- | --------------------------------------------------- | | id | string | None | Google Analytics ID | | config | object | {} | Custom GA configuration |

Optimize

Configuration specifics for Google Optimize.

| Key | Type | Default | Description | | ------------------ | ------ | -------- | ---------------------------------------------------------------- | | id | string | None | Google Optimize ID | | timeout | number | 500 | Number of milliseconds Optimize waits to try to load - Docs | | activateOn | string | None | Activation Method |

Activation Method

By default, this plugin doesn't push any activation events to Google Tag Manager for single page apps. The activateOn property allows configuration to provide different methods of activation.

Activation events refers to Google's way of handling re-activating Optimize on DOM change.

https://support.google.com/optimize/answer/7008840?hl=en

Options

Examples

Google Tag Manager with Google Analytics and Google Optimize

// In your gatsby-config.js
plugins: [
  {
    resolve: 'gatsby-plugin-google-marketing-platform',
    options: {
      dataLayer: {
        // Preset dataLayer values
        gaPropertyId: '[Google Analytics ID]',
      },
      tagmanager: {
        id: '[Google Tag Manager ID]',
        params: {
          // GTM URL Parameters
          // Ex: https://www.googletagmanager.com/gtm.js?id=[ID]&gtm_cookies_win=x
          gtm_cookies_win: 'x'
        }
      },
      analytics: {
        id: '[Google Analytics ID]',
      },
      optimize: {
        id: '[Google Optimize ID]',
      }
    }
  }
]

What That Will Add

Note: the below will be minified along with the rest of the code and not include comments

...
<head>
  ...

  <!-- Set up dataLayer with data if provided -->
  <script>
  window.dataLayer = window.dataLayer || [{"gaPropertyId":"TEST-1234"}];
  </script>

  <!-- Initialized GTM via gtag -->
  <script>
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  </script>

  <!-- Google Optimize async hide class -->
  <style>.async-hide { opacity: 0 !important }</style>

  <!-- Google Optimize async hide script -->
  <script>
  (function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
  h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
  (a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
  })(window,document.documentElement,'async-hide','dataLayer',500,{'ASDF-4321':true});
  </script>

  <!-- Configure GTM given GA and GO IDs -->
  <script>
  gtag('config', 'TEST-1234', {"optimize_id":"ASDF-4321"});
  </script>

  <!-- Configure and initialize GTM -->
  <script>
  (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id='+i+dl+'&gtm_cookies_win=x';f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer', 'TESTASDF-12345678');
  </script>
  ...
</head>
...
<body>
  <!-- GTM noscript for when JS isn't available -->
  <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=TESTASDF-12345678&gtm_cookies_win=x" height="0" width="0" style="display: none; visibility: hidden ></iframe></noscript>

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!