Install

Install the JavaScript SDK and React module. This enables easy integration with your React application.

Terminal
npm install @getpolaris.ai/sdk @getpolaris.ai/sdk-react

Configure

To configure Polaris, add the Polaris provider to your React application. Be sure to replace the apiKey and apiUrl with the values provided by Polaris for your application.

ESM
<PolarisProvider
  apiKey={import.meta.env.VITE_POLARIS_API_KEY}
>
  <Outlet />
</PolarisProvider>

useInstrument() Hook

The useInstrument() hook provides a reference to an instrument that is determined by the name of the event. Let's take a look.

ESM
const instrument = useInstrument('unique-event-name');

The useInstrument() hook returns a reference the Instrument object for the event name provided.

The Instrument object has the following methods:

  • start() - Starts the measurement.
  • done() - Completes the measurement.
  • fail() - Fails the measurement.

Create a Measurement

We recommend instrumenting your application to measure critical workflows and system operations. Here is an example of creating a new measurement for a form submission.

ESM
  import { userInstrument } from '@getpolaris.ai/sdk-react';

  export default function() {
    // first, create a new measurement
    const instrument = useInstrument('my-event')

    const handleSubmit = useCallback(async (values) => {
      try {
        await save(values);
        // then, complete the measurement using the `done()` method
        instrument.done();
      } catch (error) {
        // or, fail the measurement using the `fail()` method
        instrument.fail({ error });
      }
    }, []);

    return (
      <div>...</div>
    );
  }