Skip to content

Add support for closure based enrichments #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2023
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
50 changes: 49 additions & 1 deletion Sources/Segment/Plugins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public protocol VersionedPlugin {
// For internal platform-specific bits
internal protocol PlatformPlugin: Plugin { }

public typealias EnrichmentClosure = (_ event: RawEvent?) -> RawEvent?
public class ClosureEnrichment: Plugin {
public var type: PluginType = .enrichment
public var analytics: Analytics? = nil

internal let closure: EnrichmentClosure

init(closure: @escaping EnrichmentClosure) {
self.closure = closure
}

public func execute<T: RawEvent>(event: T?) -> T? {
return closure(event) as? T
}
}


// MARK: - Plugin instance helpers
extension Plugin {
Expand Down Expand Up @@ -98,7 +114,7 @@ extension DestinationPlugin {
Adds a new plugin to the currently loaded set.

- Parameter plugin: The plugin to be added.
- Returns: Returns the name of the supplied plugin.
- Returns: Returns the supplied plugin.

*/
@discardableResult
Expand All @@ -110,6 +126,23 @@ extension DestinationPlugin {
return plugin
}

/**
Adds a new enrichment to the currently loaded set of plugins.

- Parameter enrichment: The enrichment closure to be added.
- Returns: Returns the the generated plugin.

*/
@discardableResult
public func add(enrichment: @escaping EnrichmentClosure) -> Plugin {
let plugin = ClosureEnrichment(closure: enrichment)
if let analytics = self.analytics {
plugin.configure(analytics: analytics)
}
timeline.add(plugin: plugin)
return plugin
}

/**
Removes and unloads plugins with a matching name from the system.

Expand Down Expand Up @@ -147,6 +180,21 @@ extension Analytics {
return plugin
}

/**
Adds a new enrichment to the currently loaded set of plugins.

- Parameter enrichment: The enrichment closure to be added.
- Returns: Returns the the generated plugin.

*/
@discardableResult
public func add(enrichment: @escaping EnrichmentClosure) -> Plugin {
let plugin = ClosureEnrichment(closure: enrichment)
plugin.configure(analytics: self)
timeline.add(plugin: plugin)
return plugin
}

/**
Removes and unloads plugins with a matching name from the system.

Expand Down
39 changes: 39 additions & 0 deletions Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,43 @@ final class Analytics_Tests: XCTestCase {

RunLoop.main.run(until: Date(timeIntervalSinceNow: 5))
}

func testEnrichment() {
var sourceHit: Bool = false
let sourceEnrichment: EnrichmentClosure = { event in
print("source enrichment applied")
sourceHit = true
return event
}

var destHit: Bool = true
let destEnrichment: EnrichmentClosure = { event in
print("destination enrichment applied")
destHit = true
return event
}

let config = Configuration(writeKey: "testEnrichments")
let analytics = Analytics(configuration: config)
analytics.storage.hardReset(doYouKnowHowToUseThis: true)
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

analytics.add(enrichment: sourceEnrichment)

let segment = analytics.find(pluginType: SegmentDestination.self)
segment?.add(enrichment: destEnrichment)

waitUntilStarted(analytics: analytics)

analytics.track(name: "something")

analytics.flush()

RunLoop.main.run(until: Date(timeIntervalSinceNow: 5))

XCTAssertTrue(sourceHit)
XCTAssertTrue(destHit)

}
}