Skip to contentSkip to navigationSkip to topbar
On this page
Looking for more inspiration?Visit the
(information)
You're in the right place! Segment documentation is now part of Twilio Docs. The content you are used to is still here—just in a new home with a refreshed look.

Analytics React Native Plugin Architecture



Plugin architecture

plugin-architecture page anchor

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 types

plugin-types page anchor
Plugin TypeDescription
beforeExecutes before event processing begins.
enrichmentExecutes as the first level of event processing.
destinationExecutes as events begin to pass off to destinations.
afterExecutes after all event processing is completed. You can use this to perform cleanup operations.
utilityExecutes only with manual calls such as Logging.
(information)

Info

Plugins can have their own native code (such as the iOS-only IdfaPlugin(link takes you to an external page) or wrap an underlying library (such as the FirebasePlugin(link takes you to an external page) 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.

1
2
import { createClient } from '@segment/analytics-react-native';
3
4
import { AmplitudeSessionPlugin } from '@segment/analytics-react-native-plugin-amplitude';
5
import { FirebasePlugin } from '@segment/analytics-react-native-plugin-firebase';
6
import { IdfaPlugin } from '@segment/analytics-react-native-plugin-idfa';
7
8
const segmentClient = createClient({
9
writeKey: 'SEGMENT_KEY'
10
});
11
12
segmentClient.add({ plugin: new AmplitudeSessionPlugin() });
13
segmentClient.add({ plugin: new FirebasePlugin() });
14
segmentClient.add({ plugin: new IdfaPlugin() });

Writing your own plugins

writing-your-own-plugins page anchor

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:

  • Plugin
  • EventPlugin
  • DestinationPlugin
  • UtilityPlugin
  • PlatformPlugin

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.js
2
3
import {
4
Plugin,
5
PluginType,
6
SegmentEvent,
7
} from '@segment/analytics-react-native';
8
9
export class Logger extends Plugin {
10
11
// Note that `type` is set as a class property
12
// If you do not set a type your plugin will be a `utility` plugin (see Plugin Types above)
13
type = PluginType.before;
14
15
execute(event: SegmentEvent) {
16
console.log(event);
17
return event;
18
}
19
}
20
// app.js
21
22
import { Logger } from './logger';
23
24
segmentClient.add({ plugin: new Logger() });

As the plugin overrides the execute() method, this Logger calls console.log for every event going through the Timeline.

Add a custom destination plugin

add-a-custom-destination-plugin page anchor

You can add custom plugins to Destination Plugins. For example, you could implement the following logic to send events to Braze on weekends only:

1
2
import { createClient } from '@segment/analytics-react-native';
3
4
import {BrazePlugin} from '@segment/analytics-react-native-plugin-braze';
5
import {BrazeEventPlugin} from './BrazeEventPlugin';
6
7
const segmentClient = createClient({
8
writeKey: 'SEGMENT_KEY'
9
});
10
11
const brazeplugin = new BrazePlugin();
12
const myBrazeEventPlugin = new BrazeEventPlugin();
13
brazeplugin.add(myBrazeEventPlugin);
14
segmentClient.add({plugin: brazeplugin});
15
16
// Plugin code for BrazeEventPlugin.ts
17
import {
18
Plugin,
19
PluginType,
20
SegmentEvent,
21
} from '@segment/analytics-react-native';
22
23
export class BrazeEventPlugin extends Plugin {
24
type = PluginType.before;
25
26
execute(event: SegmentEvent) {
27
var today = new Date();
28
if (today.getDay() === 6 || today.getDay() === 0) {
29
return event;
30
}
31
}
32
}

Segment would then send events to the Braze destination plugin on Saturdays and Sundays, based on device time.