Skip to content
/ ga-gtag Public

ES module for the Google Analytics Global site tag

License

Notifications You must be signed in to change notification settings

idmadj/ga-gtag

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Global site tag (gtag.js) - Google Analytics

npm npm bundle size (minified)

ES module for the Google Analytics Global Site Tag.

Install

$ npm install --save ga-gtag

Usage

import { gtag, install } from 'ga-gtag';

// Substitute your tracking ID (begins with "G-", "UA-", "AW-" or "DC-")
install('G-##########'); 

install() adds the Global Site Tag script element into <head>. It only needs to be called once, but can safely be called multiple times (the script won't be re-added).

install() also has a second, optional argument that can be used to specify additional config info for the initialization (for instance to disable pageview measurement):

install('G-##########', { 'send_page_view': false });

From there, gtag() can be used as specified in Google's documentation:

gtag('event', 'login', { 'method': 'Google' });

If you need to call gtag() before the Global Site Tag script is installed (for instance, to manage consent settings in consent mode), import and call initDataLayer() first:

import { gtag, initDataLayer, install } from 'ga-gtag';

initDataLayer();
gtag('consent', 'default', {
  'ad_storage': 'denied',
  'analytics_storage': 'denied'
});
install('G-##########');

About

ES module for the Google Analytics Global site tag

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published