• Stars
    star
    192
  • Rank 202,019 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This repository contains a react based implementation for Google's Tag Manager snippet.

NPM Version Build Status Dependencies

react-google-tag-manager

This repository contains a react based implementation for Google's Tag Manager snippet.

Motivation & Expectation

We like to use Google's Tag Manager in our React Stack. We wrap it into a component because it makes it testable. And based on our agreement every component needs to be tested.

Other requirements for the GTM implementation are:

  1. provide a GTM-ID
  2. provide additional events on script initialization (optional)
  3. provide a name for the dataLayer (optional)
  4. can be used for server-side-rendering and client-side-rendering
  5. contains tests
  6. installable via npm

How to use

To use it in your project run npm i react-google-tag-manager. It could be used like the following example:

import React from 'react';
import gtmParts from 'react-google-tag-manager';

class GoogleTagManager extends React.Component {
    componentDidMount() {
        const dataLayerName = this.props.dataLayerName || 'dataLayer';
        const scriptId = this.props.scriptId || 'react-google-tag-manager-gtm';

        if (!window[dataLayerName]) {
            const gtmScriptNode = document.getElementById(scriptId);

            eval(gtmScriptNode.textContent);
        }
    }

    render() {
        const gtm = gtmParts({
            id: this.props.gtmId,
            dataLayerName: this.props.dataLayerName || 'dataLayer',
            additionalEvents: this.props.additionalEvents || {},
            previewVariables: this.props.previewVariables || false,
            scheme: this.props.scheme || 'https:',
        });

        return (
            <div>
                <div>{gtm.noScriptAsReact()}</div>
                <div id={this.props.scriptId || 'react-google-tag-manager-gtm'}>
                    {gtm.scriptAsReact()}
                </div>
            </div>
        );
    }
}

GoogleTagManager.propTypes = {
    gtmId: React.PropTypes.string.isRequired,
    dataLayerName: React.PropTypes.string,
    additionalEvents: React.PropTypes.object,
    previewVariables: React.PropTypes.string,
    scriptId: React.PropTypes.string,
    scheme: React.PropTypes.string,
};

export default GoogleTagManager;

You can render this later simply by

// inside the render method where you want to include the tag manager
<GoogleTagManager gtmId='GTM-12345' />

// or with all optional parameters
const event = { platform: 'react-stack' }

<GoogleTagManager gtmId='GTM-12345' scriptId='gtm-script-container' dataLayerName='dl-backup' additionalEvents={event} previewVariables='' scheme='https:' />

In this example the google tag manager id, the dataLayer name, additional events and the script id where gtm script should be mounted are configurable through props:

prop required default value
gtmId yes
dataLayerName no dataLayer
additionalEvents no {}
scriptId no react-google-tag-manager-gtm
previewVariables no false
scheme no https:

Notes:

  • The componentDidMount part is required as the script contents itself would not be executed otherwise on the client side
  • As eval can be used to do harm, make sure that you are understanding what you are doing here and read through the script that is evaluated
  • Additionally this module exports noScriptAsHTML() and scriptAsHTML() which return a simple HTML string
  • gtmParts takes the following arguments:
argument keys required default value
id yes
dataLayerName no dataLayer
additionalEvents no {}
scheme no ``
previewVariables no false

More Repositories

1

Permissify

Simplifying Android Permissions
Java
361
star
2

nix-remote-builder

Easy way to set up a linux remote builder in docker
Shell
28
star
3

RecyclerViewInjectorAdapter

RecyclerView.Adapter on steroids
Java
15
star
4

nix-venient

A collection of convenient commands for working with nix/nixpkgs
Shell
12
star
5

lean-jira-export

Allows to export jira issues based on configurable JQL including lean metrics data such as lead-time & cycle-time
Java
10
star
6

gemini-ci-gui

CI GUI for Gemini
JavaScript
10
star
7

easy-akka-http

Additional things we use to make life with akka-http easier
Scala
8
star
8

healthcheck-ping

express health check middleware
JavaScript
8
star
9

auth0-bundler

Bundle rules, scripts and hooks to deploy them to Auth0.
JavaScript
8
star
10

amqp-akka-streams

A library that allow you to leverage Akka Streams while reading or writing messages to AMQP broker.
Scala
8
star
11

tech-blog

HolidayCheckers blogging about things they care about
HTML
8
star
12

marathon-maven-plugin

Maven plugin for interacting with Marathon
Java
6
star
13

frontend-assignment

Homework task for FrontEnd recruiting process
5
star
14

mesos-in-the-box

Apache Mesos + Marathon + Haproxy ran on single host as Docker containers.
Shell
5
star
15

react-ssi-fragment

TypeScript
4
star
16

gemini-json-reporter

Gemini plugin that generates a single JSON report file.
JavaScript
4
star
17

sanitize-marathon-app-id

🚮 Sanitize marathon app ids.
JavaScript
4
star
18

eslint-config-holidaycheck

ESLint configuration from holidaycheck
JavaScript
3
star
19

gcp-quota-exporter

Prometheus exporter for Google Cloud Platform resource quota
Python
3
star
20

car_pooling_bot

this app is built to make Business Travels Carpooling much easier, it's built with Interactivity user interface
JavaScript
2
star
21

morning-katas

This repo contains the schedule, instructions and all kinds of links for the #morningKata as we do them in HolidayCheck.
2
star
22

apprenticeship

The HolidayCheck Apprenticeship Page
Nix
2
star
23

redux-pre-thunk

Pre thunk middleware for unit testing
JavaScript
2
star
24

coding-katas

JavaScript
1
star
25

koa-prevent-caching

Set appropriate response headers to prevent caching.
JavaScript
1
star
26

delta-detect-links

Detect links in a delta object.
JavaScript
1
star
27

google-analytics-persister

Read data from google analytics and persist them in Time Series Database (InfluxDB)
JavaScript
1
star
28

push-docker-image

⬆️Push docker image .tar.gz files to a docker registry.
JavaScript
1
star
29

react-meetup-intro

Into talk for the ReactJS Munich Meetup 22.02.2016
JavaScript
1
star
30

issue-prioritizer

Set priority of github issue based on potential, weight and effort metrics
JavaScript
1
star
31

hc-carousel-component

A reusable carousel webcomponent. Tries not to be opinionated, tries to be just like a (new) HTML element.
JavaScript
1
star
32

liam

Simple tool for running tasks against your GitHub repository and/or other services.
JavaScript
1
star
33

hc-event-component

A web-component containing all the things that (we think) represent an event.
HTML
1
star