-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to disable screen view usage (#844)
- Loading branch information
Showing
9 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
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,23 @@ | ||
/// Enum to define how CustomerIO SDK should handle screen view events. | ||
public enum ScreenView: String { | ||
/// Screen view events are sent to destinations for analytics purposes. | ||
/// They are also used to display in-app messages based on page rules. | ||
case all | ||
|
||
/// Screen view events are kept on device only. They are used to display in-app messages based on | ||
/// page rules. Events are not sent to our back end servers. | ||
case inApp = "inapp" | ||
|
||
/// Returns the ScreenView enum case for the given name. | ||
/// Returns fallback if the specified enum type has no constant with the given name. | ||
/// Defaults to .all | ||
public static func getScreenView(_ screenView: String?, fallback: ScreenView = .all) -> ScreenView { | ||
guard let screenView = screenView, | ||
!screenView.isEmpty, | ||
let value = ScreenView(rawValue: screenView.lowercased()) | ||
else { | ||
return fallback | ||
} | ||
return value | ||
} | ||
} |
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
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,25 @@ | ||
import CioAnalytics | ||
import CioInternalCommon | ||
/// Plugin to filter screen events based on the configuration provided by customer app. | ||
/// This plugin is used to filter out screen events that should not be processed further. | ||
class ScreenFilterPlugin: EventPlugin { | ||
private let screenViewUse: ScreenView | ||
public let type = PluginType.enrichment | ||
public weak var analytics: Analytics? | ||
|
||
init(screenViewUse: ScreenView) { | ||
self.screenViewUse = screenViewUse | ||
} | ||
|
||
func screen(event: ScreenEvent) -> ScreenEvent? { | ||
// Filter out screen events based on the configuration provided by customer app | ||
// Using switch statement to enforce exhaustive checking for all possible values of ScreenView | ||
switch screenViewUse { | ||
case .all: | ||
return event | ||
// Do not send screen events to server if ScreenView is not Analytics | ||
case .inApp: | ||
return nil | ||
} | ||
} | ||
} |
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,40 @@ | ||
@testable import CioInternalCommon | ||
import Foundation | ||
import SharedTests | ||
import XCTest | ||
|
||
class ScreenViewTest: UnitTest { | ||
func test_getScreenView_givenNamesWithMatchingCase_expectCorrectScreenView() { | ||
let screenViewAnalytics = ScreenView.getScreenView("All") | ||
let screenViewInApp = ScreenView.getScreenView("InApp") | ||
|
||
XCTAssertEqual(screenViewAnalytics, .all) | ||
XCTAssertEqual(screenViewInApp, .inApp) | ||
} | ||
|
||
func test_getScreenView_givenNamesWithDifferentCase_expectCorrectScreenView() { | ||
let screenViewAnalytics = ScreenView.getScreenView("all") | ||
let screenViewInApp = ScreenView.getScreenView("inapp") | ||
|
||
XCTAssertEqual(screenViewAnalytics, .all) | ||
XCTAssertEqual(screenViewInApp, .inApp) | ||
} | ||
|
||
func test_getScreenView_givenInvalidValue_expectFallbackScreenView() { | ||
let parsedValue = ScreenView.getScreenView("none") | ||
|
||
XCTAssertEqual(parsedValue, .all) | ||
} | ||
|
||
func test_getScreenView_givenEmptyValue_expectFallbackScreenView() { | ||
let parsedValue = ScreenView.getScreenView("", fallback: .inApp) | ||
|
||
XCTAssertEqual(parsedValue, .inApp) | ||
} | ||
|
||
func test_getScreenView_givenNil_expectFallbackScreenView() { | ||
let parsedValue = ScreenView.getScreenView(nil) | ||
|
||
XCTAssertEqual(parsedValue, .all) | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
Tests/DataPipeline/Plugin/ScreenViewsFilterPluginTest.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
@testable import CioAnalytics | ||
@testable import CioDataPipelines | ||
@testable import CioInternalCommon | ||
import Foundation | ||
@testable import SharedTests | ||
import XCTest | ||
|
||
class ScreenViewFilterPluginTests: IntegrationTest { | ||
var outputReader: OutputReaderPlugin! | ||
|
||
override func setUp() {} | ||
|
||
private func setupWithConfig(screenViewUse: ScreenView, customConfig: ((inout SdkConfig) -> Void)? = nil) { | ||
super.setUp(modifySdkConfig: { config in | ||
config.screenViewUse(screenView: screenViewUse) | ||
}) | ||
outputReader = (customerIO.add(plugin: OutputReaderPlugin()) as? OutputReaderPlugin) | ||
} | ||
|
||
func testProcessGivenScreenViewUseAnalyticsExpectScreenEventWithoutPropertiesProcessed() { | ||
setupWithConfig(screenViewUse: .all) | ||
|
||
let givenScreenTitle = String.random | ||
|
||
customerIO.screen(title: givenScreenTitle) | ||
|
||
guard let screenEvent = outputReader.screenEvents.first, outputReader.screenEvents.count == 1 else { | ||
XCTFail("Expected exactly one screen event") | ||
return | ||
} | ||
|
||
XCTAssertEqual(screenEvent.name, givenScreenTitle) | ||
XCTAssertTrue(screenEvent.properties?.dictionaryValue?.isEmpty ?? true) | ||
} | ||
|
||
func testProcessGivenScreenViewUseAnalyticsExpectScreenEventWithPropertiesProcessed() { | ||
setupWithConfig(screenViewUse: .all) | ||
|
||
let givenScreenTitle = String.random | ||
let givenProperties: [String: Any] = [ | ||
"source": "push", | ||
"discount": 10 | ||
] | ||
|
||
customerIO.screen(title: givenScreenTitle, properties: givenProperties) | ||
|
||
guard let screenEvent = outputReader.screenEvents.first, outputReader.screenEvents.count == 1 else { | ||
XCTFail("Expected exactly one screen event") | ||
return | ||
} | ||
|
||
XCTAssertEqual(screenEvent.name, givenScreenTitle) | ||
XCTAssertMatches( | ||
screenEvent.properties?.dictionaryValue, | ||
givenProperties, | ||
withTypeMap: [["discount"]: Int.self] | ||
) | ||
} | ||
|
||
func testProcessGivenScreenViewUseInAppExpectAllScreenEventsIgnored() { | ||
setupWithConfig(screenViewUse: .inApp) | ||
|
||
// Track multiple screen events | ||
for _ in 1 ... 5 { | ||
customerIO.screen(title: String.random) | ||
} | ||
|
||
XCTAssertTrue(outputReader.events.isEmpty) | ||
} | ||
} |
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