Skip to content

Commit

Permalink
✨ Add device events
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt authored and iujames committed Sep 17, 2024
1 parent 6f667c2 commit 2245366
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Sources/AppcuesKit/Appcues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ public class Appcues: NSObject {
/// Clears out the current user in this session. Can be used when the user logs out of your application.
@objc
public func reset() {
analyticsPublisher.publish(TrackingUpdate(
type: .event(name: Events.Device.deviceUnregistered.rawValue, interactive: false),
isInternal: true
))

sessionMonitor.reset()

storage.userID = ""
Expand Down Expand Up @@ -241,6 +246,13 @@ public class Appcues: NSObject {
@objc
public func setPushToken(_ deviceToken: Data?) {
storage.pushToken = deviceToken?.map { String(format: "%02x", $0) }.joined()

if sessionID != nil {
analyticsPublisher.publish(TrackingUpdate(
type: .event(name: Events.Device.deviceUpdated.rawValue, interactive: false),
isInternal: true
))
}
}

/// Register a trait that modifies an `Experience`.
Expand Down
8 changes: 6 additions & 2 deletions Sources/AppcuesKit/Data/Analytics/AutoPropertyDecorator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ internal class AutoPropertyDecorator: AnalyticsDecorating {
decorated.identityAutoProperties = merged
}

// TODO: add _device to new events
if case .event(Events.Session.sessionStarted.rawValue, _) = tracking.type {
switch tracking.type {
case .event(Events.Session.sessionStarted.rawValue, _),
.event(Events.Device.deviceUpdated.rawValue, _),
.event(Events.Device.deviceUnregistered.rawValue, _):
decorated.deviceAutoProperties = deviceAutoProperties().merging(applicationProperties)
default:
break
}

decorated.context = context
Expand Down
5 changes: 5 additions & 0 deletions Sources/AppcuesKit/Data/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ internal enum Events {
case sessionStarted = "appcues:session_started"
}

enum Device: String {
case deviceUpdated = "appcues:device_updated"
case deviceUnregistered = "appcues:device_unregistered"
}

enum Experience: String {
case stepSeen = "appcues:v2:step_seen"
case stepInteraction = "appcues:v2:step_interaction"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ internal struct LoggedEvent: Identifiable {
case let .event(name, _) where Events.Experience(rawValue: name) != nil:
self.type = .experience
self.name = name.prettifiedEventName
case let .event(name, _) where Events.Device(rawValue: name) != nil:
self.type = .device
self.name = name.prettifiedEventName
case let .event(name, _):
self.type = .custom
self.name = name
Expand Down Expand Up @@ -130,6 +133,7 @@ extension LoggedEvent {
case group
case session
case experience
case device

var description: String {
switch self {
Expand All @@ -139,6 +143,7 @@ extension LoggedEvent {
case .group: return "Group"
case .session: return "Session"
case .experience: return "Experience"
case .device: return "Device"
}
}

Expand All @@ -150,6 +155,7 @@ extension LoggedEvent {
case .group: return "person.3"
case .session: return "clock.arrow.2.circlepath"
case .experience: return "arrow.right.square"
case .device: return "iphone"
}
}
}
Expand Down

0 comments on commit 2245366

Please sign in to comment.