Skip to content

Commit c465972

Browse files
authored
Fix _metadata population (#160)
* fix metadata logic * update test * update test * fix test
1 parent 28dee14 commit c465972

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

Sources/Segment/Plugins/DestinationMetadataPlugin.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class DestinationMetadataPlugin: Plugin {
2929
guard let destinations = analytics?.timeline.plugins[.destination]?.plugins as? [DestinationPlugin] else { return event }
3030

3131
// Mark all loaded and enabled destinations as bundled
32-
var bundled = [String]()
32+
var bundled: Set<String> = []
3333
for plugin in destinations {
3434
// Skip processing for Segment.io
3535
if (plugin is SegmentDestination) {
@@ -38,21 +38,30 @@ public class DestinationMetadataPlugin: Plugin {
3838
let hasSettings = integrationSettings.hasIntegrationSettings(forPlugin: plugin)
3939
if hasSettings {
4040
// we have a device mode plugin installed.
41-
bundled.append(plugin.key)
41+
bundled.insert(plugin.key)
4242
}
4343
}
44-
44+
45+
// All active integrations, not in `bundled` are put in `unbundled`
4546
// All unbundledIntegrations not in `bundled` are put in `unbundled`
46-
var unbundled = [String]()
47+
var unbundled: Set<String> = []
48+
49+
let activeIntegrations = integrationSettings.integrations?.dictionaryValue ?? [:]
50+
for (integration, _) in activeIntegrations {
51+
if (integration != "Segment.io" && !bundled.contains(integration)) {
52+
unbundled.insert(integration)
53+
}
54+
}
55+
4756
let segmentInfo = integrationSettings.integrationSettings(forKey: "Segment.io")
4857
let unbundledIntegrations = segmentInfo?["unbundledIntegrations"] as? [String] ?? []
4958
for integration in unbundledIntegrations {
5059
if (!bundled.contains(integration)) {
51-
unbundled.append(integration)
60+
unbundled.insert(integration)
5261
}
5362
}
5463

55-
modified._metadata = DestinationMetadata(bundled: bundled, unbundled: unbundled, bundledIds: [])
64+
modified._metadata = DestinationMetadata(bundled: Array(bundled), unbundled: Array(unbundled), bundledIds: [])
5665

5766
return modified
5867
}

Tests/Segment-Tests/Analytics_Tests.swift

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ final class Analytics_Tests: XCTestCase {
358358
}
359359
}
360360

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

395396
XCTAssertEqual(metadata?.bundled, ["Mixpanel"])
396-
XCTAssertEqual(metadata?.unbundled, ["Customer.io", "Amplitude"])
397+
XCTAssertEqual(metadata?.unbundled.sorted(), ["Amplitude", "Customer.io"])
398+
}
399+
400+
// Test to ensure bundled and active integrations are populated correctly
401+
func testDestinationMetadataUnbundled() {
402+
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
403+
let mixpanel = AnyDestination(key: "Mixpanel")
404+
let outputReader = OutputReaderPlugin()
405+
406+
// we want the output reader on the segment plugin
407+
// cuz that's the only place the metadata is getting added.
408+
let segmentDest = analytics.find(pluginType: SegmentDestination.self)
409+
segmentDest?.add(plugin: outputReader)
410+
411+
analytics.add(plugin: mixpanel)
412+
var settings = Settings(writeKey: "123")
413+
let integrations = try? JSON([
414+
"Segment.io": JSON([
415+
"unbundledIntegrations":
416+
[
417+
"Customer.io"
418+
]
419+
]),
420+
"Mixpanel": JSON(["someKey": "someVal"]),
421+
"Amplitude": JSON(["someKey": "somVal"]),
422+
"dest1": JSON(["someKey": "someVal"])
423+
])
424+
settings.integrations = integrations
425+
analytics.store.dispatch(action: System.UpdateSettingsAction(settings: settings))
426+
427+
waitUntilStarted(analytics: analytics)
428+
429+
430+
analytics.track(name: "sampleEvent")
431+
432+
let trackEvent: TrackEvent? = outputReader.lastEvent as? TrackEvent
433+
let metadata = trackEvent?._metadata
434+
435+
XCTAssertEqual(metadata?.bundled, ["Mixpanel"])
436+
XCTAssertEqual(metadata?.unbundled.sorted(), ["Amplitude", "Customer.io", "dest1"])
397437
}
398438
}

0 commit comments

Comments
 (0)