Skip to content

Commit

Permalink
fix: add setSessionId() method
Browse files Browse the repository at this point in the history
  • Loading branch information
falconandy committed Oct 24, 2023
1 parent 746d433 commit 9a096d3
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Sources/Amplitude/Amplitude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ public class Amplitude {
return sessions.sessionId
}

@discardableResult
public func setSessionId(timestamp: Int64) -> Amplitude {
let sessionEvents = sessions.assignEventId(events: sessions.startNewSession(timestamp: timestamp))
sessionEvents.forEach { e in timeline.processEvent(event: e) }
return self
}

@discardableResult
public func reset() -> Amplitude {
_ = setUserId(userId: nil)
Expand Down
7 changes: 7 additions & 0 deletions Sources/Amplitude/ObjC/ObjCAmplitude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ public class ObjCAmplitude: NSObject {
amplitude.getSessionId()
}

@objc(setSessionId:)
@discardableResult
public func setSessionId(timestamp: Int64) -> ObjCAmplitude {
amplitude.setSessionId(timestamp: timestamp)
return self
}

@objc
@discardableResult
public func reset() -> ObjCAmplitude {
Expand Down
8 changes: 6 additions & 2 deletions Sources/Amplitude/Sessions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ public class Sessions {
result.append(event)
}

return assignEventId(events: result)
}

func assignEventId(events: [BaseEvent]) -> [BaseEvent] {
var newLastEventId = self.lastEventId

result.forEach({ event in
events.forEach({ event in
if event.eventId == nil {
newLastEventId += 1
event.eventId = newLastEventId
Expand All @@ -92,7 +96,7 @@ public class Sessions {

self.lastEventId = newLastEventId

return result
return events
}

private func isWithinMinTimeBetweenSessions(timestamp: Int64) -> Bool {
Expand Down
97 changes: 97 additions & 0 deletions Tests/AmplitudeTests/AmplitudeSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,103 @@ final class AmplitudeSessionTests: XCTestCase {
XCTAssertEqual(event.eventId, lastEventId+4)
}

func testSetSessionIdInBackgroundShouldStartNewSession() throws {
let lastEventId: Int64 = 123
try storageMem.write(key: StorageKey.LAST_EVENT_ID, value: lastEventId)

let amplitude = Amplitude(configuration: configuration)

let eventCollector = EventCollectorPlugin()
amplitude.add(plugin: eventCollector)

amplitude.track(event: BaseEvent(userId: "user", timestamp: 100, eventType: "test event 1"))
amplitude.setSessionId(timestamp: 150)
amplitude.track(event: BaseEvent(userId: "user", timestamp: 200, eventType: "test event 2"))

let collectedEvents = eventCollector.events

XCTAssertEqual(collectedEvents.count, 5)

var event = collectedEvents[0]
XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT)
XCTAssertEqual(event.sessionId, 100)
XCTAssertEqual(event.timestamp, 100)
XCTAssertEqual(event.eventId, lastEventId+1)

event = collectedEvents[1]
XCTAssertEqual(event.eventType, "test event 1")
XCTAssertEqual(event.sessionId, 100)
XCTAssertEqual(event.timestamp, 100)
XCTAssertEqual(event.eventId, lastEventId+2)

event = collectedEvents[2]
XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT)
XCTAssertEqual(event.sessionId, 100)
XCTAssertEqual(event.timestamp, 100)
XCTAssertEqual(event.eventId, lastEventId+3)

event = collectedEvents[3]
XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT)
XCTAssertEqual(event.sessionId, 150)
XCTAssertEqual(event.timestamp, 150)
XCTAssertEqual(event.eventId, lastEventId+4)

event = collectedEvents[4]
XCTAssertEqual(event.eventType, "test event 2")
XCTAssertEqual(event.sessionId, 150)
XCTAssertEqual(event.timestamp, 200)
XCTAssertEqual(event.eventId, lastEventId+5)
}

func testSetSessionIdInForegroundShouldStartNewSession() throws {
let lastEventId: Int64 = 123
try storageMem.write(key: StorageKey.LAST_EVENT_ID, value: lastEventId)

let amplitude = Amplitude(configuration: configuration)

let eventCollector = EventCollectorPlugin()
amplitude.add(plugin: eventCollector)

amplitude.onEnterForeground(timestamp: 1000)
amplitude.track(event: BaseEvent(userId: "user", timestamp: 1050, eventType: "test event 1"))
amplitude.setSessionId(timestamp: 1100)
amplitude.track(event: BaseEvent(userId: "user", timestamp: 2000, eventType: "test event 2"))

let collectedEvents = eventCollector.events

XCTAssertEqual(collectedEvents.count, 5)

var event = collectedEvents[0]
XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT)
XCTAssertEqual(event.sessionId, 1000)
XCTAssertEqual(event.timestamp, 1000)
XCTAssertEqual(event.eventId, lastEventId+1)

event = collectedEvents[1]
XCTAssertEqual(event.eventType, "test event 1")
XCTAssertEqual(event.sessionId, 1000)
XCTAssertEqual(event.timestamp, 1050)
XCTAssertEqual(event.eventId, lastEventId+2)

event = collectedEvents[2]
XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT)
XCTAssertEqual(event.sessionId, 1000)
XCTAssertEqual(event.timestamp, 1050)
XCTAssertEqual(event.eventId, lastEventId+3)

event = collectedEvents[3]
XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT)
XCTAssertEqual(event.sessionId, 1100)
XCTAssertEqual(event.timestamp, 1100)
XCTAssertEqual(event.eventId, lastEventId+4)

event = collectedEvents[4]
XCTAssertEqual(event.eventType, "test event 2")
XCTAssertEqual(event.sessionId, 1100)
XCTAssertEqual(event.timestamp, 2000)
XCTAssertEqual(event.eventId, lastEventId+5)
}

func getDictionary(_ props: [String: Any?]) -> NSDictionary {
return NSDictionary(dictionary: props as [AnyHashable: Any])
}
Expand Down

0 comments on commit 9a096d3

Please sign in to comment.