Skip to content

Commit

Permalink
fix: fix the enrichment plugin to enable filter event (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhao900914 authored Feb 23, 2024
1 parent 0b77c2e commit 5d3aeb0
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Amplitude-Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 54;
objects = {

/* Begin PBXAggregateTarget section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
19C9CC222937F5A600C1E660 /* AmplitudeSwiftUIExample.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 19C9CC202937F5A600C1E660 /* AmplitudeSwiftUIExample.xcdatamodeld */; };
19C9CC2D2937F5D000C1E660 /* AmplitudeSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 19C9CC2C2937F5D000C1E660 /* AmplitudeSwift */; };
3A3036482A4B45780004CF0B /* TroubleShootingPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A3036472A4B45780004CF0B /* TroubleShootingPlugin.swift */; };
3A42EB922B87CDE30044FD45 /* FilterPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A42EB912B87CDE30044FD45 /* FilterPlugin.swift */; };
3A4E19BD2941D885002EA8BC /* IDFACollectionPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A4E19BC2941D86E002EA8BC /* IDFACollectionPlugin.swift */; };
58324F75294BF7CF00C71E2E /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 58324F74294BF7CF00C71E2E /* WidgetKit.framework */; };
58324F77294BF7CF00C71E2E /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 58324F76294BF7CF00C71E2E /* SwiftUI.framework */; };
Expand Down Expand Up @@ -88,6 +89,7 @@
19C9CC292937F5B200C1E660 /* AmplitudeSwift */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = AmplitudeSwift; path = ../..; sourceTree = "<group>"; };
19C9CC2A2937F5C900C1E660 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
3A3036472A4B45780004CF0B /* TroubleShootingPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TroubleShootingPlugin.swift; sourceTree = "<group>"; };
3A42EB912B87CDE30044FD45 /* FilterPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterPlugin.swift; sourceTree = "<group>"; };
3A4E19BC2941D86E002EA8BC /* IDFACollectionPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IDFACollectionPlugin.swift; sourceTree = "<group>"; };
58324F73294BF7CF00C71E2E /* iOSWidgetExampleExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = iOSWidgetExampleExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
58324F74294BF7CF00C71E2E /* WidgetKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WidgetKit.framework; path = System/Library/Frameworks/WidgetKit.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -206,6 +208,7 @@
BA541E9A2A8B587E0088D841 /* LocationPlugin.swift */,
3A4E19BC2941D86E002EA8BC /* IDFACollectionPlugin.swift */,
3A3036472A4B45780004CF0B /* TroubleShootingPlugin.swift */,
3A42EB912B87CDE30044FD45 /* FilterPlugin.swift */,
);
path = ExamplePlugins;
sourceTree = "<group>";
Expand Down Expand Up @@ -394,6 +397,7 @@
3A4E19BD2941D885002EA8BC /* IDFACollectionPlugin.swift in Sources */,
58324F84294BF7CF00C71E2E /* iOSWidgetExample.intentdefinition in Sources */,
3A3036482A4B45780004CF0B /* TroubleShootingPlugin.swift in Sources */,
3A42EB922B87CDE30044FD45 /* FilterPlugin.swift in Sources */,
BA541E9B2A8B587E0088D841 /* LocationPlugin.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct AmplitudeSwiftUIExampleApp: App {
Amplitude.testInstance.add(plugin: LocationPlugin())
// add the trouble shooting plugin for debugging
Amplitude.testInstance.add(plugin: TroubleShootingPlugin())
Amplitude.testInstance.add(plugin: FilterPlugin())

Amplitude.experimentClient.fetch(user: nil, completion: nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ struct ContentView: View {
Text("Send Revenue Event")
}.buttonStyle(AmplitudeButton())
}
Section(header: Text("FILTERED EVENT")) {
HStack {
Button(action: {
print("Send event")
Amplitude.testInstance.track(eventType: "Filtered Event")
}) {
Text("Event Should Be Filtered")
}.buttonStyle(AmplitudeButton())

}
}
Section(header: Text("IDENTIFY")) {
HStack {
TextField("User Property Key", text: $userPropertyKey)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// FilterPlugin.swift
// AmplitudeSwiftUIExample
//
// Created by Alyssa.Yu on 2/22/24.
//

import Foundation
import AmplitudeSwift

class FilterPlugin: EnrichmentPlugin {
public override func execute(event: BaseEvent) -> BaseEvent? {
guard event.eventType != "Filtered Event" else {
return nil
}
return event
}
}
2 changes: 1 addition & 1 deletion Sources/Amplitude/Mediator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class Mediator {
}
}

internal func execute(event: BaseEvent) -> BaseEvent? {
internal func execute(event: BaseEvent?) -> BaseEvent? {
var result: BaseEvent? = event
plugins.forEach { plugin in
if let r = result {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Amplitude/Timeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Timeline {
internal func applyPlugin(pluginType: PluginType, event: BaseEvent?) -> BaseEvent? {
var result: BaseEvent? = event
if let mediator = plugins[pluginType] {
result = mediator.execute(event: event!)
result = mediator.execute(event: event)
}
return result
}
Expand Down
35 changes: 35 additions & 0 deletions Tests/AmplitudeTests/AmplitudeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,41 @@ final class AmplitudeTests: XCTestCase {
XCTAssertEqual(lastEvent?.language!.isEmpty, false)
}

func testFilterAndEnrichmentPlugin() {
let apiKey = "testFilterAndEnrichmentPlugin"
let enrichedEventType = "Enriched Event"
storageTest = TestPersistentStorage(storagePrefix: "storage-\(apiKey)")
let amplitude = Amplitude(configuration: Configuration(
apiKey: apiKey,
storageProvider: storageTest
))

class TestFilterAndEnrichmentPlugin: EnrichmentPlugin {
override func execute(event: BaseEvent) -> BaseEvent? {
if event.eventType == "Enriched Event" {
if event.eventProperties == nil {
event.eventProperties = [:]
}
event.eventProperties!["testPropertyKey"] = "testPropertyValue"
return event
}
return nil
}
}
let testPlugin = TestFilterAndEnrichmentPlugin()
amplitude.add(plugin: testPlugin)
amplitude.track(event: BaseEvent(eventType: enrichedEventType))
amplitude.track(event: BaseEvent(eventType: "Other Event"))

let events = storageTest.events()
XCTAssertEqual(events[0].eventType, enrichedEventType)
XCTAssertEqual(getDictionary(events[0].eventProperties!), [
"testPropertyKey": "testPropertyValue"
])

XCTAssertEqual(events.count, 1)
}

func testContextWithDisableTrackingOptions() {
let apiKey = "testApiKeyForDisableTrackingOptions"
let trackingOptions = TrackingOptions()
Expand Down

0 comments on commit 5d3aeb0

Please sign in to comment.