2.0.11 • Published 4 years ago

react-gtm-module v2.0.11

Weekly downloads
195,562
License
MIT
Repository
github
Last release
4 years ago

Build Status npm version npm downloads

react-gtm-module

React Google Tag Manager Module

This is a Javascript module to React based apps that implement Google Tag Manager. It is designed to use GTM snippet.

You can easily use custom dataLayer, multiple dataLayers and additional events.

Installation

npm:

npm install react-gtm-module --save

Usage

Initializing GTM Module:

import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'

...
import TagManager from 'react-gtm-module'

const tagManagerArgs = {
    gtmId: 'GTM-000000'
}

TagManager.initialize(tagManagerArgs)
...

const app = document.getElementById('app')
ReactDOM.render(<Router routes={routes} />, app)

DataLayer

Custom dataLayer example:

import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'

...
import TagManager from 'react-gtm-module'

const tagManagerArgs = {
    gtmId: 'GTM-000000',
    dataLayer: {
        userId: '001',
        userProject: 'project'
    }
}

TagManager.initialize(tagManagerArgs)
...

const app = document.getElementById('app')
ReactDOM.render(<Router routes={routes} />, app)

Multiple dataLayer example:

If you need send multiple custom dataLayer you can initialize GTM Module on different components sending different dataLayers

You can initialize it normally:

import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'

...
import TagManager from 'react-gtm-module'

const tagManagerArgs = {
    gtmId: 'GTM-000000',
    dataLayerName: 'PageDataLayer'
}

TagManager.initialize(tagManagerArgs)
...

const app = document.getElementById('app')
ReactDOM.render(<Router routes={routes} />, app)

And send your data in each page you want

import React from 'react'

...
import TagManager from 'react-gtm-module'

const tagManagerArgs = {
    dataLayer: {
        userId: '001',
        userProject: 'project',
        page: 'home'
    },
    dataLayerName: 'PageDataLayer'
}
...

const Home = () => {
    ...
    TagManager.dataLayer(tagManagerArgs)
    ...

    return (
        <div className='home'>
            //your component code
        </div>
    )
}

export default Home

Events

Example:

import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'

...
import TagManager from 'react-gtm-module'

const tagManagerArgs = {
    gtmId: 'GTM-000000',
    events: {
        sendUserInfo: 'userInfo'
    }
}

TagManager.initialize(tagManagerArgs)
...

const app = document.getElementById('app')
ReactDOM.render(<Router routes={routes} />, app)

Environments

Configure how Tag Manager will works between development and production server environments.

Example:

import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'

...
import TagManager from 'react-gtm-module'

const tagManagerArgs = {
    gtmId: 'GTM-000000',
    auth: '6sBOnZx1hqPcO01xPOytLK',
    preview: 'env-2'
}

TagManager.initialize(tagManagerArgs)
How can I find auth and preview?

Go to Google Tag Manager -> ADMIN -> Environments -> Actions -> Get Snippet. Look for gtm_auth and gtm_preview

Don't know to use GTM environments?
ValueTypeRequiredNotes
gtmIdStringYesGTM id, must be something like GTM-000000.
dataLayerObjectNoObject that contains all of the information that you want to pass to Google Tag Manager.
dataLayerNameStringNoCustom name for dataLayer object.
eventsObjectNoAdditional events such as 'gtm.start': new Date().getTime(),event:'gtm.js'.
authStringNoused to set environments.
previewStringNoused to set environments, something like env-00.

Note:

  • Disabling javascript in the browser can prevent the correct operation of this library if React is only being rendered on the client side.

  • Before implementing GTM in your application ensure that you have at least one published container, otherwise Google Tag Manager snippet will return 404.

liverpool-taggeo-libpwa-femetro2-front-componentscirplus-frontendvaluecouponhelp-centre-uienotarylog-clientbonhams-signup-widgetvulcan-cra-starter@identance/sdkconquista-componentesmicurai-pluginmicurai-plugin-democaptain-dashboardsmidland-property-detailnatana-web-uiaidexa-open-dsspot-form@airstudio/trackersfeathery-react@infinitebrahmanuniverse/nolb-react-gt@parkhub/parkhub-uihkp-sell-online@everything-registry/sub-chunk-2558tangnextjstestertest-logic-nxweb3-transaction-watcherwork-permit-spa-v3wp-component-librarysandbox-appresufoliostores_in_store_test_npmstrapi-gtm-modulesecondstep-marketingtestsupplychain2@apolitical/gtmovp-uimhydiport-nextmetrocuadrado-front-componentsmetrocuadrado-uiledn-appparax-aavekoumila-corekonverto-apollo-scriptkonverto-apolloexpert-scriptkonverto-bot-scriptkonverto-health-scriptkonverto-healthexpert-scripthsv4_extranethorizon-analyticskdan-cloudkiskadi-widgetgng-ecomm-sdkmy-smart-payroll-calculatormashreqdigitaluiparax-nemaparax-paraspaceplus-blocksplatonist-libraryo2web-react-corenext-bootstrapplatform-frontendnapa-react-coresignup-widget-ndc-bonhamssky-design-systemticketmelon-webtrekhaak-labels-configuratortrekhaak-montage-configuratortrillion-viewer@ankr.com/common@avlutils/frontend-general@avlutils/frontend-mobile@alamar/kit@axa-ch/pod-eliah-vorsorgeportal@axa-ch/pod-myaxa@axa-ch/pod-myaxa-nextrel@axa-ch/alt-pod-myaxa@cere/games-sdk@cleverlyworks/industry-ui@dica/libs@consent-manager/integration-google-tag-managerurbanpiper-engineering-meraki-sdk@feathery/react@groksmith/mychoice-sdk-modules@hubql/client@halo-media/spartan-express-checkout@identance/websdk@grace-studio/graceful-next@mychoice/mychoice-sdk-modules@nextj/kit@neferett/videojs-gtm@nbcom/services@navikt/nav-dekoratoren@modernsoftwares/adhi-research-platform@modernsoftwares/kids-study-research-platform@kompasid/web-components@kompas/web-components@raisehq/onboarding@qorebase/app-runtime
2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

5 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago