Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions Sources/Segment/Plugins/DestinationMetadataPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DestinationMetadataPlugin: Plugin {
guard let destinations = analytics?.timeline.plugins[.destination]?.plugins as? [DestinationPlugin] else { return event }

// Mark all loaded and enabled destinations as bundled
var bundled = [String]()
var bundled: Set<String> = []
for plugin in destinations {
// Skip processing for Segment.io
if (plugin is SegmentDestination) {
Expand All @@ -38,21 +38,30 @@ public class DestinationMetadataPlugin: Plugin {
let hasSettings = integrationSettings.hasIntegrationSettings(forPlugin: plugin)
if hasSettings {
// we have a device mode plugin installed.
bundled.append(plugin.key)
bundled.insert(plugin.key)
}
}


// All active integrations, not in `bundled` are put in `unbundled`
// All unbundledIntegrations not in `bundled` are put in `unbundled`
var unbundled = [String]()
var unbundled: Set<String> = []

let activeIntegrations = integrationSettings.integrations?.dictionaryValue ?? [:]
for (integration, _) in activeIntegrations {
if (integration != "Segment.io" && !bundled.contains(integration)) {
unbundled.insert(integration)
}
}

let segmentInfo = integrationSettings.integrationSettings(forKey: "Segment.io")
let unbundledIntegrations = segmentInfo?["unbundledIntegrations"] as? [String] ?? []
for integration in unbundledIntegrations {
if (!bundled.contains(integration)) {
unbundled.append(integration)
unbundled.insert(integration)
}
}

modified._metadata = DestinationMetadata(bundled: bundled, unbundled: unbundled, bundledIds: [])
modified._metadata = DestinationMetadata(bundled: Array(bundled), unbundled: Array(unbundled), bundledIds: [])

return modified
}
Expand Down
42 changes: 41 additions & 1 deletion Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ final class Analytics_Tests: XCTestCase {
}
}

// Test to ensure bundled and unbundled integrations are populated correctly
func testDestinationMetadata() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let mixpanel = AnyDestination(key: "Mixpanel")
Expand Down Expand Up @@ -393,6 +394,45 @@ final class Analytics_Tests: XCTestCase {
let metadata = trackEvent?._metadata

XCTAssertEqual(metadata?.bundled, ["Mixpanel"])
XCTAssertEqual(metadata?.unbundled, ["Customer.io", "Amplitude"])
XCTAssertEqual(metadata?.unbundled.sorted(), ["Amplitude", "Customer.io"])
}

// Test to ensure bundled and active integrations are populated correctly
func testDestinationMetadataUnbundled() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let mixpanel = AnyDestination(key: "Mixpanel")
let outputReader = OutputReaderPlugin()

// we want the output reader on the segment plugin
// cuz that's the only place the metadata is getting added.
let segmentDest = analytics.find(pluginType: SegmentDestination.self)
segmentDest?.add(plugin: outputReader)

analytics.add(plugin: mixpanel)
var settings = Settings(writeKey: "123")
let integrations = try? JSON([
"Segment.io": JSON([
"unbundledIntegrations":
[
"Customer.io"
]
]),
"Mixpanel": JSON(["someKey": "someVal"]),
"Amplitude": JSON(["someKey": "somVal"]),
"dest1": JSON(["someKey": "someVal"])
])
settings.integrations = integrations
analytics.store.dispatch(action: System.UpdateSettingsAction(settings: settings))

waitUntilStarted(analytics: analytics)


analytics.track(name: "sampleEvent")

let trackEvent: TrackEvent? = outputReader.lastEvent as? TrackEvent
let metadata = trackEvent?._metadata

XCTAssertEqual(metadata?.bundled, ["Mixpanel"])
XCTAssertEqual(metadata?.unbundled.sorted(), ["Amplitude", "Customer.io", "dest1"])
}
}