Skip to content

Commit

Permalink
Use new session id format
Browse files Browse the repository at this point in the history
  • Loading branch information
goenning committed Nov 3, 2023
1 parent b9540f0 commit b00940f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Aptabase.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Aptabase'
s.version = '0.3.3'
s.version = '0.3.4'
s.summary = 'Swift SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps'
s.homepage = 'https://aptabase.com'
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.4

* Use new session id format

## 0.3.3

* Added Privacy Manifest (PrivacyInfo.xcprivacy)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
...
dependencies: [
...
.package(name: "Aptabase", url: "https://github.com/aptabase/aptabase-swift.git", from: "0.3.3"),
.package(name: "Aptabase", url: "https://github.com/aptabase/aptabase-swift.git", from: "0.3.4"),
],
targets: [
.target(
Expand All @@ -39,7 +39,7 @@ Use this [guide](https://developer.apple.com/documentation/xcode/adding-package-
Aptabase is also available through CocoaPods. To install it, simply add the following line to your Podfile:

```ruby
pod 'Aptabase', :git => 'https://github.com/aptabase/aptabase-swift.git', :tag => '0.3.3'
pod 'Aptabase', :git => 'https://github.com/aptabase/aptabase-swift.git', :tag => '0.3.4'
```


Expand Down
12 changes: 9 additions & 3 deletions Sources/Aptabase/AptabaseClient.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Foundation

class AptabaseClient {
private static let sdkVersion = "aptabase-swift@0.3.3"
private static let sdkVersion = "aptabase-swift@0.3.4"
// Session expires after 1 hour of inactivity
private static let sessionTimeout: TimeInterval = 1 * 60 * 60

private var sessionId = UUID()
private var sessionId = newSessionId()
private var lastTouched = Date()
private var flushTimer: Timer?
private let dispatcher: EventDispatcher
Expand All @@ -22,7 +22,7 @@ class AptabaseClient {
public func trackEvent(_ eventName: String, with props: [String: AnyCodableValue] = [:]) {
let now = Date()
if lastTouched.distance(to: now) > AptabaseClient.sessionTimeout {
sessionId = UUID()
sessionId = AptabaseClient.newSessionId()
}
lastTouched = now

Expand Down Expand Up @@ -58,6 +58,12 @@ class AptabaseClient {
public func flush() async {
await dispatcher.flush()
}

private static func newSessionId() -> String {
let epochInSeconds = UInt64(Date().timeIntervalSince1970)
let random = UInt64.random(in: 0...99999999)
return String(epochInSeconds * 100000000 + random)
}

@objc private func flushSync() {
let semaphore = DispatchSemaphore(value: 0)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Aptabase/EventDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

struct Event: Encodable {
var timestamp: Date
var sessionId: UUID
var sessionId: String
var eventName: String
var systemProps: SystemProps
var props: [String: AnyCodableValue]?
Expand Down

0 comments on commit b00940f

Please sign in to comment.