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

@pinjollist/next-with-analytics

v1.0.1

Published

Minimalistic analytics helper for Next.js which respects Do Not Track (DNT) headers.

Downloads

44

Readme

next-with-analytics

CircleCI Codecov

Minimalistic analytics helper for Next.js which respects Do Not Track (DNT) headers.

next-with-analytics is a tiny HOC that wraps a Next.js application with react-ga and some useful analytics helpers. It also allows you to respect Do Not Track (DNT) settings for visitors that have them enabled.

Other features include:

  • Anonymize IP support
  • Debug mode

Installation

# yarn
yarn add @pinjollist/next-with-analytics

# npm
npm install --save @pinjollist/next-with-analytics

Usage

Initialising analytics

The initAnalytics function will create an analytics instance with any options passed into it. To use it, create a custom _app page with the initAnalytics() called inside it. You can now use the analytics instance to create a route event listener for tracking pageviews, as well as passing analytics helper functions into all pages as a prop.

// pages/_app.js

import App from 'next/app';
import Router from 'next/router';
import { initAnalytics } from '@pinjollist/next-with-analytics';

const options = {
  trackingCode: process.env.GOOGLE_ANALYTICS,
  respectDNT: true,
};
const analyticsInstance = initAnalytics(options);

class MyApp extends App {
  componentDidMount() {
    const { handleRouteChange } = analyticsInstance;

    // Enable tracking page views for route change
    Router.events.on('routeChangeComplete', handleRouteChange);
  }

  componentWillUnmount() {
    const { handleRouteChange } = analyticsInstance;

    // Disable tracking page views for route change before unmount
    Router.events.off('routeChangeComplete', handleRouteChange);
  }

  render() {
    const { Component, pageProps } = this.props;
    // Add the Analytics helpers to all pages.
    const { analytics } = analyticsInstance;

    return (
      <Container>
        <Component analytics={analytics} {...pageProps} />
      </Container>
    );
  }
}

withAnalytics HOC

You can also use the withAnalytics HOC to easily wrap your entire app with next-with-analytics. To use it, wrap the _app page with the HOC, passing the next/router instance, and some preferred options.

IMPORTANT: Note that on Next.js 9 and above, this will disable the Automatic Static Optimization feature, since it will also try to modify getInitialProps in all pages. This HOC remains available to ensure backwards compatibility with Next.js 8.

// pages/_app.js

import App from 'next/app';
import Router from 'next/router';
import { withAnalytics } from '@pinjollist/next-with-analytics';

const options = {
  trackingCode: process.env.GOOGLE_ANALYTICS,
  respectDNT: true,
};
const nextWithAnalytics = withAnalytics(Router, options);

class MyApp extends App {
  static async getInitialProps({ Component, ctx }) {
    let pageProps = {};
    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }
    return { pageProps };
  }

  render() {
    const { Component, pageProps, analytics } = this.props;
    return (
      <Container>
        <Component analytics={analytics} {...pageProps} />
      </Container>
    );
  }
}

export default nextWithAnalytics(MyApp);

Analytics Instance

The initAnalytics() function creates an analytics instance with any options passed into it. It returns an AnalyticsInstance object, which contains the following:

analytics?: AnalyticsHelpers

An object containing all analytics helper functions. See Using Analytics helpers to learn more about these helper functions.

handleRouteChange: () => void

Used to handle analytics pageview tracking after route change. Use it with Router Events:

class MyApp extends App {
  // ...

  componentDidMount() {
    const { handleRouteChange } = analyticsInstance;

    // Enable tracking page views for route change
    Router.events.on('routeChangeComplete', handleRouteChange);
  }

  componentWillUnmount() {
    const { handleRouteChange } = analyticsInstance;

    // Disable tracking page views for route change before unmount
    Router.events.off('routeChangeComplete', handleRouteChange);
  }
}

Options

trackingCode?: string

The Google Analytics tracking code of your website. Default: ''

respectDNT?: boolean

Set to true to make the module respect Do Not Track (DNT). This will prevent react-ga to be initialised if your browser has DNT enabled. Default: false

anonymizeIp?: boolean

Set to true to enable _anonymizeIp in Google Analytics, an option which is often important for legal reasons (e.g. GDPR)

Using Analytics helpers

You can add the provided Analytics helpers directly into the analytics prop in the Next.js _app page. Typings for TypeScript projects are also available as WithAnalyticsState.

import Link from 'next/link';

function IndexPage({ analytics }) {
  const handleClick = () => {
    if (analytics) {
      analytics.event('Button Click', 'Signup to Platform');
    }
  };

  return (
    <Layout>
      <Container>
        <h1>Try out our platform!</h1>
        <Link href="/signup" onClick={handleClick}>
          <a>Signup Now</a>
        </Link>
      </Container>
    </Layout>
  );
}

List of Helpers

init

function init(trackingCode?: string | undefined): void

Initializes Next.js analytics.

pageview

function pageview(): void

Tracks pageview. Can be called e.g. on router location change.

event

function event(category?: string, action?: string): void

Sends a Google Analytics event.

exception

function exception(description?: string, fatal?: boolean): void

Sends a Google Analytics exception event for tracking exceptions.

Contributing

Contributions and Pull Requests welcome! Please read the Contributing Guidelines beforehand.

License

MIT © Resi Respati.