The hassle-free way to add analytics to your React-Native app.
The hassle-free way to add analytics to your React-Native app.
This library is one of Segment’s most popular Flagship libraries. It is actively maintained by Segment, benefitting from new feature releases and ongoing support.
The hassle-free way to add Segment analytics to your React-Native app.
⚠️ This readme covers analytics-react-native 2.0.0 and greater. The code and readme for analytics-react-native versions below 2.0.0 can be found on the analytics-react-native-v1 branch. On May 15, 2023, Segment will end support for Analytics React Native Classic, which includes versions 1.5.1 and older. Upgrade to Analytics React Native 2.0.
Install @segment/analytics-react-native, @segment/sovran-react-native and react-native-get-random-values:
yarn add @segment/analytics-react-native @segment/sovran-react-native react-native-get-random-values @react-native-async-storage/async-storage
# or
npm install --save @segment/analytics-react-native @segment/sovran-react-native react-native-get-random-values @react-native-async-storage/async-storageNote: @react-native-async-storage/async-storage is an optional dependency. If you wish to use your own persistence layer you can use the storePersistor option when initializing the client. Make sure you always have a persistor (either by having AsyncStorage package installed or by explicitly passing a value), else you might get unexpected side-effects like multiple 'Application Installed' events. Read more Client Options
For iOS, install native modules with:
npx pod-install⚠️ For Android, you will have to add some extra permissions to your AndroidManifest.xml.
@segment/analytics-react-native 2.0 is compatible with Expo's Custom Dev Client and EAS builds without any additional configuration. Destination Plugins that require native modules may require custom Expo Config Plugins.
⚠️ @segment/analytics-react-native 2.0 is not compatible with Expo Go.
Android
In your app's AndroidManifest.xml add the below line between the `` tags.
See the Migration Guide for a detailed walkthrough of the changes you will need to make when upgrading to analytics-react-native 2.0
The package exposes a method called createClient which we can use to create the Segment Analytics client. This
central client manages all our tracking events.
import { createClient } from '@segment/analytics-react-native';
const segmentClient = createClient({
writeKey: 'SEGMENT_API_KEY',
});You must pass at least the writeKey. Additional configuration options are listed below:
| Name | Default | Description |
|---|---|---|
writeKey (REQUIRED) |
'' | Your Segment API key. |
collectDeviceId |
false | Set to true to automatically collect the device Id from the DRM API on Android devices. |
debug |
true* | When set to false, it will not generate any logs. |
logger |
undefined | Custom logger instance to expose internal Segment client logging. |
flushAt |
20 | How many events to accumulate before sending events to the backend. |
flushInterval |
30 | In seconds, how often to send events to the backend. |
flushPolicies |
undefined | Add more granular control for when to flush, see Adding or removing policies. Mutually exclusive with flushAt/flushInterval |
maxBatchSize |
1000 | How many events to send to the API at once |
trackAppLifecycleEvents |
false | Enable automatic tracking for app lifecycle events: application installed, opened, updated, backgrounded. |
trackDeepLinks |
false | Enable automatic tracking for when the user opens the app via a deep link (Note: Requires additional setup on iOS, see instructions). |
defaultSettings |
undefined | Settings that will be used if the request to get the settings from Segment fails. Type: SegmentAPISettings |
autoAddSegmentDestination |
true | Set to false to skip adding the SegmentDestination plugin. |
storePersistor |
undefined | A custom persistor for the store that analytics-react-native leverages. Must match Persistor interface exported from sovran-react-native. |
No open issues yet, or sync has not completed.