@@ -224,3 +224,232 @@ extension Analytics {
224224 process ( incomingEvent: event)
225225 }
226226}
227+
228+ // MARK: - Enrichment event signatures
229+
230+ extension Analytics {
231+ // Tracks an event performed by a user, including some additional event properties.
232+ /// - Parameters:
233+ /// - name: Name of the action, e.g., 'Purchased a T-Shirt'
234+ /// - properties: Properties specific to the named event. For example, an event with
235+ /// the name 'Purchased a Shirt' might have properties like revenue or size.
236+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
237+ public func track< P: Codable > ( name: String , properties: P ? , enrichments: [ EnrichmentClosure ] ? ) {
238+ do {
239+ if let properties = properties {
240+ let jsonProperties = try JSON ( with: properties)
241+ let event = TrackEvent ( event: name, properties: jsonProperties)
242+ process ( incomingEvent: event, enrichments: enrichments)
243+ } else {
244+ let event = TrackEvent ( event: name, properties: nil )
245+ process ( incomingEvent: event, enrichments: enrichments)
246+ }
247+ } catch {
248+ reportInternalError ( error, fatal: true )
249+ }
250+ }
251+
252+ /// Tracks an event performed by a user.
253+ /// - Parameters:
254+ /// - name: Name of the action, e.g., 'Purchased a T-Shirt'
255+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
256+ public func track( name: String , enrichments: [ EnrichmentClosure ] ? ) {
257+ track ( name: name, properties: nil as TrackEvent ? , enrichments: enrichments)
258+ }
259+
260+ /// Tracks an event performed by a user, including some additional event properties.
261+ /// - Parameters:
262+ /// - name: Name of the action, e.g., 'Purchased a T-Shirt'
263+ /// - properties: A dictionary or properties specific to the named event.
264+ /// For example, an event with the name 'Purchased a Shirt' might have properties
265+ /// like revenue or size.
266+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
267+ public func track( name: String , properties: [ String : Any ] ? , enrichments: [ EnrichmentClosure ] ? ) {
268+ var props : JSON ? = nil
269+ if let properties = properties {
270+ do {
271+ props = try JSON ( properties)
272+ } catch {
273+ reportInternalError ( error, fatal: true )
274+ }
275+ }
276+ let event = TrackEvent ( event: name, properties: props)
277+ process ( incomingEvent: event, enrichments: enrichments)
278+ }
279+
280+ /// Associate a user with their unique ID and record traits about them.
281+ /// - Parameters:
282+ /// - userId: A database ID for this user. If you don't have a userId
283+ /// but want to record traits, just pass traits into the event and they will be associated
284+ /// with the anonymousId of that user. In the case when user logs out, make sure to
285+ /// call ``reset()`` to clear the user's identity info. For more information on how we
286+ /// generate the UUID and Apple's policies on IDs, see
287+ /// https://segment.io/libraries/ios#ids
288+ /// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
289+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
290+ public func identify< T: Codable > ( userId: String , traits: T ? , enrichments: [ EnrichmentClosure ] ? ) {
291+ do {
292+ if let traits = traits {
293+ let jsonTraits = try JSON ( with: traits)
294+ store. dispatch ( action: UserInfo . SetUserIdAndTraitsAction ( userId: userId, traits: jsonTraits) )
295+ let event = IdentifyEvent ( userId: userId, traits: jsonTraits)
296+ process ( incomingEvent: event, enrichments: enrichments)
297+ } else {
298+ store. dispatch ( action: UserInfo . SetUserIdAndTraitsAction ( userId: userId, traits: nil ) )
299+ let event = IdentifyEvent ( userId: userId, traits: nil )
300+ process ( incomingEvent: event, enrichments: enrichments)
301+ }
302+ } catch {
303+ reportInternalError ( error, fatal: true )
304+ }
305+ }
306+
307+ /// Associate a user with their unique ID and record traits about them.
308+ /// - Parameters:
309+ /// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
310+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
311+ public func identify< T: Codable > ( traits: T , enrichments: [ EnrichmentClosure ] ? ) {
312+ do {
313+ let jsonTraits = try JSON ( with: traits)
314+ store. dispatch ( action: UserInfo . SetTraitsAction ( traits: jsonTraits) )
315+ let event = IdentifyEvent ( traits: jsonTraits)
316+ process ( incomingEvent: event, enrichments: enrichments)
317+ } catch {
318+ reportInternalError ( error, fatal: true )
319+ }
320+ }
321+
322+ /// Associate a user with their unique ID and record traits about them.
323+ /// - Parameters:
324+ /// - userId: A database ID for this user.
325+ /// For more information on how we generate the UUID and Apple's policies on IDs, see
326+ /// https://segment.io/libraries/ios#ids
327+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
328+ /// In the case when user logs out, make sure to call ``reset()`` to clear user's identity info.
329+ public func identify( userId: String , enrichments: [ EnrichmentClosure ] ? ) {
330+ let event = IdentifyEvent ( userId: userId, traits: nil )
331+ store. dispatch ( action: UserInfo . SetUserIdAction ( userId: userId) )
332+ process ( incomingEvent: event, enrichments: enrichments)
333+ }
334+
335+ /// Associate a user with their unique ID and record traits about them.
336+ /// - Parameters:
337+ /// - userId: A database ID for this user. If you don't have a userId
338+ /// but want to record traits, just pass traits into the event and they will be associated
339+ /// with the anonymousId of that user. In the case when user logs out, make sure to
340+ /// call ``reset()`` to clear the user's identity info. For more information on how we
341+ /// generate the UUID and Apple's policies on IDs, see
342+ /// https://segment.io/libraries/ios#ids
343+ /// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
344+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
345+ /// In the case when user logs out, make sure to call ``reset()`` to clear user's identity info.
346+ public func identify( userId: String , traits: [ String : Any ] ? = nil , enrichments: [ EnrichmentClosure ] ? ) {
347+ do {
348+ if let traits = traits {
349+ let traits = try JSON ( traits as Any )
350+ store. dispatch ( action: UserInfo . SetUserIdAndTraitsAction ( userId: userId, traits: traits) )
351+ let event = IdentifyEvent ( userId: userId, traits: traits)
352+ process ( incomingEvent: event, enrichments: enrichments)
353+ } else {
354+ store. dispatch ( action: UserInfo . SetUserIdAndTraitsAction ( userId: userId, traits: nil ) )
355+ let event = IdentifyEvent ( userId: userId, traits: nil )
356+ process ( incomingEvent: event, enrichments: enrichments)
357+ }
358+ } catch {
359+ reportInternalError ( error, fatal: true )
360+ }
361+ }
362+
363+ /// Track a screen change with a title, category and other properties.
364+ /// - Parameters:
365+ /// - screenTitle: The title of the screen being tracked.
366+ /// - category: A category to the type of screen if it applies.
367+ /// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
368+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
369+ public func screen< P: Codable > ( title: String , category: String ? = nil , properties: P ? , enrichments: [ EnrichmentClosure ] ? ) {
370+ do {
371+ if let properties = properties {
372+ let jsonProperties = try JSON ( with: properties)
373+ let event = ScreenEvent ( title: title, category: category, properties: jsonProperties)
374+ process ( incomingEvent: event, enrichments: enrichments)
375+ } else {
376+ let event = ScreenEvent ( title: title, category: category)
377+ process ( incomingEvent: event, enrichments: enrichments)
378+ }
379+ } catch {
380+ reportInternalError ( error, fatal: true )
381+ }
382+ }
383+
384+ /// Track a screen change with a title, category and other properties.
385+ /// - Parameters:
386+ /// - screenTitle: The title of the screen being tracked.
387+ /// - category: A category to the type of screen if it applies.
388+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
389+ public func screen( title: String , category: String ? = nil , enrichments: [ EnrichmentClosure ] ? ) {
390+ screen ( title: title, category: category, properties: nil as ScreenEvent ? , enrichments: enrichments)
391+ }
392+
393+ /// Track a screen change with a title, category and other properties.
394+ /// - Parameters:
395+ /// - screenTitle: The title of the screen being tracked.
396+ /// - category: A category to the type of screen if it applies.
397+ /// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
398+ /// - enrichments: Enrichments to be applied to this specific event only, or `nil` for none.
399+ public func screen( title: String , category: String ? = nil , properties: [ String : Any ] ? , enrichments: [ EnrichmentClosure ] ? ) {
400+ // if properties is nil, this is the event that'll get used.
401+ var event = ScreenEvent ( title: title, category: category, properties: nil )
402+ // if we have properties, get a new one rolling.
403+ if let properties = properties {
404+ do {
405+ let jsonProperties = try JSON ( properties)
406+ event = ScreenEvent ( title: title, category: category, properties: jsonProperties)
407+ } catch {
408+ reportInternalError ( error, fatal: true )
409+ }
410+ }
411+ process ( incomingEvent: event, enrichments: enrichments)
412+ }
413+
414+ public func group< T: Codable > ( groupId: String , traits: T ? , enrichments: [ EnrichmentClosure ] ? ) {
415+ do {
416+ if let traits = traits {
417+ let jsonTraits = try JSON ( with: traits)
418+ let event = GroupEvent ( groupId: groupId, traits: jsonTraits)
419+ process ( incomingEvent: event)
420+ } else {
421+ let event = GroupEvent ( groupId: groupId)
422+ process ( incomingEvent: event)
423+ }
424+ } catch {
425+ reportInternalError ( error, fatal: true )
426+ }
427+ }
428+
429+ public func group( groupId: String , enrichments: [ EnrichmentClosure ] ? ) {
430+ group ( groupId: groupId, traits: nil as GroupEvent ? , enrichments: enrichments)
431+ }
432+
433+ /// Associate a user with a group such as a company, organization, project, etc.
434+ /// - Parameters:
435+ /// - groupId: A unique identifier for the group identification in your system.
436+ /// - traits: Traits of the group you may be interested in such as email, phone or name.
437+ public func group( groupId: String , traits: [ String : Any ] ? , enrichments: [ EnrichmentClosure ] ? ) {
438+ var event = GroupEvent ( groupId: groupId)
439+ if let traits = traits {
440+ do {
441+ let jsonTraits = try JSON ( traits)
442+ event = GroupEvent ( groupId: groupId, traits: jsonTraits)
443+ } catch {
444+ reportInternalError ( error, fatal: true )
445+ }
446+ }
447+ process ( incomingEvent: event, enrichments: enrichments)
448+ }
449+
450+ public func alias( newId: String , enrichments: [ EnrichmentClosure ] ? ) {
451+ let event = AliasEvent ( newId: newId, previousId: self . userId)
452+ store. dispatch ( action: UserInfo . SetUserIdAction ( userId: newId) )
453+ process ( incomingEvent: event, enrichments: enrichments)
454+ }
455+ }
0 commit comments