Coder Social home page Coder Social logo

max-sym / gatsby-plugin-gdpr-cookies Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andrezimpel/gatsby-plugin-gdpr-cookies

0.0 0.0 0.0 419 KB

Gatsby plugin to add google analytics, google tag manager and facebook pixel in a gdpr form to your site.

JavaScript 100.00%

gatsby-plugin-gdpr-cookies's Introduction

gatsby-plugin-gdpr-cookies

Gatsby plugin to add google analytics, google tag manager and facebook pixel in a GDPR form to your site.

Important: It now supports tracking right away without reloading the page. See here.

Install

npm install --save gatsby-plugin-gdpr-cookies

How to use

// in your gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-gdpr-cookies`,
      options: {
        googleAnalytics: {
          trackingId: 'YOUR_GOOGLE_ANALYTICS_TRACKING_ID', // leave empty if you want to disable the tracker
          cookieName: 'gatsby-gdpr-google-analytics', // default
          anonymize: true, // default
          allowAdFeatures: false // default
        },
        googleTagManager: {
          trackingId: 'YOUR_GOOGLE_TAG_MANAGER_TRACKING_ID', // leave empty if you want to disable the tracker
          cookieName: 'gatsby-gdpr-google-tagmanager', // default
          dataLayerName: 'dataLayer', // default
        },
        facebookPixel: {
          pixelId: 'YOUR_FACEBOOK_PIXEL_ID', // leave empty if you want to disable the tracker
          cookieName: 'gatsby-gdpr-facebook-pixel', // default
        },
        // defines the environments where the tracking should be available  - default is ["production"]
        environments: ['production', 'development']
      },
    },
  ],
}

How it works

First of all the plugin checks in which environment your site is running. If it's currently running in one of your defined environments it will add the tracking code by default to the <head>/<body> of your site. It will not be activated or initialized by this.

By default this plugin will not send any data to Google or Facebook to make it GDPR compliant. The user first needs to accept your cookie policy. By accepting that you need to set cookies for the tracker you want to use - gatsby-gdpr-google-analytics, gatsby-gdpr-google-tagmanager, gatsby-gdpr-facebook-pixel. Depending on the user input the value of each of the cookies should be true or false.

If the gatsby-gdpr-google-analytics cookie is set to true, Google Analytics will be initialized onClientEntry. Same is for the Google Tag Manager and Facebook Pixel. The plugin will check if cookies for Google Analytics or Facebook Pixel have been set between route changes on onRouteUpdate. Reloading the page after setting the cookies is not required anymore.

The page view will be tracked on onRouteUpdate.

Important: read below about using the plugin with Google Tag Manager.

Options

Google Analytics

trackingId

Here you place your Google Analytics tracking ID.

cookieName

You can use a custom cookie name if you need to!

allowAdFeatures

The default value is false.

anonymize

Some countries (such as Germany) require you to use the _anonymizeIP function for Google Analytics. Otherwise you are not allowed to use it. The option adds two blocks to the code:

ga('set', 'anonymizeIp', 1);

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:

import ReactGA from 'react-ga';

render() {
  return (
    <div>
      <ReactGA.OutboundLink
        eventLabel="myLabel"
        to="http://www.example.com"
        target="_blank"
        trackerNames={['tracker2']}
      >
        My Link
      </ReactGA.OutboundLink>
    </div>
  );
}

Google Tag Manager

trackingId

Here you place your Google Tag Manager tracking ID.

cookieName

You can use a custom cookie name if you need to!

dataLayerName

Data layer name

routeChangeEvent

The name of the event which will be triggered when route changes Defaults to gatsbyRouteChange

gtmAuth

Google Tag Manager environment auth string

gtmPreview

Google Tag Manager environment preview name

defaultDataLayer

Data layer to be set before GTM is loaded. Should be an object or a function that is executed in the browser, e.g.:

  defaultDataLayer: { platform: "gatsby" }
  defaultDataLayer: function() {
    return {
      pageType: window.pageType,
    }
  }

Tracking routes

Out of the box this plugin will simply load Google Tag Manager on the initial page/app load. It’s up to you to fire tags based on changes in your app.

This plugin will fire a new event called gatsbyRouteChange (by default) on Gatsby's onRouteUpdate (only if the consent was given by a visitor). To record this in Google Tag Manager, we will need to add a trigger to the desired tag to listen for the event:

In order to do that, go to Tags. Under Triggering click the pencil icon, then the ”+” button to add a new trigger. In the Choose a trigger window, click on the ”+” button again. Choose the trigger type by clicking the pencil button and clicking Custom event. For event name, enter gatsbyRouteChange. This tag will now catch every route change in Gatsby, and you can add Google tag services as you wish to it.

Facebook Pixel

pixelId

Here you place your Facebook Pixel ID.

cookieName

You can use a custom cookie name if you need to!

Initialize and track

This gatsby plugin now supports initializing and tracking right after a user accepts the cookie consent.

// in your cookie banner
import { useLocation } from "@reach/router" // this helps tracking the location
import { initializeAndTrack } from 'gatsby-plugin-gdpr-cookies'

Then you can execute initializeAndTrack(location) in your cookie banner callback. This will initialize the the plugin with your options from the gatsby-config.js and than starts tracking the user based on the cookies/services are accepted.

// in your cookie banner
initializeAndTrack(location)

Contributors

Thanks goes to these wonderful people who helped shaping this project.

Simon Vanherweghe

Stefan Tertan

Osvaldas Valutis

Théo

Stefan Tertan

gatsby-plugin-gdpr-cookies's People

Contributors

andrezimpel avatar coldfire87 avatar dependabot[bot] avatar osvaldasvalutis avatar simonvanherweghe avatar

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.