Skip to content

Commit 483236e

Browse files
authoredOct 8, 2024··
Fix issue where user-supplied enrichments were lost during the startup phase. (#367)
1 parent 41df310 commit 483236e

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed
 

Diff for: ‎Sources/Segment/Analytics.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ public class Analytics {
9595

9696
internal func process<E: RawEvent>(incomingEvent: E, enrichments: [EnrichmentClosure]? = nil) {
9797
guard enabled == true else { return }
98-
let event = incomingEvent.applyRawEventData(store: store)
98+
let event = incomingEvent.applyRawEventData(store: store, enrichments: enrichments)
9999

100-
_ = timeline.process(incomingEvent: event, enrichments: enrichments)
100+
_ = timeline.process(incomingEvent: event)
101101

102102
/*let flushPolicies = configuration.values.flushPolicies
103103
for policy in flushPolicies {

Diff for: ‎Sources/Segment/Timeline.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public class Timeline {
2525
}
2626

2727
@discardableResult
28-
internal func process<E: RawEvent>(incomingEvent: E, enrichments: [EnrichmentClosure]? = nil) -> E? {
28+
internal func process<E: RawEvent>(incomingEvent: E) -> E? {
2929
// apply .before and .enrichment types first ...
3030
let beforeResult = applyPlugins(type: .before, event: incomingEvent)
3131
// .enrichment here is akin to source middleware in the old analytics-ios.
3232
var enrichmentResult = applyPlugins(type: .enrichment, event: beforeResult)
3333

34-
if let enrichments {
34+
if let enrichments = enrichmentResult?.enrichments {
3535
for closure in enrichments {
3636
if let result = closure(enrichmentResult) as? E {
3737
enrichmentResult = result

Diff for: ‎Sources/Segment/Types.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public struct DestinationMetadata: Codable {
1919
// MARK: - Event Types
2020

2121
public protocol RawEvent: Codable {
22+
var enrichments: [EnrichmentClosure]? { get set }
2223
var type: String? { get set }
2324
var anonymousId: String? { get set }
2425
var messageId: String? { get set }
@@ -32,6 +33,7 @@ public protocol RawEvent: Codable {
3233
}
3334

3435
public struct TrackEvent: RawEvent {
36+
@Noncodable public var enrichments: [EnrichmentClosure]? = nil
3537
public var type: String? = "track"
3638
public var anonymousId: String? = nil
3739
public var messageId: String? = nil
@@ -57,6 +59,7 @@ public struct TrackEvent: RawEvent {
5759
}
5860

5961
public struct IdentifyEvent: RawEvent {
62+
@Noncodable public var enrichments: [EnrichmentClosure]? = nil
6063
public var type: String? = "identify"
6164
public var anonymousId: String? = nil
6265
public var messageId: String? = nil
@@ -82,6 +85,7 @@ public struct IdentifyEvent: RawEvent {
8285
}
8386

8487
public struct ScreenEvent: RawEvent {
88+
@Noncodable public var enrichments: [EnrichmentClosure]? = nil
8589
public var type: String? = "screen"
8690
public var anonymousId: String? = nil
8791
public var messageId: String? = nil
@@ -109,6 +113,7 @@ public struct ScreenEvent: RawEvent {
109113
}
110114

111115
public struct GroupEvent: RawEvent {
116+
@Noncodable public var enrichments: [EnrichmentClosure]? = nil
112117
public var type: String? = "group"
113118
public var anonymousId: String? = nil
114119
public var messageId: String? = nil
@@ -134,6 +139,7 @@ public struct GroupEvent: RawEvent {
134139
}
135140

136141
public struct AliasEvent: RawEvent {
142+
@Noncodable public var enrichments: [EnrichmentClosure]? = nil
137143
public var type: String? = "alias"
138144
public var anonymousId: String? = nil
139145
public var messageId: String? = nil
@@ -289,11 +295,12 @@ extension RawEvent {
289295
}
290296
}
291297

292-
internal func applyRawEventData(store: Store) -> Self {
298+
internal func applyRawEventData(store: Store, enrichments: [EnrichmentClosure]?) -> Self {
293299
var result: Self = self
294300

295301
guard let userInfo: UserInfo = store.currentState() else { return self }
296302

303+
result.enrichments = enrichments
297304
result.anonymousId = userInfo.anonymousId
298305
result.userId = userInfo.userId
299306
result.messageId = UUID().uuidString

Diff for: ‎Sources/Segment/Utilities/Noncodable.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99

1010
@propertyWrapper
11-
internal struct Noncodable<T>: Codable {
11+
public struct Noncodable<T>: Codable {
1212
public var wrappedValue: T?
1313
public init(wrappedValue: T?) {
1414
self.wrappedValue = wrappedValue

Diff for: ‎Tests/Segment-Tests/Analytics_Tests.swift

+9-4
Original file line numberDiff line numberDiff line change
@@ -1017,18 +1017,23 @@ final class Analytics_Tests: XCTestCase {
10171017
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
10181018
let outputReader = OutputReaderPlugin()
10191019
analytics.add(plugin: outputReader)
1020-
1021-
waitUntilStarted(analytics: analytics)
10221020

10231021
let addEventOrigin: EnrichmentClosure = { event in
10241022
return Context.insertOrigin(event: event, data: [
10251023
"type": "mobile"
10261024
])
10271025
}
1026+
1027+
analytics.track(name: "enrichment check pre startup", enrichments: [addEventOrigin])
1028+
1029+
waitUntilStarted(analytics: analytics)
1030+
1031+
let trackEvent1: TrackEvent? = outputReader.lastEvent as? TrackEvent
1032+
XCTAssertEqual(trackEvent1?.context?.value(forKeyPath: "__eventOrigin.type"), "mobile")
10281033

10291034
analytics.track(name: "enrichment check", enrichments: [addEventOrigin])
10301035

1031-
let trackEvent: TrackEvent? = outputReader.lastEvent as? TrackEvent
1032-
XCTAssertEqual(trackEvent?.context?.value(forKeyPath: "__eventOrigin.type"), "mobile")
1036+
let trackEvent2: TrackEvent? = outputReader.lastEvent as? TrackEvent
1037+
XCTAssertEqual(trackEvent2?.context?.value(forKeyPath: "__eventOrigin.type"), "mobile")
10331038
}
10341039
}

0 commit comments

Comments
 (0)
Please sign in to comment.