Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: filter automatic screenview tracking events (#367)
- Loading branch information
1 parent
f7c456a
commit 1a535f9
Showing
8 changed files
with
214 additions
and
22 deletions.
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
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,24 @@ | ||
@testable import CioTracking | ||
import Foundation | ||
import SharedTests | ||
import SwiftUI | ||
import UIKit | ||
import XCTest | ||
|
||
class UIKitExtensionsTest: UnitTest { | ||
// MARK: bundleIdOfView | ||
|
||
func test_bundleIdOfView_givenSwiftUIView_expectAppleBundleId() { | ||
XCTAssertEqual(SwiftUI.UIHostingController(rootView: Text("")).bundleIdOfView, "com.apple.SwiftUI") | ||
} | ||
|
||
func test_bundleIdOfView_givenUIKitView_expectAppleBundleId() { | ||
XCTAssertEqual(UIAlertController().bundleIdOfView, "com.apple.UIKitCore") | ||
} | ||
|
||
func test_bundleIdOfView_givenViewFromHostApp_expectHostAppBundleId() { | ||
class MyViewController: UIViewController {} | ||
|
||
XCTAssertEqual(MyViewController().bundleIdOfView, "CommonTests") // CommonTests is value because the ViewController class above exists in the Tests target named CommonTests. | ||
} | ||
} |
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,9 @@ | ||
@testable import CioInternalCommon | ||
@testable import CioTracking | ||
import Foundation | ||
|
||
public extension QueueStorage { | ||
func filterTrackEvents(_ type: CioTracking.QueueTaskType) -> [QueueTaskMetadata] { | ||
getInventory().filter { $0.taskType == type.rawValue } | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
Tests/Tracking/CustomerIOImplementation+ScreenViewsTest.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,94 @@ | ||
@testable import CioInternalCommon | ||
@testable import CioTracking | ||
import Foundation | ||
import SharedTests | ||
import SwiftUI | ||
import UIKit | ||
import XCTest | ||
|
||
class CustomerIOImplementationScreenViewsTest: IntegrationTest { | ||
override func setUp() { | ||
super.setUp() | ||
|
||
// Screenview events are ignored if no profile identified | ||
CustomerIO.shared.identify(identifier: String.random) | ||
} | ||
|
||
// MARK: performScreenTracking | ||
|
||
func test_performScreenTracking_givenCustomerProvidesFilter_expectSdkDefaultFilterNotUsed() { | ||
var customerProvidedFilterCalled = false | ||
setUp(modifySdkConfig: { config in | ||
config.filterAutoScreenViewEvents = { _ in | ||
customerProvidedFilterCalled = true | ||
|
||
return true | ||
} | ||
}) | ||
|
||
CustomerIO.shared.performScreenTracking(onViewController: UIAlertController()) | ||
|
||
XCTAssertTrue(customerProvidedFilterCalled) | ||
assertEventTracked() | ||
} | ||
|
||
// SwiftUI wraps UIKit views and displays them in your app. Therefore, there is a good chance that automatic screenview tracking for a SwiftUI app will try to track screenview events from Views belonging to the SwiftUI framework or UIKit framework. Our SDK, by default, filters those events out. | ||
func test_performScreenTracking_givenViewFromSwiftUI_expectFalse() { | ||
CustomerIO.shared.performScreenTracking(onViewController: SwiftUI.UIHostingController(rootView: Text(""))) | ||
|
||
assertNoEventTracked() | ||
} | ||
|
||
// Our SDK believes that UIKit framework views are irrelevant to tracking data for customers. Our SDK, by default, filters those events out. | ||
func test_performScreenTracking_givenViewFromUIKit_expectFalse() { | ||
CustomerIO.shared.performScreenTracking(onViewController: UIAlertController()) | ||
|
||
assertNoEventTracked() | ||
} | ||
|
||
func test_performScreenTracking_givenViewFromHostApp_expectTrue() { | ||
class ViewInsideOfHostApp: UIViewController {} | ||
|
||
CustomerIO.shared.performScreenTracking(onViewController: ViewInsideOfHostApp()) | ||
|
||
assertEventTracked() | ||
} | ||
|
||
// MARK: getNameForAutomaticScreenViewTracking | ||
|
||
func test_getNameForAutomaticScreenViewTracking_givenViewWithNoTitle_expectNil() { | ||
class ViewController: UIViewController {} | ||
|
||
let view = ViewController() | ||
view.title = nil | ||
|
||
XCTAssertNil(view.getNameForAutomaticScreenViewTracking()) | ||
} | ||
|
||
func test_getNameForAutomaticScreenViewTracking_givenViewWithTooBasicName_expectNil() { | ||
class ViewController: UIViewController {} | ||
let view = ViewController() | ||
|
||
XCTAssertNil(view.getNameForAutomaticScreenViewTracking()) | ||
} | ||
|
||
func test_getNameForAutomaticScreenViewTracking_givenView_expectCleanupName() { | ||
class LoginViewController: UIViewController {} | ||
|
||
let view = LoginViewController() | ||
|
||
XCTAssertEqual(view.getNameForAutomaticScreenViewTracking(), "Login") | ||
} | ||
} | ||
|
||
extension CustomerIOImplementationScreenViewsTest { | ||
private func assertNoEventTracked() { | ||
XCTAssertTrue(diGraph.queueStorage.filterTrackEvents(.trackEvent).isEmpty) | ||
} | ||
|
||
private func assertEventTracked(numberOfEventsAdded: Int = 1) { | ||
let screenviewEvents = diGraph.queueStorage.filterTrackEvents(.trackEvent) | ||
|
||
XCTAssertEqual(screenviewEvents.count, numberOfEventsAdded) | ||
} | ||
} |