TrichiData

React Native SDK

Install and set up @trichidata/react-native in mobile applications.

Use this guide when your app is built with React Native 0.69+.

For session flow and event schemas, see the REST API. For a guided first integration, see Quickstart.

Installation

Device fields (userAgent, locale, timezone) are read from platform libraries. Install the set that matches your environment — the SDK auto-detects which one is available at runtime.

Expo (Expo Go or development build)

npx expo install @trichidata/react-native expo-constants expo-device expo-localization

Works in Expo Go — the Expo modules ship inside the Expo Go runtime.

Bare React Native

npm install @trichidata/react-native react-native-device-info react-native-localize

Requires react >= 18 and react-native >= 0.69 as peer dependencies. The platform libraries above are optional peer dependencies: install the Expo set for Expo projects, or the bare set otherwise. If neither is installed, the SDK falls back to Platform + Intl.

Setup

Provider

Wrap your application tree with TrichiDataProvider:

import { TrichiDataProvider } from '@trichidata/react-native';

function App() {
  return (
    <TrichiDataProvider apiKey="prj_ak_xxxxxxxxxxxx">
      <RootNavigator />
    </TrichiDataProvider>
  );
}

Props

PropTypeDefaultDescription
apiKeystringrequiredProject write key
autoInitbooleantrueCall init() on mount
devicePartial<DeviceInfo>Override auto-detected device fields
trackingTrackingContextOptional override (no auto cookie parse on RN; SDK still sends a generated clientId if omitted)

When autoInit is true, the provider calls init_sdk with device info on mount. Device fields are resolved automatically depending on the environment (see Device info sources). The session ID from the response is stored and sent on subsequent requests.

Hook

Use useTrichiData() to access the client anywhere inside the provider:

import { useTrichiData } from '@trichidata/react-native';

function CheckoutButton() {
  const client = useTrichiData();

  const handlePress = async () => {
    await client.checkoutStarted({
      total: 99.9,
      currency: 'USD',
      items: [{ productId: 'p1', name: 'Widget', price: 99.9, quantity: 1 }],
    });
  };

  return <Button title="Checkout" onPress={handlePress} />;
}

useTrichiData() returns a TrichiDataClient instance. It throws if used outside TrichiDataProvider.

Manual initialization

Set autoInit={false} when you need to control when init() runs:

<TrichiDataProvider apiKey="prj_ak_xxx" autoInit={false}>
  <App />
</TrichiDataProvider>
import { useTrichiData } from '@trichidata/react-native';

function App() {
  const client = useTrichiData();

  useEffect(() => {
    void client.init();
  }, [client]);

  return <RootNavigator />;
}

Screen tracking

Automatic screen tracking is not included in this version. Track screens manually:

await client.page({ title: 'Checkout' });

Call this from your screen component (e.g. in useEffect on mount) or from a navigation listener you control.

Pass appVersion in identify() from your app config (e.g. Expo expo-application, or your own build metadata).

Device info sources

On init(), the SDK detects the environment and reads device fields from the matching stack. Expo projects use Expo modules only; bare projects use the community libraries.

FieldExpoBare React NativeFallback
userAgentexpo-constants getWebViewUserAgentAsync(), then expo-devicereact-native-device-info getUserAgent()Platform.OS/Version
localeexpo-localization getLocales()react-native-localize getLocales()Intl
timezoneexpo-localization getCalendars()react-native-localize getTimeZone()Intl
ip0.0.0.00.0.0.00.0.0.0

Detection is based on the presence of expo-constants. If the preferred stack is missing or throws, the SDK falls back to Platform + Intl, so init() never crashes.

React Native exports

ExportPurpose
TrichiDataProviderContext provider with optional auto-init
useTrichiDataHook to access TrichiDataClient
TrichiDataClientClient for use without the provider

Client methods

MethodDescription
init(params?)Start a session (init_sdk); device collected under the hood
identify({ userId?, appVersion? })Associate session context — at least one field required
page / productViewed / …Typed helpers for reserved event names
trackCustom(name, properties?)Custom (non-reserved) event

See REST API for reserved eventName values and properties schemas.

See also

On this page