@@ -65,6 +65,22 @@ public protocol VersionedPlugin {
6565// For internal platform-specific bits
6666internal protocol PlatformPlugin : Plugin { }
6767
68+ public typealias EnrichmentClosure = ( _ event: RawEvent ? ) -> RawEvent ?
69+ public class ClosureEnrichment : Plugin {
70+ public var type : PluginType = . enrichment
71+ public var analytics : Analytics ? = nil
72+
73+ internal let closure : EnrichmentClosure
74+
75+ init ( closure: @escaping EnrichmentClosure ) {
76+ self . closure = closure
77+ }
78+
79+ public func execute< T: RawEvent > ( event: T ? ) -> T ? {
80+ return closure ( event) as? T
81+ }
82+ }
83+
6884
6985// MARK: - Plugin instance helpers
7086extension Plugin {
@@ -98,7 +114,7 @@ extension DestinationPlugin {
98114 Adds a new plugin to the currently loaded set.
99115
100116 - Parameter plugin: The plugin to be added.
101- - Returns: Returns the name of the supplied plugin.
117+ - Returns: Returns the supplied plugin.
102118
103119 */
104120 @discardableResult
@@ -110,6 +126,23 @@ extension DestinationPlugin {
110126 return plugin
111127 }
112128
129+ /**
130+ Adds a new enrichment to the currently loaded set of plugins.
131+
132+ - Parameter enrichment: The enrichment closure to be added.
133+ - Returns: Returns the the generated plugin.
134+
135+ */
136+ @discardableResult
137+ public func add( enrichment: @escaping EnrichmentClosure ) -> Plugin {
138+ let plugin = ClosureEnrichment ( closure: enrichment)
139+ if let analytics = self . analytics {
140+ plugin. configure ( analytics: analytics)
141+ }
142+ timeline. add ( plugin: plugin)
143+ return plugin
144+ }
145+
113146 /**
114147 Removes and unloads plugins with a matching name from the system.
115148
@@ -147,6 +180,21 @@ extension Analytics {
147180 return plugin
148181 }
149182
183+ /**
184+ Adds a new enrichment to the currently loaded set of plugins.
185+
186+ - Parameter enrichment: The enrichment closure to be added.
187+ - Returns: Returns the the generated plugin.
188+
189+ */
190+ @discardableResult
191+ public func add( enrichment: @escaping EnrichmentClosure ) -> Plugin {
192+ let plugin = ClosureEnrichment ( closure: enrichment)
193+ plugin. configure ( analytics: self )
194+ timeline. add ( plugin: plugin)
195+ return plugin
196+ }
197+
150198 /**
151199 Removes and unloads plugins with a matching name from the system.
152200
0 commit comments