Socket
Socket
Sign inDemoInstall

@phntms/next-gtm

Package Overview
Dependencies
Maintainers
4
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@phntms/next-gtm

A lightweight Next library to implement custom Google Tag Manager events.


Version published
Maintainers
4
Created
Source

@phntms/next-gtm

NPM version Actions Status PR Welcome

A lightweight Next library to implement custom Google Tag Manager events.

Introduction

Extends @phntms/react-gtm with native support for Next.JS 12, utilizing the new Script Component to automatically prioritize loading of third-party scripts to improve performance.

Installation

Install this package with npm.

npm i @phntms/next-gtm

Usage

<TrackingHeadScript />

PropertyTypeDefaultNotes
idstringundefinedID that uniquely identifies GTM Container. Example format: GTM-xxxxxx.
disablebooleanfalseUsed to disable tracking events (isGTM=false only). Use if you want user to consent to cookies first
isGTMbooleanfalseLoads the gtag.js script by default (legacy behaviour - compatible with UA/GA4/GTM), else, loads gtm.js.
GTMAuthstringundefined(isGTM = true required) Optional parameter to load a non-default GTM environment, e.g. for testing GTM.
GTMPreviewstringundefined(isGTM = true required) Optional parameter to load a non-default GTM environment, e.g. for testing GTM.

To initialize GTM, add TrackingHeadScript to the head of the page.

This package utilizes next/script, which means you can't place it inside a next/head. Further, TrackingHeadScript should not be used in pages/_document.js as next/script has client-side functionality to ensure loading order.

The isGTM, GTMAuth and GTMPreview optional properties are a backwards-compatible update to provide multiple GTM environment support to this library. Using multiple GTM container environments allow developers to test different GTM config versions on a preview codebase before publishing the change to the production GTM container.

  • If the project is known to be using GTM - the isGTM should be set to true regardless of the environment (the default false will continue to function though).
  • Typically you want to set the GTMAuth and GTMPreview properties (obtained from the GTM preview environment script snippet) via optional environment variables in the applications' preview environments - when left as undefined - GTM will default to the master (production) container config.

Example usage:

import type { AppProps } from "next/app";
import { TrackingHeadScript } from "@phntms/next-gtm";

const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_TRACKING_ID || "";

const App = ({ Component }: AppProps) => (
  <>
    <TrackingHeadScript id={GA_TRACKING_ID} isGTM={true} />
    <Component />
  </>
);

export default App;

Note: If used alongside any cookie consent scripts, <TrackingHeadScript /> must be loaded after.

Using trackEvent() and enableTracking()

For how to use trackEvent(), enableTracking(), learn more about EventDataProps and how this library extends window.dataLayer, please reference @phntms/react-gtm.

🍰 Contributing

Want to get involved, or found an issue? Please contribute using the GitHub Flow. Create a branch, add commits, and open a Pull Request or submit a new issue.

Please read CONTRIBUTING for details on our CODE_OF_CONDUCT, and the process for submitting pull requests to us!

Keywords

FAQs

Package last updated on 18 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc