From c3cd49e15e9df1b31e6cc60f9707a8ac2f770668 Mon Sep 17 00:00:00 2001 From: Alan Charles <50601149+alanjcharles@users.noreply.github.com> Date: Wed, 6 Sep 2023 13:24:00 -0600 Subject: [PATCH] Dedupe (#10) * fix: add unsubscribed status, remove clearing device token on change events logic, add badge clearing on app open * fix: add dedup_id fix push_open * fix: change message_id to message_id from push data * fix: change message_id to twi messageId --------- Co-authored-by: Alan Charles --- Sources/TwilioEngage/TwilioEngage.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Sources/TwilioEngage/TwilioEngage.swift b/Sources/TwilioEngage/TwilioEngage.swift index 70bfec0..ea23289 100644 --- a/Sources/TwilioEngage/TwilioEngage.swift +++ b/Sources/TwilioEngage/TwilioEngage.swift @@ -113,10 +113,23 @@ public class TwilioEngage: EventPlugin { // we only wanna look at track events guard var event = event as? TrackEvent else { return event } guard var context = event.context else { return event as? T } +// guard var properties = event.properties else {return event } // this will succeed if the event name can be used to generate a push event case. guard Events(rawValue: event.event) != nil else { return event as? T } - + + // we only need to add a deDup_id to `push recieved` and `push opened` events + if event.event == Events.tapped.rawValue || event.event == Events.received.rawValue { + if var properties = event.properties?.dictionaryValue { + let formattedEventName = event.event.lowercased().replacingOccurrences(of: " ", with: "_") + let messageId = properties["message_id"] ?? UUID().toString() + let deDup_id = "\(formattedEventName)\(messageId)" + properties[keyPath: "dedup_id"] = deDup_id + + event.properties = try? JSON(properties) + } + } + // `messaging_subscription` data type is an array of objects context[keyPath: KeyPath(Self.contextKey)] = [[ "key": deviceToken, @@ -142,7 +155,6 @@ public class TwilioEngage: EventPlugin { extension TwilioEngage: RemoteNotifications { public func receivedRemoteNotification(userInfo: [AnyHashable: Any]) { - // notification was received while the app was open. if let notification = userInfo as? [String: Any] { trackNotification(notification, fromLaunch: false) }