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

@os-team/gtm

v1.1.7

Published

The library for working with Google Tag Manager.

Downloads

42

Readme

@os-team/gtm NPM version BundlePhobia

The library for working with Google Tag Manager.

Implements the official Google Tag Manager Guide.

Usage

Step 1. Install the package

Install the package using the following command:

yarn add @os-team/gtm

Step 2. Initialize Google Tag Manager

2.1. Simple case

Call the init method and pass the ID of your GTM container.

import GoogleTagManager from '@os-team/gtm';

GoogleTagManager.init('GTM-XXXXXXX');

For example:

import React, { useEffect } from 'react';
import GoogleTagManager from '@os-team/gtm';
import AppRouter from './AppRouter';

const App: React.FC = () => {
  // Initialize Google Tag Manager
  useEffect(() => {
    GoogleTagManager.init('GTM-XXXXXXX');
  }, []);

  return <AppRouter />;
};

export default App;

If you use Next.js we recommend initializing Google Tag Manager in the ./pages/_app.tsx file:

import React, { useEffect } from 'react';
import GoogleTagManager from '@os-team/gtm';

const App: React.FC = ({ Component, pageProps }) => {
  // Initialize Google Tag Manager
  useEffect(() => {
    GoogleTagManager.init('GTM-XXXXXXX');
  }, []);

  return <Component {...pageProps} />;
};

export default App;

2.2. Set up the data layer

You can configure data layer variables when initializing the Google Tag Manager like this:

GoogleTagManager.init('GTM-XXXXXXX', {
  dataLayer: {
    userId: '1',
  },
});

In this case, the code of the GTM will be inserted after the data layer code, as it should be.

2.3. Rename the data layer

By default, the name of the data layer is dataLayer. If you want, you can change this name to your own.

GoogleTagManager.init('GTM-XXXXXXX', {
  dataLayerName: 'customDataLayer',
});

2.4. The debug mode

By default, the Google Tag Manager will be inserted into the HTML code only in the production environment. If you want to test it, you should enable the debug mode.

GoogleTagManager.init('GTM-XXXXXXX', {
  debug: true,
});

You can also set up the environment variables as follows:

GoogleTagManager.init('GTM-XXXXXXX', {
  debug: true,
  env: {
    auth: 'XXXXXXXXXXXXXXXXXXXXXX',
    preview: 'env-1',
  },
});

For more information about the environments feature in Google Tag Manager, see this help page.

2.5. Multiple containers on a page

You can insert more than one container on a page as follows:

import React, { useEffect } from 'react';
import GoogleTagManager from '@os-team/gtm';
import AppRouter from './AppRouter';

const App: React.FC = () => {
  useEffect(() => {
    GoogleTagManager.init('GTM-XXXXXX1');
    GoogleTagManager.init('GTM-XXXXXX2');
  }, []);

  return <AppRouter />;
};

export default App;

For the best performance, initialize as few GTM containers as possible.

For more information about using multiple containers on a page, see Google Tag Manager Guide / Multiple Containers on a Page.

Step 3. Fire events

To fire an event you should push the information about it to the data layer. This is accomplished by calling the push method as follows:

GoogleTagManager.push({
  event: 'event_name',
  variable_name: 'variable_value',
});

By default, the information will be added to the dataLayer layer, but you can change the layer name to your own:

GoogleTagManager.push(
  {
    event: 'event_name',
    variable_name: 'variable_value',
  },
  'customDataLayer'
);

For more information about using the data layer, see Google Tag Manager Guide / Using the Data Layer with HTML Event Handlers.