# React Native SDK (Beta)

`@datacub/react-native` is for React Native and Expo apps. It is in beta: its logic is fully tested, and runs on real Android and iOS devices are still being brought up. No runtime dependencies, about 6 KB with gzip.

> Beta. It is on npm under the `next` tag. The only datacub packages on npm are `@datacub/react-native` and `@datacub/web`: do not install any other name under `@datacub`.

## Install

The SDK and the two packages it uses:

```sh none
npx expo install @datacub/react-native@next @react-native-async-storage/async-storage expo-application
```

## Start it

```ts rn file=App.tsx
import AsyncStorage from '@react-native-async-storage/async-storage'
import * as Application from 'expo-application'
import datacub from '@datacub/react-native'

datacub.init('YOUR_PROJECT_ID', {
  storage: AsyncStorage,
  app: Application,
})
```

It sends to `https://in.datacub.to`. Pass `apiHost` only for a proxy or another deployment.

A new project accepts events from any app, Expo Go included, so there is no bundle ID to add.

In a bare app without `expo-application`, pass `app: { applicationId: 'sa.example.shop', nativeApplicationVersion: '2.3.0', nativeBuildVersion: '87' }`. Without `storage`, every launch is a new Guest and unsent events are lost when the app closes.

## Screens

Screens are the app's page views. Send the screen's name or route template, never values from it.

```tsx types tab="React Navigation" file=App.tsx
import { NavigationContainer, useNavigationContainerRef } from '@react-navigation/native'
import { trackNavigation } from '@datacub/react-native/navigation'

export default function App() {
  const ref = useNavigationContainerRef()
  return (
    <NavigationContainer ref={ref} onReady={() => trackNavigation(ref)}>
      {/* your navigators */}
    </NavigationContainer>
  )
}
```

```tsx types tab="Expo Router" file=app/_layout.tsx
import { Slot, useSegments } from 'expo-router'
import { useEffect } from 'react'
import datacub from '@datacub/react-native'

export default function RootLayout() {
  const segments = useSegments()
  // the route template (/profile/[user]), never the path with values (/profile/sara)
  useEffect(() => datacub.screen(segments.join('/') || 'index'), [segments])
  return <Slot />
}
```

The same name twice in a row counts once. Segments that look like IDs, emails or numbers are replaced (`Product/4512` becomes `Product/:id`).

## Track, identify, reset

```ts rn
import datacub from '@datacub/react-native'

datacub.screen('Checkout')
datacub.track('order_placed', { items: 2, total: 149.5, currency: 'SAR' })
datacub.identify('user_8f3a2c', { account: 'ws_51', email: 'sara@example.sa' })
// at logout
datacub.reset()
```

The rules are the same as on the web: [events and users](https://docs.datacub.to/guides/events-and-users.md).

## Consent

If your project asks for consent first, pass `requireConsent: true` and call `grant()` after the user agrees. The SDK keeps the grant in its storage, but call it at every launch anyway so it follows your app's own record. `datacub.consent.revoke()` stops sending and is remembered. More in [privacy and consent](https://docs.datacub.to/guides/privacy.md).

```ts rn
import AsyncStorage from '@react-native-async-storage/async-storage'
import datacub from '@datacub/react-native'

datacub.init('YOUR_PROJECT_ID', {
  storage: AsyncStorage,
  requireConsent: true,
})
// once the user has agreed: your app keeps their answer; call it at every launch
datacub.consent.grant()
```

## Calls

| Call | Does |
|---|---|
| `datacub.init(publicKey, options?)` | starts the SDK once. Options: `apiHost`, `storage`, `app`, `requireConsent` |
| `datacub.track(event, properties?)` | a custom event |
| `datacub.screen(name, properties?)` | a screen view |
| `datacub.identify(userId, traits?)` | after login |
| `datacub.reset()` | at logout |
| `datacub.flush()` | sends what is queued; resolves when done or failed, never rejects |
| `datacub.consent.grant(options?)`, `datacub.consent.revoke()` | consent and opt-out, as on the web |
| `trackNavigation(ref)` | screen views from a React Navigation container ref; returns an unsubscribe function |

## What it does on its own

- Sessions: 30 minutes idle, 24 hours at most, kept across restarts.
- Delivery: batches every 10 seconds or at 20 events, and when the app goes to the background. Retries when offline; up to 1,000 events wait on the device.
- Opening and backgrounding the app send nothing, so they never inflate active users.
- Never sent: advertising IDs, device IDs, device model, carrier, location, contacts, screen size or time zone.

Call `await datacub.flush()` before a test ends. Android Auto Backup restores the Guest ID on a new phone; set `android.allowBackup: false` in Expo to give every install its own.
