Analytics
Usage

Usage

NativeExpress uses Posthog (opens in a new tab) for product analytics and user behavior tracking. In future releases, NativeExpress will utilise Posthog for session replay as well (currently in development).

💡

For more information about Posthog, please refer to the official documentation (opens in a new tab).

Tracking Events

Native Express provides a simple API for tracking events in your application. Native Express has a hook called useCaptureEvent that you can find under hooks/analytics/useCaptureEvent.ts.

import { useCaptureEvent } from '@/hooks/analytics/useCaptureEvent';
 
const MyComponent = () => {
  const { captureEvent } = useCaptureEvent();
 
  const handleClick = () => {
    captureEvent('button_click', {
      button: 'my_button',
      value: 123,
    });
  };
 
  return (
    <button onClick={handleClick}>Click me</button>
  );
};

It takes two arguments:

  1. eventName: The name of the event you want to track.
  2. properties: An object containing any additional properties you want to track with the event.

Identifying Users

Native Express provides a hook called useIdentifyUser that you can find under hooks/analytics/useIdentifyUser.ts. This hook allows you to identify users from your application with posthog. That way you can associate events in posthog with a user. This hook is called in the SessionProvider whenever a user logs in or logs out. But you can also call it manually if you want to identify a user in a specific component.

import { useIdentifyUser } from '@/hooks/analytics/useIdentifyUser';
 
const MyComponent = () => {
  const { identifyUser } = useIdentifyUser();
 
  const handleClick = () => {
    identifyUser('user_id', {
      email: 'user@example.com',
      name: 'John Doe',
    });
  };
 
  return (
    <button onClick={handleClick}>Click me</button>
  );
};

It takes three arguments:

  1. userId: The unique identifier for the user.
  2. properties: An object containing any additional properties you want to associate with the user.
  3. setOnceProperties: An object containing any properties you want to associate with the user and set them only once.