-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc478a0
commit 4dc18d9
Showing
3 changed files
with
102 additions
and
8 deletions.
There are no files selected for viewing
46 changes: 41 additions & 5 deletions
46
Sources/Amplitude/Plugins/AmplitudeDestinationPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// DestinationPlugin.swift | ||
// | ||
// | ||
// Created by Hao Yu on 11/15/22. | ||
// | ||
|
||
public protocol DestinationPlugin: EventPlugin { | ||
|
||
} | ||
|
||
private var _timeline: Timeline? = nil | ||
extension DestinationPlugin { | ||
var timeline: Timeline? { | ||
if _timeline == nil { | ||
_timeline = Timeline() | ||
} | ||
return _timeline | ||
} | ||
var enabled: Bool { | ||
return true | ||
} | ||
|
||
var logger: (any Logger) { | ||
return self.amplitude?.logger ?? ConsoleLogger() | ||
} | ||
|
||
@discardableResult | ||
func add(plugin: Plugin) -> Plugin { | ||
plugin.amplitude = self.amplitude | ||
timeline?.add(plugin: plugin) | ||
return plugin | ||
} | ||
|
||
func remove(plugin: Plugin) { | ||
timeline?.remove(plugin: plugin) | ||
} | ||
|
||
func process(event: BaseEvent?) -> BaseEvent? { | ||
// Skip this destination if it is disabled via settings | ||
if !enabled { | ||
return nil | ||
} | ||
let beforeResult = timeline?.applyPlugin(pluginType: .before, event: event) | ||
let enrichmentResult = timeline?.applyPlugin(pluginType: .enrichment, event: beforeResult) | ||
var destinationResult: BaseEvent? = nil | ||
switch enrichmentResult { | ||
case let e as IdentifyEvent: | ||
destinationResult = identify(event: e) | ||
case let e as GroupIdentifyEvent: | ||
destinationResult = track(event: e) | ||
case let e as RevenueEvent: | ||
destinationResult = revenue(event: e) | ||
case let e?: | ||
destinationResult = track(event: e) | ||
default: | ||
break | ||
} | ||
return destinationResult | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters