Skip to content

Commit

Permalink
Merge pull request #195 from klaviyo/ab/CHNL-6170/remove-opened-push-…
Browse files Browse the repository at this point in the history
…event-from-public-api

[CHNL-6170] remove opened push event from public api
  • Loading branch information
ab1470 authored Sep 5, 2024
2 parents bf0effc + 6f4068c commit 6aa3946
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Sources/KlaviyoSwift/Klaviyo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions Sources/KlaviyoSwift/Models/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions Sources/KlaviyoSwift/StateManagement/StateManagement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/KlaviyoSwiftTests/EventTests.swift
Original file line number Diff line number Diff line change
@@ -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"))
}
}
2 changes: 1 addition & 1 deletion Tests/KlaviyoSwiftTests/KlaviyoSDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
}
Expand All @@ -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)
Expand Down Expand Up @@ -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")]
}
}
2 changes: 1 addition & 1 deletion Tests/KlaviyoSwiftTests/StateManagementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 6aa3946

Please sign in to comment.