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

[Sample] Add support for custom events #122

Merged
merged 1 commit into from
Jan 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -219,33 +219,24 @@ extension CartViewController {
}

private func mapToGenericEvent(customEvent: CustomEvent) -> AnalyticsEvent? {
guard let eventName = customEvent.name else {
print("Invalid custom event: \(customEvent)")
return nil
}

do {
switch eventName {
case "custom_event":
return try decodeAndMap(event: customEvent)
default:
print("Unknown custom event \(customEvent)")
return nil
}
} catch {
print("Failed to map custom event: \(error)")
guard customEvent.name != nil else {
print("Failed to parse custom event", customEvent)
return nil
}
return AnalyticsEvent(
name: customEvent.name!,
userId: getUserId(),
timestamp: customEvent.timestamp!,
checkoutTotal: nil
)
}

private func decodeAndMap(event: CustomEvent, decoder: JSONDecoder = JSONDecoder()) throws -> AnalyticsEvent {
guard let data = event.customData?.data(using: .utf8) else { throw DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "Invalid data")) }
let decodedData = try decoder.decode(CustomPixelEventData.self, from: data)
return AnalyticsEvent(
name: event.name!,
userId: getUserId(),
timestamp: event.timestamp!,
checkoutTotal: decodedData.customAttribute
checkoutTotal: nil
)
}

Expand All @@ -265,7 +256,7 @@ struct AnalyticsEvent: Codable {
var name = ""
var userId = ""
var timestamp = ""
var checkoutTotal = 0.0
var checkoutTotal: Double? = 0.0
}

struct CustomPixelEventData: Codable {
Expand Down
Loading