Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush requests queue on opened push event #183

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Sources/KlaviyoSwift/StateManagement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change.


case let .enqueueProfile(profile):
guard case .initialized = state.initalizationState
Expand Down
22 changes: 15 additions & 7 deletions Tests/KlaviyoSwiftTests/StateManagementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,22 @@ class StateManagementTests: XCTestCase {
// MARK: - Test enqueue event

@MainActor
func testEnqueueEvent() async throws {
func testEnqueueEvents() async throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this test to test all events and added an exception for opened push at the bottom.

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)
}
}
}

Expand All @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to non opened push event since it doesn't matter for this test which event we use.

await store.send(.enqueueEvent(event)) {
$0.pendingRequests = [KlaviyoState.PendingRequest.event(event)]
}
Expand All @@ -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(
Expand Down
Loading