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

@redsift/js-lib-google-analytics

v1.3.0

Published

Setup Google Analytics with cross-linked projects and more.

Downloads

6

Readme

js-lib-google-analytics

js-lib-google-analytics is a helper library to setup Google Analytics via analytics.js. It provides:

  • Setup for sending of events to a single or multiple Google Analytics projects.
  • Setup of cross domain linking.
  • Allows to enable IP anonimization.
  • Optional configuration for the autotrack.js plugins cleanUrlTracker, pageVisibilityTracker and urlChangeTracker.

Setup

Prerequisites: analytics.js and (optionally) autotrack.js

analytics.js is Google's official tracking library, providing the global ga object which this repository helps to configure. Please use the installation instructions to setup analytics.js.

Optionally you can install the autotrack.js library, which, according to its Github repository, does "Automatic and enhanced Google Analytics tracking for common user interactions on the web.". Check the repository documentation for more information and see the next step on how to configure a selection of autotrack plugins via js-lib-google-analytics.

Usage

Default usage

To use the default configuration which comes with the library use the following code:

import setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';

const config = {
    uaProjectId: 'UA-PROJECT-ID',
    ...getDefaultProjectSetup(),
};

setupGoogleAnalytics(config);

Using getDefaultProjectSetup() yields this configuration:

{
    anonymizeIp: true, // enable IP anonimization
    cookieName: '_ga',
    anonymizeIp: true,
    userId: null, // if set the `userId` will be set for the tracker, see https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#userId
    userId: null, // if set the `userId` will be set for the tracker, see https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference?hl=en#clientId
    clientId: null,
    autoLink: [], // don't configure cross domain linking 
    sendInitialPageView: true, // send an initial page view when using `setupGoogleAnalytics()`
    autoTrack: {
      // configure the `cleanUrlTracker` autotrack plugin with this configuration
      // see https://github.com/googleanalytics/autotrack/blob/master/docs/plugins/clean-url-tracker.md        
      cleanUrlTracker: {
        stripQuery: true,
        queryDimensionIndex: 1,
        indexFilename: 'index.html',
        trailingSlash: 'remove',
      },
      // enable the `urlChangeTracker` autotrack plugin
      // see https://github.com/googleanalytics/autotrack/blob/master/docs/plugins/url-change-tracker.md#differentiating-between-virtual-pageviews-and-the-initial-pageview
      urlChangeTracker: true, // configure the `urlChangeTracker` autotrack plugin with this configuration
    },
}

You can use this configuration as a base for your own customization.

NOTE: If you don't provide a name property to the config the tracker name will be derived from the uaProjectId in removing all -.

Sending events to multiple projects

import setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';

const configs = [{
    uaProjectId: 'UA-PROJECT-ID-0',
    ...getDefaultProjectSetup(),
}, {
    uaProjectId: 'UA-PROJECT-ID-1',
    ...getDefaultProjectSetup(),
}];

setupGoogleAnalytics(configs);

This will send events to each configured projects. A use case is to have a GA project which only collects events for a single domain, and a second project which collects events from all your domains, including the one you are using @redsift/js-lib-google-analytics for (in this case it is useful to setup cross domain linking, see below).

Cross domain linking

import setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';

const config = {
    uaProjectId: 'UA-PROJECT-ID',
    ...getDefaultProjectSetup(),
    autoLink: ['blog.myproject.at', 'docs.myproject.at'],
};

setupGoogleAnalytics(config);

This will provide pageview and URL information for the domains referenced in autoLink in the UA-PROJECT-ID project.

Usage without autotrack.js

import setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';

const config = {
    uaProjectId: 'UA-PROJECT-ID',
    ...getDefaultProjectSetup(),
    autoTrack: null,
};

setupGoogleAnalytics(config);

This will provide setup analytics.js but will skip the configuration of autotrack.js.

Trigger events for all configured projects

After you called setupGoogleAnalytics() you can send events to all of them with a single command. Please note that this command only supportes 3 parameters max:

import setupGoogleAnalytics, { gaAll } from '@redsift/js-lib-google-analytics';

// ... setup you projects

gaAll('send', 'pageview'); // Sends a pageview event to all configured projects.

Configure a session based cookie (which immediately expires)

See https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#cookie_expiration for more information.

To create a permanent cookie after initializing the session based on you can call setupGoogleAnalytics() without the temporarySession config parameter. The clientId for the cookie will stay the same in this case, so your GA project will treat the two sessions as the same user.

import setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';

const temporarySessionConfig = {
    uaProjectId: 'UA-PROJECT-ID',
    ...getDefaultProjectSetup(),
    temporarySession: true,
};

setupGoogleAnalytics(temporarySessionConfig);

// To make the cookie permanent call the setup function again without `temporarySession`:

const config = {
    uaProjectId: 'UA-PROJECT-ID',
    ...getDefaultProjectSetup(),
};

setupGoogleAnalytics(config);

Get a list of the configured project names

import setupGoogleAnalytics, { getDefaultProjectSetup, getProjectNames } from '@redsift/js-lib-google-analytics';

const config = [{
    uaProjectId: 'UA-PROJECT-ID-0',
    ...getDefaultProjectSetup(),
}, {
    uaProjectId: 'UA-PROJECT-ID-1',
    ...getDefaultProjectSetup(),
}];

setupGoogleAnalytics(config);

console.log(getProjectNames()); // Returns a Set of project names, here: ['UA-PROJECT-ID-0' 'UA-PROJECT-ID-1']

console.log(getProjectNames({ asArray: true })); // Returns an Array of project names, here: ['UA-PROJECT-ID-0' 'UA-PROJECT-ID-1']

Set a custom project name for the tracker

import setupGoogleAnalytics, { getDefaultProjectSetup, getProjectNames } from '@redsift/js-lib-google-analytics';

const config = {
    uaProjectId: 'UA-PROJECT-ID-0',
    name: 'MyTracker',
    ...getDefaultProjectSetup(),
};

setupGoogleAnalytics(config);

console.log(getProjectNames()); // ['MyTracker']