Skip to content

Commit 0ba9642

Browse files
Merge pull request #5078 from segmentio/joeynmq-patch-4
Update doc: iOS does not support destination middleware
2 parents 127eaef + 106d3a5 commit 0ba9642

File tree

1 file changed

+4
-123
lines changed
  • src/connections/sources/catalog/libraries/mobile/ios

1 file changed

+4
-123
lines changed

src/connections/sources/catalog/libraries/mobile/ios/middleware.md

Lines changed: 4 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ title: Middleware for iOS
33
strat: ios
44
---
55

6-
Middlewares are simple functions invoked by the Segment libraries, which give you a way to add information to the events you collect using the Segment SDKs. They can be used to monitor, modify, or reject events. Source Middleware are available on `analytics-ios` 3.6.0 and later. Destination Middleware are available on `analytics-ios` 4.0.0 and later.
6+
Middlewares are simple functions invoked by the Segment libraries, which give you a way to add information to the events you collect using the Segment SDKs. They can be used to monitor, modify, or reject events. Source Middlewares are available on `analytics-ios` 3.6.0 and later.
77

88
You can access the middleware API in both Objective-C and Swift.
99

1010
> info ""
11-
> **Note**: Destination-middleware only act on [data sent to destinations in device-mode](/docs/connections/destinations#connection-modes). Since the destination middleware code exists in your app or project, it cannot transform the data sent from the Segment servers to the destination endpoint.
11+
> **Note**: Destination Middlewares are **not** available for iOS.
1212
1313
### Use
1414

@@ -122,11 +122,6 @@ Finally, to use a middleware, you must provide it to the `SEGAnalyticsConfigurat
122122
*/
123123
@property (nonatomic, strong, nullable) NSArray<id<SEGMiddleware>> *sourceMiddleware;
124124

125-
/**
126-
* Set custom destination middleware. Will be run before the associated integration for a destination.
127-
*/
128-
@property (nonatomic, strong, nullable) NSArray<SEGDestinationMiddleware *> *destinationMiddleware;
129-
130125
// ...
131126
@end
132127
```
@@ -162,12 +157,9 @@ config.sourceMiddleware = [
162157
turnScreenIntoTrack,
163158
enforceEventTaxonomy,
164159
customizeAllTrackCalls,
160+
dropSpecificEvents,
165161
blockScreenCallsToAmplitude,
166162
]
167-
config.destinationMiddleware = [
168-
SEGDestinationMiddleware(key: mixpanelIntegration.key(), middleware: [sampleEventsToMixpanel]),
169-
SEGDestinationMiddleware(key: amplitudeIntegration.key(), middleware: [customizeAmplitudeTrackCalls])
170-
]
171163

172164
Analytics.setup(with: config)
173165
```
@@ -190,14 +182,10 @@ config.sourceMiddleware = @[
190182
turnScreenIntoTrack,
191183
enforceEventTaxonomy,
192184
customizeAllTrackCalls,
185+
dropSpecificEvents,
193186
blockScreenCallsToAmplitude,
194187
];
195188

196-
config.destinationMiddleware = @[
197-
[[SEGDestinationMiddleware alloc] initWithKey:mixpanelIntegration.key middleware:@[sampleEventsToMixpanel]];
198-
[[SEGDestinationMiddleware alloc] initWithKey:amplitudeIntegration.key middleware:@[customizeAmplitudeTrackCalls]];
199-
];
200-
201189
[SEGAnalytics setupWithConfiguration:config];
202190
```
203191
{% endcodeexampletab %}
@@ -411,113 +399,6 @@ SEGBlockMiddleware *blockScreenCallsToAmplitude = [[SEGBlockMiddleware alloc] in
411399
{% endcodeexample %}
412400
413401
414-
#### Sample events to a destination
415-
416-
The following example records a random selection of events sent to the Mixpanel device-mode destination.
417-
418-
{% codeexample %}
419-
{% codeexampletab Swift %}
420-
```swift
421-
let sampleEventsToMixpanel = BlockMiddleware { (context, next) in
422-
if let track = context.payload as? TrackPayload {
423-
let numberBetween0To4 = arc4random() % 5
424-
if numberBetween0To4 != 0 {
425-
return
426-
}
427-
}
428-
next(context)
429-
}
430-
```
431-
432-
{% endcodeexampletab %}
433-
{% codeexampletab Objective-C %}
434-
435-
```objc
436-
SEGBlockMiddleware *sampleEventsToMixpanel = [[SEGBlockMiddleware alloc] initWithBlock:^(SEGContext * _Nonnull context, SEGMiddlewareNext _Nonnull next) {
437-
if ([context.payload isKindOfClass:[SEGTrackPayload class]]) {
438-
NSUInteger numberBetween0To4 = arc4random() % 5;
439-
if (numberBetween0To4 != 0) {
440-
return;
441-
}
442-
}
443-
next(context);
444-
}];
445-
```
446-
{% endcodeexampletab %}
447-
{% endcodeexample %}
448-
449-
450-
451-
#### Add a custom attribute for a specific destination
452-
453-
The following example adds a custom attribute to the `context` object when sending data to Amplitude in device-mode.
454-
455-
{% codeexample %}
456-
{% codeexampletab Swift %}
457-
```swift
458-
// define middleware we'll use for amplitude
459-
let customizeAmplitudeTrackCalls = BlockMiddleware { (context, next) in
460-
if context.eventType == .track {
461-
next(context.modify { ctx in
462-
guard let track = ctx.payload as? TrackPayload else {
463-
return
464-
}
465-
let newEvent = "[Amplitude] \(track.event)"
466-
var newProps = track.properties ?? [:]
467-
newProps["customAttribute"] = "Hello"
468-
ctx.payload = TrackPayload(
469-
event: newEvent,
470-
properties: newProps,
471-
context: track.context,
472-
integrations: track.integrations
473-
)
474-
})
475-
} else {
476-
next(context)
477-
}
478-
}
479-
480-
...
481-
482-
// configure destination middleware for amplitude
483-
let amplitude = SEGAmplitudeIntegrationFactory.instance()
484-
config.use(amplitude)
485-
config.destinationMiddleware = [DestinationMiddleware(key: amplitude.key(), middleware:[customizeAmplitudeTrackCalls])]
486-
```
487-
488-
{% endcodeexampletab %}
489-
{% codeexampletab Objective-C %}
490-
491-
```objc
492-
// define middleware we'll use for amplitude
493-
SEGBlockMiddleware *customizeAmplitudeTrackCalls = [[SEGBlockMiddleware alloc] initWithBlock:^(SEGContext * _Nonnull context, SEGMiddlewareNext _Nonnull next) {
494-
if ([context.payload isKindOfClass:[SEGTrackPayload class]]) {
495-
SEGTrackPayload *track = (SEGTrackPayload *)context.payload;
496-
next([context modify:^(id<SEGMutableContext> _Nonnull ctx) {
497-
NSString *newEvent = [NSString stringWithFormat:@"[Amplitude] %@", track.event];
498-
NSMutableDictionary *newProps = (track.properties != nil) ? [track.properties mutableCopy] : [@{} mutableCopy];
499-
newProps[@"customAttribute"] = @"Hello";
500-
ctx.payload = [[SEGTrackPayload alloc] initWithEvent:newEvent
501-
properties:newProps
502-
context:track.context
503-
integrations:track.integrations];
504-
}]);
505-
} else {
506-
next(context);
507-
}
508-
}];
509-
510-
...
511-
512-
// configure destination middleware for amplitude
513-
id<SEGIntegrationFactory> amplitude = [SEGAmplitudeIntegrationFactory instance];
514-
[config use:amplitude];
515-
config.destinationMiddleware = [SEGDestinationMiddleware alloc] initWithKey:amplitude.key middleware:@[customizeAmplitudeTrackCalls]];
516-
```
517-
{% endcodeexampletab %}
518-
{% endcodeexample %}
519-
520-
521402
### Braze Middleware
522403
523404
If you use the Braze (Appboy) destination in either [cloud or device mode](/docs/connections/destinations/#connection-modes) you can save Braze costs by "debouncing" duplicate Identify calls from Segment by adding the [open-source Middleware tool](https://github.com/segmentio/segment-braze-mobile-middleware) to your implementation. More information about this tool and how it works [is available in the project's README](https://github.com/segmentio/segment-braze-mobile-middleware/blob/master/README.md#how-does-this-work).

0 commit comments

Comments
 (0)