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-Swift Plugin Architecture



Plugin Architecture

plugin-architecture page anchor

Segment's plugin architecture enables you to modify and augment how the analytics client works. From modifying event payloads to changing analytics functionality, plugins help to speed up the process of getting things done.

Plugins are run through a timeline, which executes in order of insertion based on their types. Segment has these 5 types:

TypeDetails
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 completes. You can use this to perform cleanup operations.
utilityExecutes only with manual calls such as Logging.

Fundamentals

fundamentals page anchor

There are 3 basic types of plugins that you can use as a foundation for modifying functionality. They are: Plugin, EventPlugin, and DestinationPlugin.

Plugin

plugin page anchor

Plugin acts on any event payload going through the timeline.

For example, if you want to add something to the context object of any event payload as an enrichment:

1
class SomePlugin: Plugin {
2
let type: PluginType = .enrichment
3
let name: String
4
let analytics: Analytics
5
6
init(name: String) {
7
self.name = name
8
}
9
10
override func execute(event: BaseEvent): BaseEvent? {
11
var workingEvent = event
12
if var context = workingEvent?.context?.dictionaryValue {
13
context[keyPath: "foo.bar"] = 12
14
workingEvent?.context = try? JSON(context)
15
}
16
return workingEvent
17
}
18
}

EventPlugin is a plugin interface that acts on specific event types. You can choose the event types by only overriding the event functions you want.

For example, if you only want to act on Track and Identify events:

1
class SomePlugin: EventPlugin {
2
let type: PluginType = .enrichment
3
let name: String
4
let analytics: Analytics
5
6
init(name: String) {
7
self.name = name
8
}
9
10
func identify(event: IdentifyEvent) -> IdentifyEvent? {
11
// code to modify identify event
12
return event
13
}
14
15
func track(event: TrackEvent) -> TrackEvent? {
16
// code to modify track event
17
return event
18
}
19
}

The DestinationPlugin interface is commonly used for device-mode destinations. This plugin contains an internal timeline that follows the same process as the analytics timeline, enabling you to modify and augment how events reach a particular destination.

For example, if you want to implement a device-mode destination plugin for AppsFlyer, you can use this:

1
internal struct AppsFlyerSettings: Codable {
2
let appsFlyerDevKey: String
3
let appleAppID: String
4
let trackAttributionData: Bool?
5
}
6
7
@objc
8
class AppsFlyerDestination: UIResponder, DestinationPlugin, UserActivities, RemoteNotifications {
9
10
let timeline: Timeline = Timeline()
11
let type: PluginType = .destination
12
let name: String
13
var analytics: Analytics?
14
15
internal var settings: AppsFlyerSettings? = nil
16
17
required init(name: String) {
18
self.name = name
19
analytics?.track(name: "AppsFlyer Loaded")
20
}
21
22
public func update(settings: Settings) {
23
24
guard let settings: AppsFlyerSettings = settings.integrationSettings(name: "AppsFlyer") else {return}
25
self.settings = settings
26
27
28
AppsFlyerLib.shared().appsFlyerDevKey = settings.appsFlyerDevKey
29
AppsFlyerLib.shared().appleAppID = settings.appleAppID
30
AppsFlyerLib.shared().isDebug = true
31
AppsFlyerLib.shared().deepLinkDelegate = self
32
33
// additional update logic
34
}
35
36
// ...
37
38
analytics.add(plugin: AppsFlyerPlugin(name: "AppsFlyer"))
39
analytics.track("AppsFlyer Event")
  • update(settings:) Use this function to react to any settings updates. This implicitly calls when settings update.
  • OS Lifecycle hooks Plugins can also hook into lifecycle events by conforming to the platform appropriate protocol. These functions call implicitly as the lifecycle events process, like: iOSLifecycleEvents , macOSLifecycleEvents, watchOSLifecycleEvents, and LinuxLifecycleEvents.

Adding plugins enable you to modify your analytics implementation to best fit your needs. You can add a plugin using this line of code:

analytics.add(plugin: yourIntegration)

Though you can add plugins anywhere in your code, it's best to implement your plugin when you configure the client.