1.0.3 • Published 6 years ago

google-tag-manager-fn v1.0.3

Weekly downloads
173
License
ISC
Repository
github
Last release
6 years ago

google-tag-manager-fn

google-tag-manager-fn is google tag manager functional management module. You can add gtm container to your web app via this module. Here are some functions that you can use for the various functionalities of gtm.

Installation

npm install --save google-tag-manager-fn

Usage

Import the module to your code

import GTM from 'google-tag-manager-fn'; // Adds functionalities to GTM variable

Functions

init({ id, preview, auth, events, dataLayerName, dataLayer, reactFlag })

GTM.init({
    id: 'GTM-abc', 
    preview: '', 
    auth: '', 
    events: {}, 
    dataLayerName: 'dataLayer', 
    dataLayer: {}, 
    reactFlag: false,
})

Folowing are the options to be passed :

optionused forrequireddefault value
idGTM container Idyes
previewPreviewing containerno''
authAuth key for using gtmno''
eventsFor Adding events to datalayer for tags to be firedno{}
dataLayerNameData Layer Key for Gtm to look for datanodataLayer
dataLayerobject for adding data to datalayer variableno{}
reactFlagFor returning react components to render in your app as script with dangerouslySetInnerHTMLnofalse

appendDL({ dataLayerName, dataLayer, state, reactFlag, event })

GTM.appendDL({
    dataLayerName: 'dataLayer', 
    dataLayer: {
        value: 'gtm variable',
    }, 
    state: 1,
    reactFlag: false,
    event: 'gtm-value',
})

Folowing are the options to be passed :

optionused forrequireddefault value
dataLayerNameData Layer Key for Gtm to look for datanodataLayer
dataLayerobject for adding data to datalayer variableno{}
state0 : data without event, 1 : data with eventno0
reactFlagFor returning react components to render in your app as script with dangerouslySetInnerHTMLnofalse
eventEvent Name for dataLayer variableno'DLChanged'

fireEvent({ event, data, elementId })

GTM.fireEvent({
    event: 'gtm-event',
    data: {
        value: 'gtm event value',
    }, 
    elementId: '',
})

Folowing are the options to be passed :

optionused forrequireddefault value
eventEvent Name for to be firedno'custom-event'
dataobject for passing it to the custom eventno{}
elementIdstring Id for the event to be attached to or in default case it attaches event to document objectno''

GTM to Catch Events

Firing Tags

You can add Page Views Tags or Tags on the basis of data layer variable values. You can All Page Route Change trigger for your react app. to check for the tag firing conditions.

Events Capture

You can add Custom Html Tag for capturing events that you are firing from your app using this module. (CustomEvent : object should be supported by your browser version.)

<script>
document.addEventListener('ga-event', function(ev){
    // Your data that you have passed from the app in your event will be available in ev.detail.
    console.log(ev.detail); // You data is logged on console.
});
</script>

If you have your app server side rendering try using functions in your user interactive functions or componentDidMount which runs only on browser as the document object might not be present on server side. And use reactFlag option as true for adding the component to your component render function.

Links

You can visit some links for reference:

Organisation

Gradeup