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

react-event-tracker

v5.2.0

Published

Easily track events in your React application

Downloads

851

Readme

react-event-tracker

Install

npm install --save react-event-tracker

How to use

App.js - root level component

import { useSiteTracking } from "react-event-tracker";

const trackingConfig = {
  siteData: {
    site: "my site",
  },
  pageTracking: {
    trackPageView: ({ siteData, pageData }) => {
      // Fire a page view to your analytics solution.
    },
  },
  eventTracking: {
    trackEvent: ({ siteData, pageData, eventData }) => {
      // Fire a click event to your analytics solution.
    },
  },
};

function App() {
  const { SiteTracking } = useSiteTracking(trackingConfig);

  // Wrap your app with SiteTracking
  return <SiteTracking>...</SiteTracking>;
}

ProductPage.js - page level component

import { usePageTracking } from "react-event-tracker";

// To automatically fire a page view, just pass the `pageData` to `usePageTracking`. This will call your `trackingConfig.pageTracking.trackPageView` once the page mounts.
function ProductPage() {
  usePageTracking({
    page: "my_product",
  });

  ...
}

// If you don't want to fire the page view immediately after the page gets mounted, you can fire it yourself based on any logic you want.
function ProductPage(props) {
  const [products, setProducts] = useState();
  const { trackPageView } = usePageTracking({
    page: "my_product",
    products // will be fetched from the server
  }, {
    trackPageViewByDefault: false
  });

  useEffect(() => {
    if (products) {
      trackPageView();
    }
  }, [products, trackPageView]); // react-event-tracker guarantees that trackPageView will never change

  ...
}

Note: Make sure that you never render more than one page level component at a given time.

Content.js - any component deep inside the tree

import { useEventTracking } from "react-event-tracker";

function Content() {
  const { trackEvent } = useEventTracking();

  return (
    ...
    <button
      onClick={() => {
        /*
          Here is the core of what this library does.

          You call `trackEvent` (provided by `react-event-tracker`) with `eventData`.

          In return, `react-event-tracker` will call your own `trackEvent` (that you defined in the `trackingConfig` above) with `siteData`, `pageData`, and `eventData`.
        */
        trackEvent({ button: "Apply" });
      }}
    >
      Apply
    </button>
    ...
  )
}

Writing to localStorage

Sometimes, when tracking a page view, you may want to track the traffic source.

For example, say you are tracking page views of the Application page. It could be very useful to know how users have arrived to the Application page. Did they click the "Apply" link in the header on the Home page? Maybe the "Apply" link in the footer? Or, maybe, they landed on the Application page after clicking "Apply" on your Product Page?

One way to track this, is to write to localStorage when users click the "Apply" link. Then, read from localStorage in the trackPageView function.

const trackingConfig = {
  ...
  eventTracking: {
    storeTrafficSource: ({ pageData, eventData }) => {
      localStorage.setItem(
        "traffic_source",
        `${pageData.page}:${eventData.source}`
      );
    }
  }
};
import { useEventTracking } from "react-event-tracker";

function Content() {
  const { storeTrafficSource } = useEventTracking();

  return (
    ...
    {/*
      You call `storeTrafficSource` (provided by `react-event-tracker`) with `eventData`.

      In return, `react-event-tracker` will call your own `storeTrafficSource` (that you defined in the `trackingConfig` above) with `siteData`, `pageData`, and `eventData`.
    */}
    <a
      href="/apply"
      onClick={() => {
        // This will write "my_product:apply" to "traffic_source" in `localStorage`.
        storeTrafficSource({ source: "apply" });
      }}
    >
      Apply
    </a>
    ...
  )
}

Building a query string

When linking to external sites, you may want to add query string parameters based on siteData, pageData, and/or eventData.

Add a getQueryString function to eventTracking, e.g.:

const trackingConfig = {
  eventTracking: {
    getQueryString: ({ siteData, pageData, eventData }) => {
      const dataLayer = {
        ...siteData,
        ...pageData,
        ...eventData,
      };

      return Object.keys(dataLayer)
        .map((key) => `${key}=${encodeURIComponent(dataLayer[key])}`)
        .join("&");
    },
  },
};

Then, call getQueryString that is given to you by useEventTracking.

import { useEventTracking } from "react-event-tracker";

function Content() {
  const { getQueryString } = useEventTracking();

  return (
    ...
    {/*
      You call `getQueryString` (provided by `react-event-tracker`) with `eventData`.

      In return, `react-event-tracker` will call your own `getQueryString` (that you defined in the `trackingConfig` above) with `siteData`, `pageData`, and `eventData`.
    */}
    <a
      href={`https://external-site.com?${getQueryString({
        link: "apply"
      })}`}
    >
      Apply on external site
    </a>
    ...
  )
}

Related