Upgrade to React Native 2.0
If you're using analytics-react-native 1.5.1 or older, follow these steps to upgrade to analytics-react-native 2.0. You can continue to use your React Native source write key for the upgrade to view historical events. Additionally, with React Native 2.0, you don't need to leverage bundled SDK packages, but can use this list of supported destinations.
Info
Analytics React Native 2.0 implements a new storage framework, @segment/sovran-react-native, which makes it impossible to determine if your app has been previously installed. Migrating to Analytics React Native 2.0 results in new Application Installed events for your existing users. To filter these events out you can either create an Enrichment Plugin to drop events or filter them using your Segment workspace. Furthermore, previously set userId values do not persist. Trigger an Identify event to reassign the userId.
To upgrade to React Native 2.0:
-
Update the existing package:
yarn upgrade @segment/analytics-react-native -
Install additional dependencies:
yarn add @segment/sovran-react-native @react-native-async-storage/async-storage -
Install or update pods:
npx pod-install -
Add permissions to
AndroidManifest.xml:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> -
Initialize and configure the Analytics React Native 2.0 client. The package exposes a method called
createClientwhich you can use to create the Segment Analytics client. This central client manages all the tracking events.1import { createClient, AnalyticsProvider } from '@segment/analytics-react-native';23const segmentClient = createClient({4writeKey: 'SEGMENT_API_KEY'5});These are the options you can apply to configure the client:
Option name Description writeKey (required) This is your Segment write key. autoAddSegmentDestination The default is set to true.
This automatically adds the Segment Destination plugin. Set tofalseif you don't want to add the Segment Destination.debug The default is set to true.
The default value isfalsein production.
When set to false, logs don't generate.defaultSettings The default is set to undefined.
Settings that will be used if the request to get the settings from Segment failsflushAt The default is set to 20.
The count of events at which Segment sends to the backend.flushInterval The default is set to 30.
The internval in seconds at which Segment sends events to the backend.maxBatchSize The default is set to 1000.
The maxiumum batch size of how many events to send to the API at once.maxEventsToRetry The default is set to 1000.
The maximum number of events needed to retry sending if the initial request failed.retryInterval The default is set to 60.
The interval in seconds at which to retry sending events the request failed, for example, in case of a network failure.trackAppLifecycleEvents The default is set to false.
This enables you to automatically track app lifecycle events, such as application installed, opened, updated, backgrounded. Set totrueto track.trackDeepLinks The default is set to false.
This automatically tracks when the user opens the app via a deep link. Set to Enable automatic tracking for when the user opens the app via a deep link.
1import analytics from '@segment/analytics-react-native'23...45analytics.setup('WRITE_KEY', {6debug: true,7using: [amplitude, appsflyer],8trackAdvertising: true,9});
1import {2createClient,3AnalyticsProvider,4} from '@segment/analytics-react-native';56import { FirebasePlugin } from '@segment/analytics-react-native-plugin-firebase';7...89const segmentClient = createClient({10writeKey: 'WRITE_KEY',11trackAppLifecycleEvents: true,12});1314segmentClient.add({ plugin: new FirebasePlugin() });1516const App = () => {17...18return (19<AnalyticsProvider client={segmentClient}>20...21</AnalyticsProvider>22);23};
Home.js
1import analytics from '@segment/analytics-react-native';23...45import analytics from '@segment/analytics-react-native';6...7onSendEvent = async() => {8let name = this.state.eventName9let properties = this.state.props1011await analytics.track(name, properties);12}
Home.tsx
1import { useAnalytics } from '@segment/analytics-react-native';23...45const Home = ({ navigation }: { navigation: any }) => {6const { screen, track, identify, group, alias, reset, flush } =7useAnalytics();8...9onPress: () => {10track('Track pressed', { foo: 'bar' });11};12...13};