add session back

This commit is contained in:
goenning 2023-09-01 17:45:59 +01:00
parent 61eb7bd9e1
commit 14197518e8
3 changed files with 154 additions and 4 deletions

120
package-lock.json generated
View file

@ -1429,6 +1429,126 @@
"name": "@aptabase/web", "name": "@aptabase/web",
"version": "0.1.3", "version": "0.1.3",
"license": "MIT" "license": "MIT"
},
"node_modules/@next/swc-darwin-x64": {
"version": "13.4.19",
"resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.19.tgz",
"integrity": "sha512-jyzO6wwYhx6F+7gD8ddZfuqO4TtpJdw3wyOduR4fxTUCm3aLw7YmHGYNjS0xRSYGAkLpBkH1E0RcelyId6lNsw==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@next/swc-linux-arm64-gnu": {
"version": "13.4.19",
"resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.19.tgz",
"integrity": "sha512-vdlnIlaAEh6H+G6HrKZB9c2zJKnpPVKnA6LBwjwT2BTjxI7e0Hx30+FoWCgi50e+YO49p6oPOtesP9mXDRiiUg==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@next/swc-linux-arm64-musl": {
"version": "13.4.19",
"resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.19.tgz",
"integrity": "sha512-aU0HkH2XPgxqrbNRBFb3si9Ahu/CpaR5RPmN2s9GiM9qJCiBBlZtRTiEca+DC+xRPyCThTtWYgxjWHgU7ZkyvA==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@next/swc-linux-x64-gnu": {
"version": "13.4.19",
"resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.19.tgz",
"integrity": "sha512-htwOEagMa/CXNykFFeAHHvMJeqZfNQEoQvHfsA4wgg5QqGNqD5soeCer4oGlCol6NGUxknrQO6VEustcv+Md+g==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@next/swc-linux-x64-musl": {
"version": "13.4.19",
"resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.19.tgz",
"integrity": "sha512-4Gj4vvtbK1JH8ApWTT214b3GwUh9EKKQjY41hH/t+u55Knxi/0wesMzwQRhppK6Ddalhu0TEttbiJ+wRcoEj5Q==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@next/swc-win32-arm64-msvc": {
"version": "13.4.19",
"resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.19.tgz",
"integrity": "sha512-bUfDevQK4NsIAHXs3/JNgnvEY+LRyneDN788W2NYiRIIzmILjba7LaQTfihuFawZDhRtkYCv3JDC3B4TwnmRJw==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@next/swc-win32-ia32-msvc": {
"version": "13.4.19",
"resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.19.tgz",
"integrity": "sha512-Y5kikILFAr81LYIFaw6j/NrOtmiM4Sf3GtOc0pn50ez2GCkr+oejYuKGcwAwq3jiTKuzF6OF4iT2INPoxRycEA==",
"cpu": [
"ia32"
],
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@next/swc-win32-x64-msvc": {
"version": "13.4.19",
"resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.19.tgz",
"integrity": "sha512-YzA78jBDXMYiINdPdJJwGgPNT3YqBNNGhsthsDoWHL9p24tEJn9ViQf/ZqTbwSpX/RrkPupLfuuTH2sf73JBAw==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">= 10"
}
} }
} }
} }

View file

@ -1,3 +1,5 @@
import { newSessionId } from './session';
export type AptabaseOptions = { export type AptabaseOptions = {
host?: string; host?: string;
appVersion?: string; appVersion?: string;
@ -6,6 +8,10 @@ export type AptabaseOptions = {
const locale = getBrowserLocale(); const locale = getBrowserLocale();
const isDebug = getIsDebug(); const isDebug = getIsDebug();
// Session expires after 1 hour of inactivity
const SESSION_TIMEOUT = 1 * 60 * 60;
let _sessionId = newSessionId();
let _lastTouched = new Date();
let _appKey = ''; let _appKey = '';
let _apiUrl = ''; let _apiUrl = '';
let _options: AptabaseOptions | undefined; let _options: AptabaseOptions | undefined;
@ -35,12 +41,18 @@ export async function trackEvent(eventName: string, props?: Record<string, strin
if (!_appKey) return; if (!_appKey) return;
if (typeof window === 'undefined' || !window.fetch) { if (typeof window === 'undefined' || !window.fetch) {
console.warn( console.warn(`Aptabase: trackEvent requires a browser environment. Event "${eventName}" will not be tracked.`);
`Aptabase: this call to "trackEvent" requires a browser environment. Did you import from the wrong package?`,
);
return; 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;
try { try {
const response = await window.fetch(_apiUrl, { const response = await window.fetch(_apiUrl, {
method: 'POST', method: 'POST',
@ -51,7 +63,7 @@ export async function trackEvent(eventName: string, props?: Record<string, strin
credentials: 'omit', credentials: 'omit',
body: JSON.stringify({ body: JSON.stringify({
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
sessionId: 'CHANGE-THIS', sessionId: _sessionId,
eventName: eventName, eventName: eventName,
systemProps: { systemProps: {
isDebug, isDebug,

View file

@ -0,0 +1,18 @@
export function newSessionId() {
if (typeof crypto !== 'undefined' && crypto && crypto.randomUUID) {
return crypto.randomUUID();
}
return [randomStr(8), randomStr(4), randomStr(4), randomStr(4), randomStr(12)].join('-');
}
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
function randomStr(len: number) {
let result = '';
for (let i = 0; i < len; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}