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 🙏

© 2026 – Pkg Stats / Ryan Hefner

gatsby-segment

v0.0.8

Published

Add Segment JS to gatsby site

Readme

Gatsby Segment Js - plugin

This plugin extends the core gatsby-plugin-segment-js and adds an option to stop the analytics.load() happening on page render. This allows you to create functionality for the web user to set there cookie preferences, in compliance with the EU Cookie Law.

A lightweight & feature-rich Gatsby plugin to easily add Segment JS snippet to your site.

Features

Packed with features:

  • use multiple write keys (one for prod env, another optional one for dev)
  • disable page view tracking (just in case you want to add it later manually)
  • up to date (Segment snippet 4.1.0)

Install

  • NPM: $ yarn add gatsby-segment-js
  • YARN: $ npm install --save gatsby-segment-js

How to use

Setup

In your gatsby-config.js file:

plugins: [
    {
        resolve: `gatsby-segment-js`,
        options: {
            // your segment write key for your production environment
            // when process.env.NODE_ENV === 'production'
            // required; non-empty string
            prodKey: `SEGMENT_PRODUCTION_WRITE_KEY`,

            // if you have a development env for your segment account, paste that key here
            // when process.env.NODE_ENV === 'development'
            // optional; non-empty string
            devKey: `SEGMENT_DEV_WRITE_KEY`,

            // boolean (defaults to false) on whether you want
            // to include analytics.page() automatically
            // if false, see below on how to track pageviews manually
            trackPage: false
            // change this to true for standard settings
            // or keep as false to call this manually
            // this effectively pauses the scripts and cookies form loading
            loadOnRender: false,
        }
    }
];

Track Events

Manually begin the load scripts by calling window.analytics.load(writeKey) If you have a more complex configuration which enables users to select between different levels of cookies (analytics / tracking / marketing) you can pass an object to load which sets individual apps.

analytics.load('writekey', { integrations: { All: false, 'Google Analytics': true, 'Segment.io': true } })

See Segment docs for more info

Track Events

If you want to track events, you simply invoke Segment as normal in your React components — (window.analytics.track('Event Name', {...}) — and you should see the events within your Segment debugger! For example, if you wanted to track events on a click, it may look something like this:

class IndexPage extends React.Component {
    ...
    _handleClick() {
        window.analytics.track("Track Event Fired", {
            userId: user.id,
            gender: 'male',
            age: 33,
        });
    }
    render() {
        return (
            <p>
                <Link onClick={this._handleClick} to="/">
                    Click here
                </Link>{" "}
                to see a track event
            </p>
        );
    }
}

Track Pageviews

If you want to track pageviews automatically, set trackPage to true in your gatsby-config.js file. What we mean by "automatically" is that whenever there is a route change, we leverage Gatsby's onRouteUpdate API in the gatsby-browser.js file (link) to invoke window.analytics.page() on each route change. But if you want to pass in properties along with the pageview call (ie, it's common to see folks pass in some user or account data with each page call), then you'll have to set trackPage: false and call it yourself in your gatsby-browser.js file, like this:

// gatsby-browser.js
exports.onRouteUpdate = () => {
    window.analytics && window.analytics.page();
};