# Analytics setup

Turn on screen, session and push-open analytics in the native-notify SDK.

## Enable the flags

Every flag is off until you set it. Set them once at module scope, or on the registration call (native-notify **v5.2+**). The two merge.

<!-- tabs -->

#### NativeNotify.init

```js
import { NativeNotify } from "native-notify";

NativeNotify.init({
  appId: yourAppId,
  appToken: "yourAppToken",
  analytics: {
    screens: true,   // screen views ("which screens do users use most")
    sessions: true,  // foreground session length
    opens: true,     // push-notification open reporting
    deviceId: true,  // stable device id (see below)
  },
});
```

#### registerNNPushToken

```js
import registerNNPushToken from "native-notify";

export default function App() {
  registerNNPushToken(yourAppId, "yourAppToken", {
    analytics: { screens: true, sessions: true, opens: true, deviceId: true },
  });
  // ...
}
```

<!-- /tabs -->

Analytics needs native-notify **v5.1+**, ideally 5.1.1 or later.

## What each flag collects

| Flag       | What it does                                                                                                                                                                                                     |
| ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `screens`  | Reports a screen name and timestamp per view, plus device context, when you call `trackScreen()` or use `useNativeNotifyScreenTracking()`.                                                                       |
| `sessions` | Reports session start, end and duration. `registerNNPushToken` wires it: a session starts when the app opens and ends when it backgrounds.                                                                       |
| `opens`    | Reports which notification was tapped, read from the payload's `nn_notification_id`. One tap counts once.                                                                                                        |
| `deviceId` | Sends a stable device id with registrations and events, from `expo-application` if installed: `getIosIdForVendorAsync()` on iOS, `getAndroidId()` on Android. Without it, unique counts use the Expo push token. |

## What gets sent

- Only screen names, event counts, a device identifier, app version and timezone are sent, all to your Native Notify app.
- Events are best-effort: 10-second timeout, batched and throttled, and a failure never throws into your app. `flushScreenQueue()` forces a flush, and the SDK calls it when a session ends.
- Raw app opens, session events and open events are kept for 365 days, per-device screen-view dedupe rows for 90. Daily totals stay, so long-range charts keep working.
- Turning a flag off stops that collection on the next app run. Counts already collected age out normally.

Not using the SDK? Report events yourself with the [Analytics API](/docs/analytics/api).
