npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@chetwoodfinancial/gtm-tracker

v1.0.6

Published

A utility module to make Google Tag Manager tracking easier for developers

Downloads

19

Readme

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