Skip to content

Commit cc48275

Browse files
committed
Add automatic session timeout after 1 hour of inactivity
1 parent aa75f78 commit cc48275

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.3
2+
3+
- Add automatic session timeout after 1 hour of inactivity
4+
15
## 0.1.2
26

37
- Added support for automatic segregation of Debug/Release events

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aptabase/web",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"private": false,
55
"type": "module",
66
"description": "JavaScript SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",

src/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ export type AptabaseOptions = {
88
appVersion?: string;
99
};
1010

11+
// Session expires after 1 hour of inactivity
12+
const SESSION_TIMEOUT = 1 * 60 * 60;
13+
let _sessionId = newSessionId();
14+
let _lastTouched = new Date();
1115
let _appKey = "";
12-
let _locale = "";
1316
let _apiUrl = "";
14-
let _sessionId = newSessionId();
17+
let _locale = "";
1518
let _isDebug = false;
1619
let _options: AptabaseOptions | undefined;
1720

@@ -72,6 +75,14 @@ export function trackEvent(
7275
) {
7376
if (!_appKey || typeof window === "undefined" || !window.fetch) return;
7477

78+
let now = new Date();
79+
const diffInMs = now.getTime() - _lastTouched.getTime();
80+
const diffInSec = Math.floor(diffInMs / 1000);
81+
if (diffInSec > SESSION_TIMEOUT) {
82+
_sessionId = newSessionId();
83+
}
84+
_lastTouched = now;
85+
7586
const body = JSON.stringify({
7687
timestamp: new Date().toISOString(),
7788
sessionId: _sessionId,

0 commit comments

Comments
 (0)