@chetwoodfinancial/gtm-tracker

1.0.6 • Public • Published

GTM Tracker

Overview

A utility module to make tracking DOM elements easier with Google Tag Manager.

Install

Install with your favoured package manager like:

npm install @chetwoodfinancial/gtm-tracker --save

yarn add @chetwoodfinancial/gtm-tracker

Or add manually to your package.json file.

About

GTM Tracker by default needs a collection of tag objects in order for it to work:

Example collection of 'Tag' objects

const GTMConfig = {
    tags : [
        {
            tagName: 'confirmButton',
            selector: '[data-gtm="confirmButton"]',
            eventType: 'click',
            GTMEvents: {
                event: 'confirmButton',
            },
        }
    ]
}

A tag object requires all of the above values by default however you can change the behaviour by writing your own eventRegistrar callback function and passing it to registerEvents as the second parameter.

'Tag' object attributes

tagName

Correlates to the Tag set up in GTM.

selector

Correlates to the element in the DOM you wish to track. I personally prefer to use a custom data attribute to do this as IDs and classnames may change over time and skew the data you are trying to collect.

eventType

The event listener type (click, load etc) that will be attached to the DOM element defined above. When the event such as a click is fired the cooresponding 'Trigger' in GTM should also fire.

Note - Only click events have been tested so far.

GTMEvents.event

Correlates to the 'Trigger' that should fire in GTM when the given eventType on the selector is fired.

Examples and usage

A simple example.

In your main javascript file you could have something that looks like.

// Import the module
import * as gtmTracker from '../gtm-tracker';

// An example configuration object for gtm-tracker

window.dataLayer = [];

var GTMConfig = {
    tags : [
        {
            tagName: 'confirmButton',
            selector: '[data-gtm="confirmButton"]',
            eventType: 'click',
            GTMEvents: {
                event: 'confirmButton',
            },
        }
    ]
};

window.gtmTracker = gtmTracker;
window.gtmConfig = GTMConfig;


(function(){
    return window.gtmTracker.registerEvents(window.gtmConfig.tags, window.gtmTracker.registerEvent);
})();

Then in your HTML add something that looks like this:

<button class="btn btn-lg" data-gtm="confirmButton">Confirm your selection</button>

Check out the fixtures/example.js file for an example.

Note: You must first have GTM loaded on your page (and a dataLayer variable) as well as having some actual tags set up in GTM for you to test. The GTM preview tool is a handy way to test your implementation. See https://developers.google.com/tag-manager/devguide for more info.

Tests

To run this modules test suite, clone the repo, install all of the dependencies with npm i and run:

npm test

Package Sidebar

Install

npm i @chetwoodfinancial/gtm-tracker

Weekly Downloads

1

Version

1.0.6

License

MIT

Unpacked Size

6.38 kB

Total Files

4

Last publish

Collaborators

  • hannah-collingwood-1
  • ciouhuei
  • rosschetwood
  • alex.unku
  • designer023
  • kurt-culley
  • jwarby
  • garethchetwood