add session back
This commit is contained in:
parent
61eb7bd9e1
commit
14197518e8
3 changed files with 154 additions and 4 deletions
|
@ -1,3 +1,5 @@
|
|||
import { newSessionId } from './session';
|
||||
|
||||
export type AptabaseOptions = {
|
||||
host?: string;
|
||||
appVersion?: string;
|
||||
|
@ -6,6 +8,10 @@ export type AptabaseOptions = {
|
|||
const locale = getBrowserLocale();
|
||||
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 _apiUrl = '';
|
||||
let _options: AptabaseOptions | undefined;
|
||||
|
@ -35,12 +41,18 @@ export async function trackEvent(eventName: string, props?: Record<string, strin
|
|||
if (!_appKey) return;
|
||||
|
||||
if (typeof window === 'undefined' || !window.fetch) {
|
||||
console.warn(
|
||||
`Aptabase: this call to "trackEvent" requires a browser environment. Did you import from the wrong package?`,
|
||||
);
|
||||
console.warn(`Aptabase: trackEvent requires a browser environment. Event "${eventName}" will not be tracked.`);
|
||||
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 {
|
||||
const response = await window.fetch(_apiUrl, {
|
||||
method: 'POST',
|
||||
|
@ -51,7 +63,7 @@ export async function trackEvent(eventName: string, props?: Record<string, strin
|
|||
credentials: 'omit',
|
||||
body: JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: 'CHANGE-THIS',
|
||||
sessionId: _sessionId,
|
||||
eventName: eventName,
|
||||
systemProps: {
|
||||
isDebug,
|
||||
|
|
18
packages/web/src/session.ts
Normal file
18
packages/web/src/session.ts
Normal 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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue