Skip to content

Commit

Permalink
Add support for custom events
Browse files Browse the repository at this point in the history
  • Loading branch information
markmur committed Jan 29, 2024
1 parent 7a55ae9 commit f9cbda1
Showing 1 changed file with 10 additions and 19 deletions.
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

0 comments on commit f9cbda1

Please sign in to comment.