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

@lightspeed/react-mixpanel-script

v0.4.13

Published

React Mixpanel for SSR pages

Downloads

77

Readme

@lightspeed/react-mixpanel-script

npm version

Introduction

Add and use Mixpanel in your React application.

Quick Start

  1. Install the dependency in your webapp.
yarn add @lightspeed/react-mixpanel-script
  1. In your serverside rendered document component, render the <MixpanelScript /> component in the <head /> of your document. It supports these properties:

| Property | Type | Description | | ---------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | mixpanelApiKey | string | Your app's mixpanel API Key, typically passed through with an environment variable, for example process.env.MIXPANEL_API_KEY | | nonce | string | A unique identifier to reference the analytics script for security, install our @lightspeed/express-csp-middleware package to implement | | mixpanelConfig | object? | Optional config to pass to the mixpanel.init function, see available config options |

In a Next.js app with serverside rendering for example, use the component as follows:

// app/_document.tsx
import React from 'react';
import Document, { Head, Main, NextScript, NextDocumentContext } from 'next/document';
import { MixpanelScript } from '@lightspeed/react-mixpanel-script';

type MyDocumentProps = {
  nonce: string;
};

export default class MyDocument extends Document<MyDocumentProps> {
  static async getInitialProps(ctx: NextDocumentContext) {
    // nonce comes from server.ts with our @lightspeed/express-csp-middleware package
    const { nonce } = (ctx.res as any).locals;
    const initialProps = await Document.getInitialProps(ctx);
    return { ...initialProps, nonce };
  }

  render() {
    return (
      <html>
        <Head>
          <title>Lightspeed - My App</title>
          <MixpanelScript
            nonce={this.props.nonce}
            mixpanelApiKey={process.env.MIXPANEL_API_KEY || ''}
          />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </html>
    );
  }
}
  1. In your application, add an Analytics component. This is used to provide and configure the Mixpanel context. It supports three arguments:

| Property | Type | Description | | ------------- | :----: | ------------------------------------------------- | | appName | string | A unique identifier for your application | | identity | string | A unique identifier for your user | | eventData | object | Data that will be sent with every generated event | | profileData | object | Data that is defined for the analytic profile |

// app/components/Main/index.tsx
import { Analytics } from '@lightspeed/react-mixpanel-script';

export default () => (
  <Analytics
    appName="web-tools"
    identity="some-unique-identifier"
    eventData={{ myVar: 'data sent with every event' }}
    profileData={{ personId: 'some-unique-identifier' }}
  >
    <Application>
      <ApplicationHeader />
      <ApplicsationBody />
      <ApplicationFooter />
    </Application>
  </Analytics>
);
  1. In your code, when you want to track an event, you can use the useAnalaytics hook to obtain the mixpanel context:
// example_using_hook.tsx
import React from 'react';
import { useAnalytics } from '@lightspeed/react-mixpanel-script';

const MyComponent = () => {
  const mixpanel = useAnalytics();

  return (
    <button onClick={() => mixpanel.track('button clicked', { custom: 'something custom' })}>
      Click Me!
    </button>
  );
};
  1. Alternatively, if your code is not structured to use hooks, you can use the convenience component TrackClick, which wraps over a component with or without a pre-existing click handler. Ultimately, this helper will be deprecated so it's suggested you use the useAnalytics hook instead.
// example_track_click.tsx
import React from 'react';
import { TrackClick } from '@lightspeed/react-mixpanel-script';

const MyComponent = () => (
  <TrackClick event="button clicked" eventData={{ custom: 'something custom' }}>
    <button onClick={() => console.log('I am wrapped')}>Click Me!</button>;
  </TrackClick>
);
  1. In your code, when you want to track any type of customer interaction, you can use the function mixpanel, which takes a callback with the Mixpanel object as a parameter:
// component.tsx
import React from 'react';
import { mixpanel } from '@lightspeed/react-mixpanel-script';

export default class TrackedButton {
  render() {
    return <button onClick={() => mixpanel(m => m.track('button clicked'))}>Click Me!</button>;
  }
}

Please refer to the Mixpanel documentation to see all the functions that Mixpanel provides.