Expo

Get Started
API Reference

Segment

expo-analytics-segment provides access to https://segment.com/ mobile analytics. Wraps Segment's iOS and Android sources.
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
Pending

Installation

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';

  • newId (string) - Identifier to associate with.
  • options (CommonOptions) - An 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.


  • groupId (string) - ID of the group.

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


  • groupId (string) - ID of the group.
  • traits (Record<string, any>) - Free-form dictionary of traits of the group.
  • options (CommonOptions) - A map that can include any of these common fields. Defaults to null.

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


  • userId (string) - User 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.


  • userId (string) - User ID for the current user.
  • traits (Record<string, any>) - A map of custom properties.
  • options (CommonOptions) - Map that can include any of these common fields. Defaults to null.

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/


  • screenName (string) - Name of the screen.

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


  • screenName (string) - Name of the screen.
  • properties (Record<string, any>) - A map of custom properties.
  • options (CommonOptions) - A 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.


  • event (string) - The event name.

Log an event to Segment. See Segment Track docs.


  • event (string) - The event name.
  • properties (Record<string, any>) - A map of custom properties.
  • options (CommonOptions) - A map that can include any of these common fields. Defaults to null.

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

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

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

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);
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.