Add automatic session timeout after 1 hour of inactivity
This commit is contained in:
parent
aa75f78fbb
commit
cc48275c37
4 changed files with 21 additions and 6 deletions
|
@ -1,3 +1,7 @@
|
|||
## 0.1.3
|
||||
|
||||
- Add automatic session timeout after 1 hour of inactivity
|
||||
|
||||
## 0.1.2
|
||||
|
||||
- Added support for automatic segregation of Debug/Release events
|
||||
|
|
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@aptabase/web",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.3",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@aptabase/web",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.3",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-replace": "5.0.2",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@aptabase/web",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.3",
|
||||
"private": false,
|
||||
"type": "module",
|
||||
"description": "JavaScript SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
|
||||
|
|
17
src/index.ts
17
src/index.ts
|
@ -8,10 +8,13 @@ export type AptabaseOptions = {
|
|||
appVersion?: string;
|
||||
};
|
||||
|
||||
let _appKey = "";
|
||||
let _locale = "";
|
||||
let _apiUrl = "";
|
||||
// Session expires after 1 hour of inactivity
|
||||
const SESSION_TIMEOUT = 1 * 60 * 60;
|
||||
let _sessionId = newSessionId();
|
||||
let _lastTouched = new Date();
|
||||
let _appKey = "";
|
||||
let _apiUrl = "";
|
||||
let _locale = "";
|
||||
let _isDebug = false;
|
||||
let _options: AptabaseOptions | undefined;
|
||||
|
||||
|
@ -72,6 +75,14 @@ export function trackEvent(
|
|||
) {
|
||||
if (!_appKey || typeof window === "undefined" || !window.fetch) return;
|
||||
|
||||
let now = new Date();
|
||||
const diffInMs = now.getTime() - _lastTouched.getTime();
|
||||
const diffInSec = Math.floor(diffInMs / 1000);
|
||||
if (diffInSec > SESSION_TIMEOUT) {
|
||||
_sessionId = newSessionId();
|
||||
}
|
||||
_lastTouched = now;
|
||||
|
||||
const body = JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: _sessionId,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue