Skip to content

Commit

Permalink
📈 Track device updated events on re-identify
Browse files Browse the repository at this point in the history
  • Loading branch information
iujames authored and mmaatttt committed May 6, 2024
1 parent fa48cf6 commit 6b06959
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/AppcuesKit/Appcues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,17 @@ public class Appcues: NSObject {
storage.userSignature = properties?.removeValue(forKey: "appcues:user_id_signature") as? String
analyticsPublisher.publish(TrackingUpdate(type: .profile(interactive: true), properties: properties, isInternal: false))

// Track a device update on re-identify of the same user, since these will not trigger a new
// session start, but they do allow a force update of any device props that may have changed
// outside of the SDK in the application, i.e. push permission.
// This is interactive: true so it gets batched together with the identify in a single request
if !userChanged {
analyticsPublisher.publish(TrackingUpdate(
type: .event(name: Events.Device.deviceUpdated.rawValue, interactive: true),
isInternal: true
))
}

let pushMonitor = container.resolve(PushMonitoring.self)
pushMonitor.attemptDeferredNotificationResponse()
}
Expand Down
38 changes: 38 additions & 0 deletions Tests/AppcuesKitTests/AppcuesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,44 @@ class AppcuesTests: XCTestCase {
XCTAssertEqual(deferredPushAttempts, 1)
}

func testIdentifySameUserTriggersDeviceUpdate() throws {
// Arrange
let deviceUpdateExpectation = expectation(description: "Device updated")
appcues.analyticsPublisher.onPublish = { update in
if case .event("appcues:device_updated", true) = update.type {
deviceUpdateExpectation.fulfill()
}
}
appcues.storage.userID = "test-user"

// Act
appcues.identify(userID: "test-user") // re-identify

// Assert
waitForExpectations(timeout: 1)
}

func testIdentifyNewUserDoesNotTriggerDeviceUpdate() throws {
// no device update here since the new user will trigger a new session_started
// event with device props

// Arrange
let deviceUpdateExpectation = expectation(description: "Device updated")
deviceUpdateExpectation.isInverted = true
appcues.analyticsPublisher.onPublish = { update in
if case .event("appcues:device_updated", true) = update.type {
deviceUpdateExpectation.fulfill()
}
}
appcues.storage.userID = "test-user"

// Act
appcues.identify(userID: "different-user")

// Assert
waitForExpectations(timeout: 1)
}

func testReset() throws {
// Act
appcues.identify(userID: "test-user", properties: ["foo": 100])
Expand Down

0 comments on commit 6b06959

Please sign in to comment.