@@ -9,16 +9,10 @@ import Foundation
9
9
import Sovran
10
10
11
11
12
- // MARK: - Event Parameter Types
13
-
14
- typealias Integrations = Codable
15
- typealias Properties = Codable
16
- typealias Traits = Codable
17
-
18
-
19
12
// MARK: - Event Types
20
13
21
14
public protocol RawEvent : Codable {
15
+
22
16
var type : String ? { get set }
23
17
var anonymousId : String ? { get set }
24
18
var messageId : String ? { get set }
@@ -149,11 +143,192 @@ public struct AliasEvent: RawEvent {
149
143
}
150
144
}
151
145
146
+ // MARK: - RawEvent conveniences
147
+
148
+ internal struct IntegrationConstants {
149
+ static let allIntegrationsKey = " All "
150
+ }
151
+
152
+ extension RawEvent {
153
+ /**
154
+ Disable all cloud-mode integrations for this event, except for any specific keys given.
155
+ This will preserve any per-integration specific settings if the integration is to remain enabled.
156
+ - Parameters:
157
+ - exceptKeys: A list of integration keys to exclude from disabling.
158
+ */
159
+ public mutating func disableCloudIntegrations( exceptKeys: [ String ] ? = nil ) {
160
+ guard let existing = integrations? . dictionaryValue else {
161
+ // this shouldn't happen, might oughta log it.
162
+ Analytics . segmentLog ( message: " Unable to get what should be a valid list of integrations from event. " , kind: . error)
163
+ return
164
+ }
165
+ var new = [ String: Any] ( )
166
+ new [ IntegrationConstants . allIntegrationsKey] = false
167
+ if let exceptKeys = exceptKeys {
168
+ for key in exceptKeys {
169
+ if let value = existing [ key] , value is [ String : Any ] {
170
+ new [ key] = value
171
+ } else {
172
+ new [ key] = true
173
+ }
174
+ }
175
+ }
176
+
177
+ do {
178
+ integrations = try JSON ( new)
179
+ } catch {
180
+ // this shouldn't happen, log it.
181
+ Analytics . segmentLog ( message: " Unable to convert list of integrations to JSON. \( error) " , kind: . error)
182
+ }
183
+ }
184
+
185
+ /**
186
+ Enable all cloud-mode integrations for this event, except for any specific keys given.
187
+ - Parameters:
188
+ - exceptKeys: A list of integration keys to exclude from enabling.
189
+ */
190
+ public mutating func enableCloudIntegrations( exceptKeys: [ String ] ? = nil ) {
191
+ var new = [ String: Any] ( )
192
+ new [ IntegrationConstants . allIntegrationsKey] = true
193
+ if let exceptKeys = exceptKeys {
194
+ for key in exceptKeys {
195
+ new [ key] = false
196
+ }
197
+ }
198
+
199
+ do {
200
+ integrations = try JSON ( new)
201
+ } catch {
202
+ // this shouldn't happen, log it.
203
+ Analytics . segmentLog ( message: " Unable to convert list of integrations to JSON. \( error) " , kind: . error)
204
+ }
205
+ }
206
+
207
+ /**
208
+ Disable a specific cloud-mode integration using it's key name.
209
+ - Parameters:
210
+ - key: The key name of the integration to disable.
211
+ */
212
+ public mutating func disableIntegration( key: String ) {
213
+ guard let existing = integrations? . dictionaryValue else {
214
+ // this shouldn't happen, might oughta log it.
215
+ Analytics . segmentLog ( message: " Unable to get what should be a valid list of integrations from event. " , kind: . error)
216
+ return
217
+ }
218
+ // we don't really care what the value of this key was before, as
219
+ // a disabled one can only be false.
220
+ var new = existing
221
+ new [ key] = false
222
+
223
+ do {
224
+ integrations = try JSON ( new)
225
+ } catch {
226
+ // this shouldn't happen, log it.
227
+ Analytics . segmentLog ( message: " Unable to convert list of integrations to JSON. \( error) " , kind: . error)
228
+ }
229
+ }
230
+
231
+ /**
232
+ Enable a specific cloud-mode integration using it's key name.
233
+ - Parameters:
234
+ - key: The key name of the integration to enable.
235
+ */
236
+ public mutating func enableIntegration( key: String ) {
237
+ guard let existing = integrations? . dictionaryValue else {
238
+ // this shouldn't happen, might oughta log it.
239
+ Analytics . segmentLog ( message: " Unable to get what should be a valid list of integrations from event. " , kind: . error)
240
+ return
241
+ }
242
+
243
+ var new = existing
244
+ // if it's a dictionary already, it's considered enabled, so don't
245
+ // overwrite whatever they may have put there. If that's not the case
246
+ // just set it to true since that's the only other value it could have
247
+ // to be considered `enabled`.
248
+ if ( existing [ key] as? [ String : Any ] ) == nil {
249
+ new [ key] = true
250
+ }
251
+
252
+ do {
253
+ integrations = try JSON ( new)
254
+ } catch {
255
+ // this shouldn't happen, log it.
256
+ Analytics . segmentLog ( message: " Unable to convert list of integrations to JSON. \( error) " , kind: . error)
257
+ }
258
+ }
259
+
260
+ /**
261
+ Set values to be received for this event in cloud-mode, specific to an integration key path.
262
+ Note that when specifying nil as the value, the key will be removed for the given key path. Additionally,
263
+ any keys that don't already exist in the path will be created as necessary.
264
+
265
+ Example:
266
+ ```
267
+ trackEvent.setIntegrationValue(42, forKeyPath: "Amplitude.threshold")
268
+ ```
269
+
270
+ - Parameters:
271
+ - value: The value to set for the given keyPath, or nil.
272
+ - forKeyPath: The key path for the value.
273
+ */
274
+ public mutating func setIntegrationValue( _ value: Any ? , forKeyPath keyPath: String ) {
275
+ guard let existing = integrations? . dictionaryValue else {
276
+ // this shouldn't happen, might oughta log it.
277
+ Analytics . segmentLog ( message: " Unable to get what should be a valid list of integrations from event. " , kind: . error)
278
+ return
279
+ }
280
+
281
+ var new = existing
282
+ new [ keyPath: KeyPath ( keyPath) ] = value
283
+ do {
284
+ integrations = try JSON ( new)
285
+ } catch {
286
+ // this shouldn't happen, log it.
287
+ Analytics . segmentLog ( message: " Unable to convert list of integrations to JSON. \( error) " , kind: . error)
288
+ }
289
+ }
290
+
291
+ /**
292
+ Set context values for this event.
293
+ Note that when specifying nil as the value, the key will be removed for the given key path. Additionally,
294
+ any keys that don't already exist in the path will be created as necessary.
295
+
296
+ Example:
297
+ ```
298
+ // the metadata key will be created as a dictionary, and the key nickname will be set.
299
+ trackEvent.setContextValue("Brandon's device", forKeyPath: "device.metadata.nickname")
300
+
301
+ // the metadata key will be removed entirely.
302
+ trackEvent.setContextValue(nil, forKeyPath: "device.metadata")
303
+ ```
304
+
305
+ - Parameters:
306
+ - value: The value to set for the given keyPath, or nil.
307
+ - forKeyPath: The key path for the value.
308
+ */
309
+ public mutating func setContextValue( _ value: Any ? , forKeyPath keyPath: String ) {
310
+ guard let existing = context? . dictionaryValue else {
311
+ // this shouldn't happen, might oughta log it.
312
+ Analytics . segmentLog ( message: " Unable to get what should be a valid context from event. " , kind: . error)
313
+ return
314
+ }
315
+
316
+ var new = existing
317
+ new [ keyPath: KeyPath ( keyPath) ] = value
318
+ do {
319
+ context = try JSON ( new)
320
+ } catch {
321
+ // this shouldn't happen, log it.
322
+ Analytics . segmentLog ( message: " Unable to convert context to JSON. \( error) " , kind: . error)
323
+ }
324
+ }
325
+ }
326
+
152
327
153
328
// MARK: - RawEvent data helpers
154
329
155
330
extension RawEvent {
156
- public mutating func applyRawEventData( event: RawEvent ? ) {
331
+ internal mutating func applyRawEventData( event: RawEvent ? ) {
157
332
if let e = event {
158
333
anonymousId = e. anonymousId
159
334
messageId = e. messageId
0 commit comments