Skip to content

Commit 176e395

Browse files
authored
Merge pull request #183 from klaviyo/as/chnl-9274-flush-on-opened-event
Flush requests queue on opened push event
2 parents 9b08d8f + 09c9f88 commit 176e395

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

Sources/KlaviyoSwift/StateManagement.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,13 @@ struct KlaviyoReducer: ReducerProtocol {
400400
endpoint: .createEvent(
401401
.init(data: .init(event: event, anonymousId: anonymousId))
402402
)))
403-
return .none
403+
404+
/*
405+
if we receive an opened push event we want to flush the queue right away so that
406+
we don't miss any user engagement events. In all other cases we will flush the queue
407+
using the flush intervals defined above in `StateManagementConstants`
408+
*/
409+
return event.metric.name == .OpenedPush ? .task { .flushQueue } : .none
404410

405411
case let .enqueueProfile(profile):
406412
guard case .initialized = state.initalizationState

Tests/KlaviyoSwiftTests/StateManagementTests.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,22 @@ class StateManagementTests: XCTestCase {
485485
// MARK: - Test enqueue event
486486

487487
@MainActor
488-
func testEnqueueEvent() async throws {
488+
func testEnqueueEvents() async throws {
489489
var initialState = INITIALIZED_TEST_STATE()
490490
initialState.phoneNumber = "555BLOB"
491491
let store = TestStore(initialState: initialState, reducer: KlaviyoReducer())
492-
let event = Event(name: .OpenedPush, properties: ["push_token": initialState.pushTokenData!.pushToken])
493-
_ = await store.send(.enqueueEvent(event)) {
494-
let newEvent = Event(name: .OpenedPush, properties: event.properties, identifiers: .init(phoneNumber: $0.phoneNumber))
495-
try $0.enqueueRequest(request: .init(apiKey: XCTUnwrap($0.apiKey), endpoint: .createEvent(.init(data: .init(event: newEvent, anonymousId: XCTUnwrap($0.anonymousId))))))
492+
493+
for eventName in Event.EventName.allCases {
494+
let event = Event(name: eventName, properties: ["push_token": initialState.pushTokenData!.pushToken])
495+
await store.send(.enqueueEvent(event)) {
496+
let newEvent = Event(name: eventName, properties: event.properties, identifiers: .init(phoneNumber: $0.phoneNumber))
497+
try $0.enqueueRequest(request: .init(apiKey: XCTUnwrap($0.apiKey), endpoint: .createEvent(.init(data: .init(event: newEvent, anonymousId: XCTUnwrap($0.anonymousId))))))
498+
}
499+
500+
// if the event is opened push we want to flush immidietly, for all other events we flush during regular intervals set in code
501+
if eventName == .OpenedPush {
502+
await store.receive(.flushQueue, timeout: TIMEOUT_NANOSECONDS)
503+
}
496504
}
497505
}
498506

@@ -501,7 +509,7 @@ class StateManagementTests: XCTestCase {
501509
let initialState = INITILIZING_TEST_STATE()
502510
let store = TestStore(initialState: initialState, reducer: KlaviyoReducer())
503511

504-
let event = Event(name: .OpenedPush)
512+
let event = Event(name: .OpenedAppMetric)
505513
await store.send(.enqueueEvent(event)) {
506514
$0.pendingRequests = [KlaviyoState.PendingRequest.event(event)]
507515
}
@@ -512,7 +520,7 @@ class StateManagementTests: XCTestCase {
512520
}
513521

514522
await store.receive(.enqueueEvent(event), timeout: TIMEOUT_NANOSECONDS) {
515-
let newEvent = Event(name: .OpenedPush, identifiers: .init(phoneNumber: $0.phoneNumber))
523+
let newEvent = Event(name: .OpenedAppMetric, identifiers: .init(phoneNumber: $0.phoneNumber))
516524
try $0.enqueueRequest(
517525
request: .init(apiKey: XCTUnwrap($0.apiKey),
518526
endpoint: .createEvent(.init(

0 commit comments

Comments
 (0)