Skip to content

Commit 14ac114

Browse files
committed
Added enrichment for other events.
1 parent ebf0a93 commit 14ac114

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

Sources/Segment/Events.swift

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,179 @@ extension Analytics {
277277
process(incomingEvent: event, enrichments: enrichments)
278278
}
279279

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+
}
280455
}

0 commit comments

Comments
 (0)