From f9cbda1f1ca86c65e36382b38d71a934c73aaea5 Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Mon, 29 Jan 2024 10:56:37 +0000 Subject: [PATCH] Add support for custom events --- .../CartViewController.swift | 29 +++++++------------ 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/Samples/MobileBuyIntegration/MobileBuyIntegration/CartViewController.swift b/Samples/MobileBuyIntegration/MobileBuyIntegration/CartViewController.swift index 9ecc5e22..8e0c588e 100644 --- a/Samples/MobileBuyIntegration/MobileBuyIntegration/CartViewController.swift +++ b/Samples/MobileBuyIntegration/MobileBuyIntegration/CartViewController.swift @@ -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 ) } @@ -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 {