Segment

Automatic integration

Smartlook can be automatically integrated into Segment as its source middleware:

// initialize Smartlook
let smartlookConfiguration = Smartlook.Configuration.configuration(key: SMARTLOOK_API_KEY)
Smartlook.setupAndStartRecording(configuration: smartlookConfiguration)

// initialize Segment
let segmentConfiguration = SEGAnalyticsConfiguration(writeKey: SEGMENT_API_KEY)
...
if let smartlookMiddleware = Smartlook.segmentSourceMiddleware(options: .default, segResetEventType: SEGEventType.reset.rawValue) {
  segmentConfiguration.middlewares = [smartlookMiddleware]
}
 
SEGAnalytics.setup(with: segmentConfiguration)
// initialize Smartlook
SLSetupConfiguration *configuration = [SLSetupConfiguration configurationWithKey:SMARTLOOK_API_KEY];
[Smartlook setupAndStartRecordingWithConfiguration:configuration];

// initialize Segment
SEGAnalyticsConfiguration *segmentConfiguration = [SEGAnalyticsConfiguration configurationWithWriteKey:SEGMENT_API_KEY];
...    
SEGBlockMiddleware *smartlookMiddleware = [Smartlook segmentSourceMiddlewareWithOptions:SLSegmentMiddlewareOptionDefault whereSEGResetEventTypeIs:SEGEventTypeReset];
if (smartlookMiddleware != nil) {
  segmentConfiguration.middlewares = @[smartlookMiddleware];
}

[SEGAnalytics setupWithConfiguration:segmentConfiguration];

Smartlook Middleware integration translates Segment's track, screen, identify, alias, and reset calls to the respective Smartlook method calls. The options argument can filter unnecessary or unwanted mapping.

Option

Segment Event Type

Smartlook API Method(s)

Notes

.track

SEGEventTypeTrack

trackCustomEvent(name:props:)

.screen

SEGEventTypeScreen

trackNavigationEvent
(withControllerId:type:)

This mapping is set off in the .default options combinations, as it interferes with Smartlook's Heatmaps generating.
When enabled, the Smartlook's type is always .enter.

.identity

SEGEventTypeIdentify

setUserIdentifier(userIdentifier:)
setSessionProperty(value:forName:)

.alias

SEGEventTypeAlias

setUserIdentifier(userIdentifier:)

.reset

SEGEventTypeReset

resetSession(resetUser:)

resetUser attribute is always set to true.

.all

Sets all options (all call mappings) enabled.

.default

Sets all options except for .screen to avoid Smartlook's Heatmaps mangling.

Manual integration

There are recipes for straightforward Segment integration in iOS.

let segmentConfiguration = SEGAnalyticsConfiguration(writeKey: SEGMENT_API_KEY)
    
let smartlookMiddleware = SEGBlockMiddleware(block: { (context, next) in
  switch (context.eventType) {
  case .track:
    if let track = context.payload as? SEGTrackPayload {
      var props = [String:String]()
      // striglify payload props
      Smartlook.trackCustomEvent(name: track.event, props: props)
    }
  default:
    break
  }
  next(context)
})
segmentConfiguration.middlewares = [smartlookMiddleware]

SEGAnalytics.setup(with: segmentConfiguration)
SEGAnalyticsConfiguration *segmentConfiguration = [SEGAnalyticsConfiguration configurationWithWriteKey:SEGMENT_API_KEY];

SEGBlockMiddleware *smartlookMiddleware = [[SEGBlockMiddleware alloc] initWithBlock:^(SEGContext * _Nonnull context, SEGMiddlewareNext  _Nonnull next) {
  switch (context.eventType) {
    SEGEventTypeTrack:
    {
      SEGTrackPayload *track = (SEGTrackPayload *)context.payload;
      [Smartlook trackCustomEventWithName:track.event props:track.properties];
    }
    default: {}
  }
  next(context);
}];
segmentConfiguration.middlewares = @[smartlookMiddleware];

[SEGAnalytics setupWithConfiguration:segmentConfiguration];

 Full Segment documentation for mobile platforms can be found on the official website.