|
| 1 | +// |
| 2 | +// ComscoreDestination.swift |
| 3 | +// ComscoreDestination |
| 4 | +// |
| 5 | +// Created by Cody Garvin on 9/21/21. |
| 6 | +// |
| 7 | + |
| 8 | +import Segment |
| 9 | +import Intercom |
| 10 | +import CoreMedia |
| 11 | + |
| 12 | +/** |
| 13 | + An implementation of the Comscore Analytics device mode destination as a plugin. |
| 14 | + */ |
| 15 | + |
| 16 | +class IntercomDestination: DestinationPlugin { |
| 17 | + let timeline = Timeline() |
| 18 | + let type = PluginType.destination |
| 19 | + let key = "Intercom" |
| 20 | + var analytics: Analytics? = nil |
| 21 | + |
| 22 | + private var intercomSettings: IntercomSettings? |
| 23 | + private var configurationLabels = [String: Any]() |
| 24 | + |
| 25 | + func update(settings: Settings, type: UpdateType) { |
| 26 | + // Skip if you have a singleton and don't want to keep updating via settings. |
| 27 | + guard type == .initial else { return } |
| 28 | + |
| 29 | + // Grab the settings and assign them for potential later usage. |
| 30 | + // Note: Since integrationSettings is generic, strongly type the variable. |
| 31 | + guard let tempSettings: IntercomSettings = settings.integrationSettings(forPlugin: self) else { return } |
| 32 | + intercomSettings = tempSettings |
| 33 | + Intercom.setApiKey(tempSettings.mobileApiKey, forAppId: tempSettings.appId) |
| 34 | + analytics?.log(message: "Intercolm.setApiKey(\(tempSettings.mobileApiKey), forApId:\(tempSettings.appId))", kind: .debug) |
| 35 | + } |
| 36 | + |
| 37 | + func identify(event: IdentifyEvent) -> IdentifyEvent? { |
| 38 | + |
| 39 | + if let userId = event.userId { |
| 40 | + Intercom.registerUser(withUserId: userId) |
| 41 | + analytics?.log(message: "Intercom.registerUser(withUserId: \(userId)", kind: .debug) |
| 42 | + } else if let _ = event.anonymousId { |
| 43 | + Intercom.registerUnidentifiedUser() |
| 44 | + analytics?.log(message: "Intercom.registerUnidentifiedUser()", kind: .debug) |
| 45 | + } |
| 46 | + |
| 47 | + if let integration = event.integrations?.dictionaryValue?["Intercom"] as? [AnyHashable: Any], |
| 48 | + let userHash = integration["user_hash"] as? String { |
| 49 | + Intercom.setUserHash(userHash) |
| 50 | + } |
| 51 | + |
| 52 | + if let traits = event.traits?.dictionaryValue { |
| 53 | + // Set user attributes |
| 54 | + setUserAttributes(traits, event: event) |
| 55 | + } |
| 56 | + |
| 57 | + return event |
| 58 | + } |
| 59 | + |
| 60 | + func track(event: TrackEvent) -> TrackEvent? { |
| 61 | + |
| 62 | + // Properties can not be empty |
| 63 | + guard let properties = event.properties?.dictionaryValue else { |
| 64 | + |
| 65 | + Intercom.logEvent(withName: event.event) |
| 66 | + analytics?.log(message: "Intercom.logEvent(withName: \(event.event))", kind: .debug) |
| 67 | + return event |
| 68 | + } |
| 69 | + |
| 70 | + var output = [String: Any]() |
| 71 | + var price = [String: Any]() |
| 72 | + var isAmountSet = false |
| 73 | + |
| 74 | + for (key, value) in properties { |
| 75 | + output[key] = value |
| 76 | + |
| 77 | + if let dataValue = value as? Double, |
| 78 | + key == "revenue" || key == "total" && !isAmountSet { |
| 79 | + let amountInCents = dataValue * 100 |
| 80 | + price["amount"] = amountInCents |
| 81 | + output.removeValue(forKey: key) |
| 82 | + isAmountSet = true |
| 83 | + } |
| 84 | + |
| 85 | + if key == "currency" { |
| 86 | + price["currency"] = value |
| 87 | + output.removeValue(forKey: "currency") |
| 88 | + } |
| 89 | + |
| 90 | + if price.count > 0 { |
| 91 | + output["price"] = price |
| 92 | + } |
| 93 | + |
| 94 | + if value is [String: Any] || value is [Any] { |
| 95 | + output.removeValue(forKey: key) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + Intercom.logEvent(withName: event.event, metaData: output) |
| 100 | + analytics?.log(message: "Intercom.logEvent(withName: \(event.event), metaData: \(output))", kind: .debug) |
| 101 | + |
| 102 | + return event |
| 103 | + } |
| 104 | + |
| 105 | + func group(event: GroupEvent) -> GroupEvent? { |
| 106 | + |
| 107 | + // id is required field for adding or modifying a company |
| 108 | + guard let traits = event.traits?.dictionaryValue, |
| 109 | + let groupId = event.groupId else { return event } |
| 110 | + |
| 111 | + let company = setCompanyAttributes(traits) |
| 112 | + company.companyId = groupId |
| 113 | + |
| 114 | + let userAttributes = ICMUserAttributes() |
| 115 | + userAttributes.companies = [company] |
| 116 | + |
| 117 | + Intercom.updateUser(userAttributes) |
| 118 | + analytics?.log(message: "Intercom.updateUser(\(userAttributes))", kind: .debug) |
| 119 | + |
| 120 | + return event |
| 121 | + } |
| 122 | + |
| 123 | + func reset() { |
| 124 | + Intercom.logout() |
| 125 | + analytics?.log(message: "Intercom.logout()", kind: .debug) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// Example of what settings may look like. |
| 130 | +private struct IntercomSettings: Codable { |
| 131 | + let appId: String |
| 132 | + let mobileApiKey: String |
| 133 | +} |
| 134 | + |
| 135 | +private extension IntercomDestination { |
| 136 | + |
| 137 | + func setUserAttributes(_ traits: [String: Any], event: RawEvent?) { |
| 138 | + let userAttributes = ICMUserAttributes() |
| 139 | + var customAttributes = traits |
| 140 | + |
| 141 | + if let email = traits["email"] as? String { |
| 142 | + userAttributes.email = email |
| 143 | + customAttributes.removeValue(forKey: "email") |
| 144 | + } |
| 145 | + |
| 146 | + if let userId = traits["user_id"] as? String { |
| 147 | + userAttributes.userId = userId |
| 148 | + customAttributes.removeValue(forKey: "user_id") |
| 149 | + } |
| 150 | + |
| 151 | + if let name = traits["name"] as? String { |
| 152 | + userAttributes.name = name |
| 153 | + customAttributes.removeValue(forKey: "name") |
| 154 | + } |
| 155 | + |
| 156 | + if let phone = traits["phone"] as? String { |
| 157 | + userAttributes.phone = phone |
| 158 | + customAttributes.removeValue(forKey: "phone") |
| 159 | + } |
| 160 | + |
| 161 | + if let createdAt = traits["created_at"] as? Double { |
| 162 | + let date = Date(timeIntervalSince1970: createdAt) |
| 163 | + userAttributes.signedUpAt = date |
| 164 | + customAttributes.removeValue(forKey: "created_at") |
| 165 | + } |
| 166 | + |
| 167 | + if let integration = event?.integrations?.dictionaryValue?["Intercom"] as? [AnyHashable: Any] { |
| 168 | + if let languageOverride = integration["language_override"] as? String { |
| 169 | + userAttributes.languageOverride = languageOverride |
| 170 | + } |
| 171 | + |
| 172 | + if let unsubscribed = integration["unsubscribed"] as? Bool { |
| 173 | + userAttributes.unsubscribedFromEmails = unsubscribed |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if let company = traits["company"] as? [String: Any] { |
| 178 | + let companyData = setCompanyAttributes(company) |
| 179 | + userAttributes.companies = [companyData] |
| 180 | + } |
| 181 | + |
| 182 | + for (key, value) in traits { |
| 183 | + if !(value is String) && |
| 184 | + !(value is Int) && |
| 185 | + !(value is Double) && |
| 186 | + !(value is Bool) { |
| 187 | + customAttributes.removeValue(forKey: key) |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + userAttributes.customAttributes = customAttributes |
| 192 | + Intercom.updateUser(userAttributes) |
| 193 | + analytics?.log(message: "Intercom.updateUser(\(userAttributes)", kind: .debug) |
| 194 | + } |
| 195 | + |
| 196 | + func setCompanyAttributes(_ company: [String: Any]) -> ICMCompany { |
| 197 | + let companyData = ICMCompany() |
| 198 | + var customTraits = company |
| 199 | + |
| 200 | + if let companyId = company["id"] as? String { |
| 201 | + companyData.companyId = companyId |
| 202 | + customTraits.removeValue(forKey: "id") |
| 203 | + } |
| 204 | + |
| 205 | + if let monthlySpending = company["monthly_spend"] as? Double { |
| 206 | + companyData.monthlySpend = NSNumber(value: monthlySpending) |
| 207 | + customTraits.removeValue(forKey: "monthly_spend") |
| 208 | + } |
| 209 | + |
| 210 | + if let plan = company["plan"] as? String { |
| 211 | + companyData.plan = plan |
| 212 | + customTraits.removeValue(forKey: "plan") |
| 213 | + } |
| 214 | + |
| 215 | + if let createdAt = company["created_at"] as? Double { |
| 216 | + let date = Date(timeIntervalSince1970: createdAt) |
| 217 | + companyData.createdAt = date |
| 218 | + customTraits.removeValue(forKey: "created_at") |
| 219 | + } |
| 220 | + |
| 221 | + for (key, value) in company { |
| 222 | + if !(value is String) && |
| 223 | + !(value is Int) && |
| 224 | + !(value is Double) && |
| 225 | + !(value is Bool) { |
| 226 | + customTraits.removeValue(forKey: key) |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + companyData.customAttributes = customTraits |
| 231 | + |
| 232 | + return companyData |
| 233 | + } |
| 234 | +} |
0 commit comments