> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mobile-starter.amisi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Analytics

> Track interactions and errors through adapter-based analytics

# @amisi/analytics

`@amisi/analytics` provides an adapter-based analytics API with first-party support for Firebase Analytics (GA4) and Firebase Crashlytics.

## Adapter model

* **Contract**
  * Adapters implement the shared `AnalyticsAdapter` contract.

* **Runtime selection**
  * Select the active adapter at runtime using `initializeAnalytics({ adapter, options })`.

* **Built-in adapters**
  * `firebase`
  * `mock`

## Initialize

Initialize once at app startup.

The starter app initializes analytics in `apps/mobile/app/_layout.tsx` and gates collection using feature flags:

* **`flags.enableAnalytics`**
* **`flags.enableCrashReporting`**

## Track interactions and events

Use the hook inside screens/components:

```tsx theme={null}
import { useAnalytics } from '@amisi/analytics';

export function Screen() {
  const analytics = useAnalytics();

  const handlePress = async () => {
    await analytics.trackInteraction({
      type: 'press',
      name: 'settings_opened',
      properties: {
        source: 'home',
      },
    });
  };

  return null;
}
```

## Record errors

For non-fatal errors you want to see in Crashlytics:

```ts theme={null}
import { useAnalytics } from '@amisi/analytics';

export function useProfile() {
  const analytics = useAnalytics();

  const run = async () => {
    try {
      await Promise.resolve();
    } catch (error) {
      if (error instanceof Error) {
        await analytics.recordError(error, { feature: 'profile' });
      }
    }
  };

  return { run };
}
```

## Firebase configuration

The Firebase adapter requires native Firebase configuration for iOS/Android builds:

* **iOS**
  * `GoogleService-Info.plist`

* **Android**
  * `google-services.json`

The app already uses React Native Firebase packages, so the Firebase setup should follow the same pattern used for `@react-native-firebase/auth`.
