Giter Club home page Giter Club logo

gatsby-plugin-google-marketing-platform's Introduction

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):


Colby Fayock

๐Ÿ’ป ๐Ÿ“–

Richard Taylor Dawson

๐Ÿ“–

Markus Witzlsperger

๐Ÿ’ป

Matt Smith

๐Ÿ“– ๐Ÿ’ป

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

gatsby-plugin-google-marketing-platform's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gatsby-plugin-google-marketing-platform's Issues

Ability to include GA without GTM

Currently, the plugin is set up to require you to add Google Tag Manager to use Google Analytics. The plugin should be able to inject Google Analytics without Google Tag Manager

'yarn add gatsby-plugin-google-marketing-platform' causes an error

Hey!

Just tried to install this awesome plugin and got an error:

yarn add gatsby-plugin-google-marketing-platform
error An unexpected error occurred: "expected hoisted manifest for \"gatsby-plugin-google-marketing-platform#gatsby#babel-preset-gatsby\"". info If you think this is a bug, please open a bug report with the information provided in "/Users/oleg.obidin/WebstormProjects/my-project/yarn-error.log". info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

The yarn-error.log content is:

Arguments:
  /usr/local/Cellar/node/11.14.0/bin/node /usr/local/Cellar/yarn/1.15.2/libexec/bin/yarn.js add gatsby-plugin-google-marketing-platform

PATH:
  /Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Users/oleg.obidin/google-cloud-sdk/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS:/Users/oleg.obidin/WebstormProjects/my-project/node_modules/.bin

Yarn version:
  1.15.2

Node version:
  11.14.0

Platform:
  darwin x64

Trace:
  Invariant Violation: expected hoisted manifest for "gatsby-plugin-google-marketing-platform#gatsby#babel-preset-gatsby"
      at invariant (/usr/local/Cellar/yarn/1.15.2/libexec/lib/cli.js:2168:15)
      at _loop2 (/usr/local/Cellar/yarn/1.15.2/libexec/lib/cli.js:94162:9)
      at PackageHoister.init (/usr/local/Cellar/yarn/1.15.2/libexec/lib/cli.js:94222:7)
      at PackageLinker.getFlatHoistedTree (/usr/local/Cellar/yarn/1.15.2/libexec/lib/cli.js:48042:20)
      at PackageLinker.<anonymous> (/usr/local/Cellar/yarn/1.15.2/libexec/lib/cli.js:48053:27)
      at Generator.next (<anonymous>)
      at step (/usr/local/Cellar/yarn/1.15.2/libexec/lib/cli.js:304:30)
      at /usr/local/Cellar/yarn/1.15.2/libexec/lib/cli.js:322:14
      at new Promise (<anonymous>)
      at new F (/usr/local/Cellar/yarn/1.15.2/libexec/lib/cli.js:5228:28)

What do I do wrong?

custom config for analytics

Hey,
we get the following error warning in the 'Google Tag Assistant'-Plugin:
Analytics configuration mismatch
to fix this warning we need to include custom properties in the analytics config
(described here: https://support.google.com/optimize/thread/7578779?hl=en)

So the config should look something like this:
gtag('config', 'TEST-1234', {"optimize_id":'ASDF-4321', 'linker':['myDomain.com']})

GA Debug showing "Initializing Google Analytics." twice

Not sure if I'm doing something wrong, but after adding this plugin, I added 2 tags in Google Tag Manger:

  1. Google Analytics tag that fires on every page load
  2. Google Optimize tag

Apart from that things seem to be running fine. Is this an issue? Have I done something wrong?

Update Readme

yarn install name is deprecated and the new command is yarn add name.

  • Change line 24 in README.md to yarn add gatsby-plugin-google-marketing-platform

Just a very minor issue, to a great plugin :)

Global Site Tag

This Tag Manager Support Doc expains that the difference between Tag Manager and global site tags.

Based on what Google Analytics admin gives me to paste into the <head />, the google tag manager link looks like this:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXX"></script>

But when I use this gatsby plugin, the script in the <head /> is:

<script async src="https://www.googletagmanager.com/gtm.js?id=GTM-XXX"></script>
Additionally, this script passes gaPropertyId into the window.dataLayer array in the subsequent script(s).

I can't get tracking to work using this plugin. Could it be due to this discrepancy?

Google Optimize Diagnostic Warning

Hey.. thanks for the plugin it save me some work to just add the plugin but I get the followings warning by the google optimize diagnostic:

We found problems with the Optimize installation on this page
Refer to the diagnostics below to fix problems and troubleshoot any issues with your installation. Learn more about diagnostics .
Warnings
The anti-flicker snippet timed out
The anti-flicker snippet might not have been configured correctly or the timeout value is too short. The anti-flicker timeout is a worst-case scenario. Your page will only be hidden for the full timeout if the experiment cannot execute sooner (and when it does, the page is unhidden immediately). When anti-flicker times out, your experiment doesn't run on that page load, which can limit the efficacy of your experiment and results. Regardless of timeout value, ensure that the Optimize plugin is deployed correctly and not late in the page (e.g. in the <BODY>).

Anti-flicker snippet configuration error
The Optimize container is loaded via Google Tag Manager. The anti-flicker snippet should be configured to use the Tag Manager container ID (GTM-XXX), not the Optimize container ID (GTM-YYY). While this won't cause a problem when always firing the Optimize tag, it can adversely affect your site performance when you stop firing it, or if you fire it conditionally. In this case the best practice is to use the Tag Manager container ID in the anti-flicker snippet.

Suggestions
Anti-flicker snippet timed out too fast
The anti-flicker snippet timeout value is too short. When anti-flicker times out your experiment doesn't run, decreasing the amount of visitors that will see it. The anti-flicker timeout value is designed as a worst case scenario, not the actual time that your page will be hidden.

It seems you have to use the tag manager id in the antiflicker ..
Also an option for the anti flicker timeout could be added .. what do you think?
Its this something would you like to address in your plugin ?

React 18 support

Hey there @colbyfayock,

Could we add react 18 to the support peer deps?
I've been using this with React 18 for a few weeks now and not had any issues.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.