diff --git a/Sources/KlaviyoSwift/Klaviyo.swift b/Sources/KlaviyoSwift/Klaviyo.swift index b2ffe4a5..3306f5e5 100644 --- a/Sources/KlaviyoSwift/Klaviyo.swift +++ b/Sources/KlaviyoSwift/Klaviyo.swift @@ -158,7 +158,7 @@ public struct KlaviyoSDK { public func handle(notificationResponse: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void, deepLinkHandler: ((URL) -> Void)? = nil) -> Bool { if let properties = notificationResponse.notification.request.content.userInfo as? [String: Any], let body = properties["body"] as? [String: Any], let _ = body["_k"] { - create(event: Event(name: .OpenedPush, properties: properties)) + create(event: Event(name: ._openedPush, properties: properties)) Task { await MainActor.run { if let url = properties["url"] as? String, let url = URL(string: url) { diff --git a/Sources/KlaviyoSwift/Models/Event.swift b/Sources/KlaviyoSwift/Models/Event.swift index e403a07b..cdcb15bf 100644 --- a/Sources/KlaviyoSwift/Models/Event.swift +++ b/Sources/KlaviyoSwift/Models/Event.swift @@ -11,12 +11,15 @@ import KlaviyoCore public struct Event: Equatable { public enum EventName: Equatable { - case OpenedPush case OpenedAppMetric case ViewedProductMetric case AddedToCartMetric case StartedCheckoutMetric case CustomEvent(String) + + internal static var _openedPush: EventName { + EventName.CustomEvent("_openedPush") + } } public struct Metric: Equatable { @@ -87,7 +90,7 @@ public struct Event: Equatable { extension Event.EventName { public var value: String { switch self { - case .OpenedPush: return "$opened_push" + case ._openedPush: return "$opened_push" case .OpenedAppMetric: return "Opened App" case .ViewedProductMetric: return "Viewed Product" case .AddedToCartMetric: return "Added to Cart" diff --git a/Sources/KlaviyoSwift/StateManagement/StateManagement.swift b/Sources/KlaviyoSwift/StateManagement/StateManagement.swift index af3f739e..7ec9ea68 100644 --- a/Sources/KlaviyoSwift/StateManagement/StateManagement.swift +++ b/Sources/KlaviyoSwift/StateManagement/StateManagement.swift @@ -94,7 +94,7 @@ enum KlaviyoAction: Equatable { var requiresInitialization: Bool { switch self { // if event metric is opened push we DON'T require initilization in all other event metric cases we DO. - case let .enqueueEvent(event) where event.metric.name == .OpenedPush: + case let .enqueueEvent(event) where event.metric.name == ._openedPush: return false case .setEmail, .setPhoneNumber, .setExternalId, .setPushToken, .setPushEnablement, .enqueueProfile, .setProfileProperty, .resetProfile, .resetStateAndDequeue, .enqueueEvent: @@ -436,7 +436,7 @@ struct KlaviyoReducer: ReducerProtocol { we don't miss any user engagement events. In all other cases we will flush the queue using the flush intervals defined above in `StateManagementConstants` */ - return event.metric.name == .OpenedPush ? .task { .flushQueue } : .none + return event.metric.name == ._openedPush ? .task { .flushQueue } : .none case let .enqueueProfile(profile): guard case .initialized = state.initalizationState @@ -524,7 +524,7 @@ extension Event { phoneNumber: state.phoneNumber, externalId: state.externalId) var properties = properties - if metric.name == EventName.OpenedPush, + if metric.name == EventName._openedPush, let pushToken = state.pushTokenData?.pushToken { properties["push_token"] = pushToken } diff --git a/Tests/KlaviyoSwiftTests/EventTests.swift b/Tests/KlaviyoSwiftTests/EventTests.swift new file mode 100644 index 00000000..e01fd18a --- /dev/null +++ b/Tests/KlaviyoSwiftTests/EventTests.swift @@ -0,0 +1,17 @@ +// +// EventTests.swift +// +// +// Created by Andrew Balmer on 9/3/24. +// + +@testable import KlaviyoSwift +import Foundation +import XCTest + +class KlaviyoEventTests: XCTestCase { + func testOpenedPushEvent() { + let openedPushEvent = Event.EventName._openedPush + XCTAssertEqual(openedPushEvent, .CustomEvent("_openedPush")) + } +} diff --git a/Tests/KlaviyoSwiftTests/KlaviyoSDKTests.swift b/Tests/KlaviyoSwiftTests/KlaviyoSDKTests.swift index ecfb03bd..72760984 100644 --- a/Tests/KlaviyoSwiftTests/KlaviyoSDKTests.swift +++ b/Tests/KlaviyoSwiftTests/KlaviyoSDKTests.swift @@ -133,7 +133,7 @@ class KlaviyoSDKTests: XCTestCase { "foo": "bar" ] ]] - let expectation = setupActionAssertion(expectedAction: .enqueueEvent(.init(name: .OpenedPush, properties: push_body))) + let expectation = setupActionAssertion(expectedAction: .enqueueEvent(.init(name: ._openedPush, properties: push_body))) let response = try UNNotificationResponse.with(userInfo: push_body) let handled = klaviyo.handle(notificationResponse: response) { callback.fulfill() diff --git a/Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift b/Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift index f724336d..e1e9bf8c 100644 --- a/Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift +++ b/Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift @@ -374,7 +374,7 @@ class StateManagementEdgeCaseTests: XCTestCase { @MainActor func testOpenedPushEventUninitializedAddsToPendingRequests() async throws { let store = TestStore(initialState: .init(queue: []), reducer: KlaviyoReducer()) - let event = Event(name: .OpenedPush) + let event = Event(name: ._openedPush) _ = await store.send(.enqueueEvent(event)) { $0.pendingRequests = [.event(event)] } @@ -389,7 +389,7 @@ class StateManagementEdgeCaseTests: XCTestCase { } let store = TestStore(initialState: .init(queue: []), reducer: KlaviyoReducer()) - let nonOpenedPushEvents = Event.EventName.allCases.filter { $0 != .OpenedPush } + let nonOpenedPushEvents = Event.EventName.allCases.filter { $0 != ._openedPush } for event in nonOpenedPushEvents { let event = Event(name: event) @@ -444,6 +444,6 @@ class StateManagementEdgeCaseTests: XCTestCase { extension Event.EventName: CaseIterable { public static var allCases: [KlaviyoSwift.Event.EventName] { - [.OpenedPush, .OpenedAppMetric, .ViewedProductMetric, .AddedToCartMetric, .StartedCheckoutMetric, .CustomEvent("someEvent")] + [._openedPush, .OpenedAppMetric, .ViewedProductMetric, .AddedToCartMetric, .StartedCheckoutMetric, .CustomEvent("someEvent")] } } diff --git a/Tests/KlaviyoSwiftTests/StateManagementTests.swift b/Tests/KlaviyoSwiftTests/StateManagementTests.swift index b2ef5156..df60ab36 100644 --- a/Tests/KlaviyoSwiftTests/StateManagementTests.swift +++ b/Tests/KlaviyoSwiftTests/StateManagementTests.swift @@ -576,7 +576,7 @@ class StateManagementTests: XCTestCase { } // if the event is opened push we want to flush immidietly, for all other events we flush during regular intervals set in code - if eventName == .OpenedPush { + if eventName == ._openedPush { await store.receive(.flushQueue, timeout: TIMEOUT_NANOSECONDS) } }