Analytics React Native Plugin Architecture
Segment's plugin architecture enables you to modify and augment how the events are processed before they're uploaded to the Segment API. In order to customize what happens after an event is created, you can create and place various Plugins along the processing pipeline that an event goes through. This pipeline is referred to as a timeline.
| Plugin Type | Description |
|---|---|
before | Executes before event processing begins. |
enrichment | Executes as the first level of event processing. |
destination | Executes as events begin to pass off to destinations. |
after | Executes after all event processing is completed. You can use this to perform cleanup operations. |
utility | Executes only with manual calls such as Logging. |
Info
Plugins can have their own native code (such as the iOS-only IdfaPlugin or wrap an underlying library (such as the FirebasePlugin which uses react-native-firebase under the hood).
Segment is an out-of-the-box DestinationPlugin. You can add as many other destination plugins as you like and upload events and data to them.
If you don't want the Segment destination plugin, you can pass autoAddSegmentDestination = false in the options when setting up your client. This prevents the SegmentDestination plugin from being added automatically for you.
You can add a plugin at any time through the segmentClient.add() method.
12import { createClient } from '@segment/analytics-react-native';34import { AmplitudeSessionPlugin } from '@segment/analytics-react-native-plugin-amplitude';5import { FirebasePlugin } from '@segment/analytics-react-native-plugin-firebase';6import { IdfaPlugin } from '@segment/analytics-react-native-plugin-idfa';78const segmentClient = createClient({9writeKey: 'SEGMENT_KEY'10});1112segmentClient.add({ plugin: new AmplitudeSessionPlugin() });13segmentClient.add({ plugin: new FirebasePlugin() });14segmentClient.add({ plugin: new IdfaPlugin() });
Plugins implement as ES6 Classes. To get started, familiarize yourself with the available classes in /packages/core/src/plugin.ts.
The available plugin classes are:
PluginEventPluginDestinationPluginUtilityPluginPlatformPlugin
Any plugin must be an extension of one of these classes.
You can then customize the functionality by overriding different methods on the base class. For example, here is a simple Logger plugin:
1// logger.js23import {4Plugin,5PluginType,6SegmentEvent,7} from '@segment/analytics-react-native';89export class Logger extends Plugin {1011// Note that `type` is set as a class property12// If you do not set a type your plugin will be a `utility` plugin (see Plugin Types above)13type = PluginType.before;1415execute(event: SegmentEvent) {16console.log(event);17return event;18}19}20// app.js2122import { Logger } from './logger';2324segmentClient.add({ plugin: new Logger() });
As the plugin overrides the execute() method, this Logger calls console.log for every event going through the Timeline.
You can add custom plugins to Destination Plugins. For example, you could implement the following logic to send events to Braze on weekends only:
12import { createClient } from '@segment/analytics-react-native';34import {BrazePlugin} from '@segment/analytics-react-native-plugin-braze';5import {BrazeEventPlugin} from './BrazeEventPlugin';67const segmentClient = createClient({8writeKey: 'SEGMENT_KEY'9});1011const brazeplugin = new BrazePlugin();12const myBrazeEventPlugin = new BrazeEventPlugin();13brazeplugin.add(myBrazeEventPlugin);14segmentClient.add({plugin: brazeplugin});1516// Plugin code for BrazeEventPlugin.ts17import {18Plugin,19PluginType,20SegmentEvent,21} from '@segment/analytics-react-native';2223export class BrazeEventPlugin extends Plugin {24type = PluginType.before;2526execute(event: SegmentEvent) {27var today = new Date();28if (today.getDay() === 6 || today.getDay() === 0) {29return event;30}31}32}
Segment would then send events to the Braze destination plugin on Saturdays and Sundays, based on device time.