|
| 1 | +# Aptabase React Native SDK |
| 2 | + |
| 3 | +> Privacy-first analytics SDK for React Native and Expo apps. Package: @aptabase/react-native. Open source, GDPR-compliant. Supports iOS and Android. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm add @aptabase/react-native |
| 9 | +``` |
| 10 | + |
| 11 | +Android requires the INTERNET permission in `AndroidManifest.xml`: |
| 12 | + |
| 13 | +```xml |
| 14 | +<uses-permission android:name="android.permission.INTERNET" /> |
| 15 | +``` |
| 16 | + |
| 17 | +## Initialization |
| 18 | + |
| 19 | +Call `init` before rendering your app component: |
| 20 | + |
| 21 | +```js |
| 22 | +import Aptabase from "@aptabase/react-native"; |
| 23 | + |
| 24 | +Aptabase.init("<APP_KEY>"); |
| 25 | + |
| 26 | +export default function App() { |
| 27 | + return <MyApp />; |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Alternatively, use the React Context provider: |
| 32 | + |
| 33 | +```jsx |
| 34 | +import { AptabaseProvider } from "@aptabase/react-native"; |
| 35 | + |
| 36 | +export default function App() { |
| 37 | + return ( |
| 38 | + <AptabaseProvider appKey="<APP_KEY>"> |
| 39 | + <MyApp /> |
| 40 | + </AptabaseProvider> |
| 41 | + ); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Track Events |
| 46 | + |
| 47 | +```js |
| 48 | +import { trackEvent } from "@aptabase/react-native"; |
| 49 | + |
| 50 | +trackEvent("app_started"); |
| 51 | +``` |
| 52 | + |
| 53 | +## Track Events with Properties |
| 54 | + |
| 55 | +Properties accept string, number, and boolean values: |
| 56 | + |
| 57 | +```js |
| 58 | +import { trackEvent } from "@aptabase/react-native"; |
| 59 | + |
| 60 | +trackEvent("purchase_completed", { |
| 61 | + product: "subscription", |
| 62 | + price: 9.99, |
| 63 | + trial: false, |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +When using the Context provider, use the `useAptabase` hook: |
| 68 | + |
| 69 | +```jsx |
| 70 | +import { useAptabase } from "@aptabase/react-native"; |
| 71 | + |
| 72 | +function MyComponent() { |
| 73 | + const { trackEvent } = useAptabase(); |
| 74 | + trackEvent("screen_viewed", { screen: "home" }); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## Configuration |
| 79 | + |
| 80 | +The `init` function accepts an optional `AptabaseOptions` object: |
| 81 | + |
| 82 | +```js |
| 83 | +Aptabase.init("<APP_KEY>", { |
| 84 | + host: "https://your-self-hosted-instance.com", // for self-hosted deployments (A-SH-* keys) |
| 85 | + appVersion: "1.2.3", // override detected app version (useful in Expo Go) |
| 86 | + flushInterval: 30000, // override flush interval in ms (default: 60s prod, 2s dev) |
| 87 | +}); |
| 88 | +``` |
| 89 | + |
| 90 | +## Dispose |
| 91 | + |
| 92 | +To stop tracking and deinitialize the SDK: |
| 93 | + |
| 94 | +```js |
| 95 | +import Aptabase from "@aptabase/react-native"; |
| 96 | + |
| 97 | +Aptabase.dispose(); |
| 98 | +``` |
| 99 | + |
| 100 | +## Platform Notes |
| 101 | + |
| 102 | +- Supports iOS and Android via React Native and Expo |
| 103 | +- Events are batched and flushed every 60 seconds in production, every 2 seconds in development |
| 104 | +- Sessions auto-rotate after 1 hour of inactivity |
| 105 | +- Events are flushed when the app goes to background and polling resumes when active |
| 106 | +- In Expo Go, `App Version` is unavailable (native modules not loaded) — use the `appVersion` init option as a workaround |
| 107 | +- The SDK does not track any events automatically — you control what is sent |
| 108 | +- `trackEvent` runs asynchronously in the background — no need to await it |
| 109 | + |
| 110 | +## Cross-Discovery |
| 111 | + |
| 112 | +For all Aptabase SDKs and documentation, see: https://aptabase.com/llms.txt |
0 commit comments