From dce35415b3132f85832fa0ecb5f4208f62d3157a Mon Sep 17 00:00:00 2001 From: Ajay Subramanya Date: Tue, 4 Jun 2024 11:39:50 -0500 Subject: [PATCH 1/2] Flush on opened push event --- Sources/KlaviyoSwift/StateManagement.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/KlaviyoSwift/StateManagement.swift b/Sources/KlaviyoSwift/StateManagement.swift index b2e2c4b5..9c8e8ec7 100644 --- a/Sources/KlaviyoSwift/StateManagement.swift +++ b/Sources/KlaviyoSwift/StateManagement.swift @@ -400,7 +400,13 @@ struct KlaviyoReducer: ReducerProtocol { endpoint: .createEvent( .init(data: .init(event: event, anonymousId: anonymousId)) ))) - return .none + + /* + if we receive an opened push event we want to flush the queue right away so that + 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 case let .enqueueProfile(profile): guard case .initialized = state.initalizationState From 09c9f88b9c1234ea2ced7b586fc724c765d5a430 Mon Sep 17 00:00:00 2001 From: Ajay Subramanya Date: Tue, 4 Jun 2024 11:51:35 -0500 Subject: [PATCH 2/2] added a test --- .../StateManagementTests.swift | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Tests/KlaviyoSwiftTests/StateManagementTests.swift b/Tests/KlaviyoSwiftTests/StateManagementTests.swift index 67b12e8d..9fd0fb5d 100644 --- a/Tests/KlaviyoSwiftTests/StateManagementTests.swift +++ b/Tests/KlaviyoSwiftTests/StateManagementTests.swift @@ -485,14 +485,22 @@ class StateManagementTests: XCTestCase { // MARK: - Test enqueue event @MainActor - func testEnqueueEvent() async throws { + func testEnqueueEvents() async throws { var initialState = INITIALIZED_TEST_STATE() initialState.phoneNumber = "555BLOB" let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) - let event = Event(name: .OpenedPush, properties: ["push_token": initialState.pushTokenData!.pushToken]) - _ = await store.send(.enqueueEvent(event)) { - let newEvent = Event(name: .OpenedPush, properties: event.properties, identifiers: .init(phoneNumber: $0.phoneNumber)) - try $0.enqueueRequest(request: .init(apiKey: XCTUnwrap($0.apiKey), endpoint: .createEvent(.init(data: .init(event: newEvent, anonymousId: XCTUnwrap($0.anonymousId)))))) + + for eventName in Event.EventName.allCases { + let event = Event(name: eventName, properties: ["push_token": initialState.pushTokenData!.pushToken]) + await store.send(.enqueueEvent(event)) { + let newEvent = Event(name: eventName, properties: event.properties, identifiers: .init(phoneNumber: $0.phoneNumber)) + try $0.enqueueRequest(request: .init(apiKey: XCTUnwrap($0.apiKey), endpoint: .createEvent(.init(data: .init(event: newEvent, anonymousId: XCTUnwrap($0.anonymousId)))))) + } + + // 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 { + await store.receive(.flushQueue, timeout: TIMEOUT_NANOSECONDS) + } } } @@ -501,7 +509,7 @@ class StateManagementTests: XCTestCase { let initialState = INITILIZING_TEST_STATE() let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) - let event = Event(name: .OpenedPush) + let event = Event(name: .OpenedAppMetric) await store.send(.enqueueEvent(event)) { $0.pendingRequests = [KlaviyoState.PendingRequest.event(event)] } @@ -512,7 +520,7 @@ class StateManagementTests: XCTestCase { } await store.receive(.enqueueEvent(event), timeout: TIMEOUT_NANOSECONDS) { - let newEvent = Event(name: .OpenedPush, identifiers: .init(phoneNumber: $0.phoneNumber)) + let newEvent = Event(name: .OpenedAppMetric, identifiers: .init(phoneNumber: $0.phoneNumber)) try $0.enqueueRequest( request: .init(apiKey: XCTUnwrap($0.apiKey), endpoint: .createEvent(.init(