Docs-logo

Expo

Get Started
API Reference
Slash-shortcut-icon
Hamburger-icon

Segment

Info-icon
We recommend using the official @segment/analytics-react-native instead of expo-analytics-segment.
expo-analytics-segment provides access to https://segment.com/ mobile analytics. Wraps Segment's iOS and Android sources.
Info-icon
Note: Session tracking may not work correctly when running Experiences in the main Expo app. It will work correctly if you create a standalone app.

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb
Status-success-iconStatus-success-iconStatus-success-iconStatus-success-iconStatus-failed-icon

Installation

Terminal
→ expo install expo-analytics-segment

If you're installing this in a bare React Native app, you should also follow these additional installation instructions.

import * as Segment from 'expo-analytics-segment';

Arguments

NameTypeDescription
newIdstringIdentifier to associate with.
optionsCommonOptionsAn extra dictionary with options for the call, see here for possible configuration options. An example options object would be:
{
  "integrations": {
    "Sentry": {
      "enabled": true
     }
  },
  "context": {
    "ip": "0.0.0.0"
  }
}

Associate current identity with a new identifier. See Segment Alias docs.

  • Promise<boolean>

A Promise which fulfils witch a boolean indicating whether the method has been executed on the underlying Segment instance or not.


Manually flush the event queue. You shouldn't need to call this in most cases.

  • void

Arguments

NameTypeDescription
groupIdstringID of the group.

Associate the user with a group. See Segment Group docs.

  • void

Arguments

NameTypeDescription
groupIdstringID of the group.
traitsRecord<string, any>Free-form dictionary of traits of the group.
optionsCommonOptionsA map that can include any of these common fields. Defaults to null.

Associate the user with a group with traits. See Segment Group docs.

  • void

Arguments

NameTypeDescription
userIdstringUser ID for the current user.

Associates the current user with a user ID. Call this after calling Segment.initialize() but before other segment calls. See Segment Identify docs.

  • void

Arguments

NameTypeDescription
userIdstringUser ID for the current user.
traitsRecord<string, any>A map of custom properties.
optionsCommonOptionsMap that can include any of these common fields. Defaults to null.

  • void

Arguments

NameTypeDescription
optionsInitializeOptionsAn InitializeOptions object.

Segment requires separate write keys for iOS and Android. You will need to log in to Segment to receive these keys: https://segment.com/docs/guides/setup/how-do-i-find-my-write-key/

  • void

Arguments

NameTypeDescription
screenNamestringName of the screen.

Record that a user has seen a screen to Segment. See Segment Screen docs.

  • void

Arguments

NameTypeDescription
screenNamestringName of the screen.
propertiesRecord<string, any>A map of custom properties.
optionsCommonOptionsA map that can include any of these common fields. Defaults to null.

Record that a user has seen a screen to Segment with custom properties. See Segment Screen docs.

  • void

Arguments

NameTypeDescription
enabledboolean-


Arguments

NameTypeDescription
eventstringThe event name.

Log an event to Segment. See Segment Track docs.

  • void

Arguments

NameTypeDescription
eventstringThe event name.
propertiesRecord<string, any>A map of custom properties.
optionsCommonOptionsA map that can include any of these common fields. Defaults to null.

Log an event to Segment with custom properties. See Segment Track docs.

  • void

Acceptable values are: Record<string, any>, null.

NameTypeDescription
androidWriteKey
(optional)
stringWrite key for Android source.
iosWriteKey
(optional)
stringWrite key for iOS source.

Info-icon
Depending on the audience for your app (e.g. children) or the countries where you sell your app (e.g. the EU), you may need to offer the ability for users to opt-out of analytics data collection inside your app. You can turn off forwarding to ALL destinations including Segment itself: (Source – Segment docs)
import * as Segment from 'expo-analytics-segment';

Segment.setEnabledAsync(false);

// Or if they opt-back-in, you can re-enable data collection:
Segment.setEnabledAsync(true);
Info-icon
Note: disabling the Segment SDK ensures that all data collection method invocations (eg. track, identify, etc) are ignored.
This method is only supported in standalone and detached apps. In Expo Go the promise will reject.
The setting value will be persisted across restarts, so once you call setEnabledAsync(false), Segment won't track the users even when the app restarts. To check whether tracking is enabled, use Segment.getEnabledAsync() which returns a promise which should resolve to a boolean.