-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from klaviyo/ab/CHNL-16694/handle-aggregate-e…
…vents [CHNL-16694] handle aggregate events
- Loading branch information
Showing
13 changed files
with
405 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Sources/KlaviyoCore/Models/APIModels/AggregateEventPayload.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// AggregateEventPayload.swift | ||
// klaviyo-swift-sdk | ||
// | ||
// Created by Andrew Balmer on 1/31/25. | ||
// | ||
|
||
import Foundation | ||
|
||
public typealias AggregateEventPayload = Data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// KlaviyoSDK.swift | ||
// klaviyo-swift-sdk | ||
// | ||
// Created by Andrew Balmer on 2/4/25. | ||
// | ||
|
||
import KlaviyoCore | ||
|
||
/// The internal interface for the Klaviyo SDK. Can only be accessed from other modules within the Klaviyo-Swift-SDK package; cannot be accessed from the host app. | ||
package struct KlaviyoInternal { | ||
/// Create and send an aggregate event. | ||
/// - Parameter event: the event to be tracked in Klaviyo | ||
package static func create(aggregateEvent: AggregateEventPayload) { | ||
dispatchOnMainThread(action: .enqueueAggregateEvent(aggregateEvent)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// IAFWebViewModel.swift | ||
// TestApp | ||
// | ||
// Created by Andrew Balmer on 1/27/25. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import KlaviyoSwift | ||
import WebKit | ||
|
||
class IAFWebViewModel: KlaviyoWebViewModeling { | ||
private enum MessageHandler: String, CaseIterable { | ||
case klaviyoNativeBridge = "KlaviyoNativeBridge" | ||
} | ||
|
||
weak var delegate: KlaviyoWebViewDelegate? | ||
|
||
let url: URL | ||
var loadScripts: Set<WKUserScript>? | ||
var messageHandlers: Set<String>? = Set(MessageHandler.allCases.map(\.rawValue)) | ||
|
||
public let (navEventStream, navEventContinuation) = AsyncStream.makeStream(of: WKNavigationEvent.self) | ||
|
||
init(url: URL) { | ||
self.url = url | ||
} | ||
|
||
// MARK: handle WKWebView events | ||
|
||
func handleScriptMessage(_ message: WKScriptMessage) { | ||
guard let handler = MessageHandler(rawValue: message.name) else { | ||
// script message has no handler | ||
return | ||
} | ||
|
||
switch handler { | ||
case .klaviyoNativeBridge: | ||
guard let jsonString = message.body as? String else { return } | ||
|
||
do { | ||
let jsonData = Data(jsonString.utf8) // Convert string to Data | ||
let messageBusEvent = try JSONDecoder().decode(IAFNativeBridgeEvent.self, from: jsonData) | ||
handleNativeBridgeEvent(messageBusEvent) | ||
} catch { | ||
print("Failed to decode JSON: \(error)") | ||
} | ||
} | ||
} | ||
|
||
private func handleNativeBridgeEvent(_ event: IAFNativeBridgeEvent) { | ||
switch event { | ||
case .formsDataLoaded: | ||
// TODO: handle formsDataLoaded | ||
() | ||
case .formAppeared: | ||
// TODO: handle formAppeared | ||
() | ||
case let .trackAggregateEvent(data): | ||
KlaviyoInternal.create(aggregateEvent: data) | ||
case .trackProfileEvent: | ||
// TODO: handle tracktProfileEvent | ||
() | ||
case .openDeepLink: | ||
// TODO: handle openDeepLink | ||
() | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
Sources/KlaviyoUI/InAppForms/Models/IAFNativeBridgeEvent.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// IAFNativeBridgeEvent.swift | ||
// TestApp | ||
// | ||
// Created by Andrew Balmer on 2/3/25. | ||
// | ||
|
||
import AnyCodable | ||
import Foundation | ||
|
||
enum IAFNativeBridgeEvent: Decodable, Equatable { | ||
// TODO: add associated values with the appropriate data types | ||
case formsDataLoaded | ||
case formAppeared | ||
case trackAggregateEvent(Data) | ||
case trackProfileEvent | ||
case openDeepLink | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case type | ||
case data | ||
} | ||
|
||
private enum TypeIdentifier: String, Decodable { | ||
case formsDataLoaded | ||
case formAppeared | ||
case trackAggregateEvent | ||
case trackProfileEvent | ||
case openDeepLink | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let typeIdentifier = try container.decode(TypeIdentifier.self, forKey: .type) | ||
|
||
switch typeIdentifier { | ||
case .formsDataLoaded: | ||
self = .formsDataLoaded | ||
case .formAppeared: | ||
self = .formAppeared | ||
case .trackAggregateEvent: | ||
let decodedData = try container.decode(AnyCodable.self, forKey: .data) | ||
let data = try JSONEncoder().encode(decodedData) | ||
self = .trackAggregateEvent(data) | ||
case .trackProfileEvent: | ||
self = .trackProfileEvent | ||
case .openDeepLink: | ||
self = .openDeepLink | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.