React

LogSnag client for React applications

Installation

Using npm

npm install @logsnag/react

Using yarn

yarn add @logsnag/react

Using pnpm

pnpm add @logsnag/react

Usage

First, wrap your application with the LogSnagProvider at the top level, passing in your project and token:

Set your token's scope to public in the LogSnag dashboard.

import { LogSnagProvider } from '@logsnag/react';

function App() {
  return (
    <LogSnagProvider
      token="<TOKEN>"
      project="<PROJECT_NAME>"
    >
      {/* Rest of your app */}
    </LogSnagProvider>
  );
}

Hooks

The useLogSnag hook can be used across your React components and provides the following methods:

  • track(options: TrackOptions): Track custom events.

  • identify(options: IdentifyOptions): Identify user traits.

  • setUserId(userId: string | null): Set the user id for the current user. If the user is not logged in, pass null.

  • clearUserId(): Clear the user id for the current user.

  • setDebug(flag: boolean = true): Set debug mode for logging.

Tracking Events

You can also track events directly from HTML elements using data attributes:

In this example, when the button is clicked, an event named "Upgraded Plan" will be tracked with the specified tags.

Last updated

Was this helpful?