From 5356338d679520fe25cd3e787ce7c4e875bf0fb0 Mon Sep 17 00:00:00 2001 From: Alan Charles <50601149+alanjcharles@users.noreply.github.com> Date: Thu, 25 Jan 2024 12:18:34 -0700 Subject: [PATCH] feat: add reset() to plugin (#15) * update analytics * clear sessionID on reset() * fix app open session logic --- .../xcshareddata/swiftpm/Package.resolved | 17 +++++++++++++---- Package.swift | 2 +- Sources/SegmentAmplitude/AmplitudeSession.swift | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Example/BasicExample/BasicExample.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/BasicExample/BasicExample.xcworkspace/xcshareddata/swiftpm/Package.resolved index 60bcf69..676b096 100644 --- a/Example/BasicExample/BasicExample.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/BasicExample/BasicExample.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -6,8 +6,17 @@ "repositoryURL": "https://github.com/segmentio/analytics-swift", "state": { "branch": null, - "revision": "79fb17a5d4abf8f80e6a0935e57c7df7b670a6c0", - "version": "1.4.1" + "revision": "51f56b972de8daee251f18fd2c91fa3a33c8d77e", + "version": "1.5.2" + } + }, + { + "package": "JSONSafeEncoder", + "repositoryURL": "https://github.com/segmentio/jsonsafeencoder-swift.git", + "state": { + "branch": null, + "revision": "8b70dc8c01b7b041912e30e29d2b488a43f782ac", + "version": "1.0.1" } }, { @@ -15,8 +24,8 @@ "repositoryURL": "https://github.com/segmentio/Sovran-Swift.git", "state": { "branch": null, - "revision": "944c17d7c46bd95fc37f09136cabd172be5b413b", - "version": "1.0.3" + "revision": "64f3b5150c282a34af4578188dce2fd597e600e3", + "version": "1.1.0" } } ] diff --git a/Package.swift b/Package.swift index 01e0f3a..37806d2 100644 --- a/Package.swift +++ b/Package.swift @@ -22,7 +22,7 @@ let package = Package( .package( name: "Segment", url: "https://github.com/segmentio/analytics-swift.git", - from: "1.4.1" + from: "1.5.2" ) ], targets: [ diff --git a/Sources/SegmentAmplitude/AmplitudeSession.swift b/Sources/SegmentAmplitude/AmplitudeSession.swift index 3c33b92..6d55f02 100644 --- a/Sources/SegmentAmplitude/AmplitudeSession.swift +++ b/Sources/SegmentAmplitude/AmplitudeSession.swift @@ -66,20 +66,22 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle { return event } - lastEventFiredTime = Date() - var result: T? = event switch result { case let r as IdentifyEvent: result = self.identify(event: r) as? T + lastEventFiredTime = Date() case let r as TrackEvent: result = self.track(event: r) as? T case let r as ScreenEvent: result = self.screen(event: r) as? T + lastEventFiredTime = Date() case let r as AliasEvent: result = self.alias(event: r) as? T + lastEventFiredTime = Date() case let r as GroupEvent: result = self.group(event: r) as? T + lastEventFiredTime = Date() default: break } @@ -87,6 +89,10 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle { } public func track(event: TrackEvent) -> TrackEvent? { + if event.event != "Application Opened" { + lastEventFiredTime = Date() + } + guard let returnEvent = insertSession(event: event) as? TrackEvent else { return nil } @@ -121,6 +127,10 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle { return returnEvent } + public func reset() { + sessionID = nil + } + public func applicationWillEnterForeground(application: UIApplication?) { if Date().timeIntervalSince(lastEventFiredTime) >= minSessionTime { sessionID = Date().timeIntervalSince1970