Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add setSessionId() method #95

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/Amplitude/Amplitude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ public class Amplitude {
return sessions.sessionId
}

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

@discardableResult
public func setSessionId(date: Date) -> Amplitude {
let timestamp = Int64(date.timeIntervalSince1970 * 1000)
setSessionId(timestamp: timestamp)
return self
}

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

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

@objc(setSessionIdWithDate:)
@discardableResult
public func setSessionId(date: Date) -> ObjCAmplitude {
amplitude.setSessionId(date: date)
return self
}

@objc
@discardableResult
public func reset() -> ObjCAmplitude {
Expand Down
25 changes: 23 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 Expand Up @@ -138,4 +142,21 @@ public class Sessions {

return sessionEvents
}

public func endCurrentSession() -> [BaseEvent] {
var sessionEvents: [BaseEvent] = Array()
let trackingSessionEvents = amplitude.configuration.defaultTracking.sessions

if trackingSessionEvents && self.sessionId >= 0 {
let sessionEndEvent = BaseEvent(
timestamp: self.lastEventTime > 0 ? self.lastEventTime : nil,
sessionId: self.sessionId,
eventType: Constants.AMP_SESSION_END_EVENT
)
sessionEvents.append(sessionEndEvent)
}

self.sessionId = -1
return sessionEvents
}
}
204 changes: 204 additions & 0 deletions Tests/AmplitudeTests/AmplitudeSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,210 @@ 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 testSessionEndInBackgroundShouldEndCurrentSession() 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: 1000, eventType: "test event 1"))
XCTAssertEqual(amplitude.sessionId, 1000)

amplitude.setSessionId(timestamp: -1)
XCTAssertEqual(amplitude.sessionId, -1)

amplitude.track(event: BaseEvent(userId: "user", timestamp: 2000, eventType: "test event 2"))
XCTAssertEqual(amplitude.sessionId, 2000)

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, 1000)
XCTAssertEqual(event.eventId, lastEventId+2)

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

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

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

func testSessionEndInForegroundShouldEndCurrentSession() 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: 1500, eventType: "test event 1"))
XCTAssertEqual(amplitude.sessionId, 1000)

amplitude.setSessionId(timestamp: -1)
XCTAssertEqual(amplitude.sessionId, -1)

amplitude.track(event: BaseEvent(userId: "user", timestamp: 2000, eventType: "test event 2"))
XCTAssertEqual(amplitude.sessionId, 2000)

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, 1500)
XCTAssertEqual(event.eventId, lastEventId+2)

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

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

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

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