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

@pod-point/tracking.js

v6.0.0-alpha-4

Published

Wrapper for handling Segment tracking events

Downloads

1,698

Readme

tracking.js

npm Node.js CI

Wrapper for handling tracking events via Segment or GTM

Installation

npm install --save @pod-point/tracking.js

Deploy

To publish a new version to NPM run the following commands:

npm version [patch|minor|major]

This creates an automatic commit with the new version for the package.json, so be sure to push that commit before merging to main.

git push --tags

This pushes the version tag to Github, which you can see under the releases tab.

Usage

Either import the module in Node:

import { segment, gtm } from '@pod-point/tracking';

Or in the browser, create a new <script> tag to include the library on the page:

<script src="https://unpkg.com/@pod-point/tracking.js@<version>/dist/bundle.min.js"></script>

Init

Support by: Segment, GTM

For Segment, should only be used if the api key needs to be added dynamically. Otherwise use the snippet.

// Segment example
segment.init('segment-key');

// GTM example
gtm.init('gtm-id');

Track

Support by: Segment, GTM

tracking.track('Event name', {
    properties: 1,
});

Track Page

Support by: Segment

https://segment.com/docs/libraries/analytics.js/#page

Used to manually track a page view allowing to set the url.

tracking.trackPage('http://website.com/url?page=1');

Used to manually track a page view allowing to append more data to the default sent by the lib.

tracking.page({ label: 'label' });

Track Form

Support by: Segment

Used to track a form submit.

const form = document.querySelector('#form-to-track');
// The form fields you want to track
const fieldsToTrack = ['product', 'price'];
// The form fields which are only passed to the identify/alias call
const fieldsForIdentity = ['email', 'first_name', 'last_name'];

form.addEventListener('submit', e => {
    // Stop form from submitting automatically
    e.preventDefault();

    // Do anything else you want to do before the data is sent here, such as validation
    // ...

    tracking.trackForm('Event name', fieldsToTrack, fieldsForIdentity).then(() => {
        // Do anything you want to do after the tracking request has completed here, such as submitting the form
        // ...

        form.submit();
    });
});

Identify

Support by: Segment

Used to identify a user.

https://segment.com/docs/libraries/analytics.js/#identify

tracking.identify('[email protected]', {
    userParams: 1,
});

Alias

Support by: Segment

Used to alias a user.

https://segment.com/docs/libraries/analytics.js/#page

tracking.alias('[email protected]').then(() => tracking.identify('[email protected]', userDetails));

Reset

Support by: Segment

Used to delete the cookies/localStorage set by Segment, resetting the id and traits of the user.

https://segment.com/docs/sources/website/analytics.js/#reset-logout

tracking.reset();

Implementations

Browser environment

See browser.js.

This file registers custom event listeners on the DOM for all supported tracking actions.

In your application, you can then dispatch events as so:

document.dispatchEvent(
    // Segment example
    helpers.createCustomEvent('tracking.init.segment', {
        detail: {
            key,
        },
    })
    // GTM example
    helpers.createCustomEvent('tracking.init.gtm', {
        detail: {
            key,
        },
    })
);

document.dispatchEvent(
    helpers.createCustomEvent('tracking.track', {
        detail: {
            event,
            properties,
            options,
        },
    })
);

document.dispatchEvent(
    helpers.createCustomEvent('tracking.trackPage', {
        detail: {
            url,
        },
    })
);

This file is bundled up and available to reference in your application via this script tag:

<script src="https://unpkg.com/@pod-point/tracking.js@<version>/dist/browser.min.js"></script>

This separation away from making tracking calls directly within an applications JavaScript is with cookie compliance in mind. This library drops various cookies, and isolating that plus any tracking functionality to this external script will mean it is what is identified and blocked by our cookie management solution as opposed to our applications JavaScript which would likely result in breaking changes to its functionality.

Node environment

See node.js.

This file initiates an event manager / emitter with listeners for the following tracking actions: tracking.init, tracking.track, and tracking.trackPage.

In your application, you can then dispatch events as so:

import eventManager from './node';

eventManager.emit('tracking.init', key);
eventManager.emit('tracking.track', event, properties, options);
eventManager.emit('tracking.trackPage', url);

This can be useful for decoupling making tracking calls away from an applications business logic.