reuse a bunch of stuff

This commit is contained in:
goenning 2023-09-01 13:33:07 +01:00
parent c0a2387f76
commit e256d13c92
14 changed files with 1593 additions and 138 deletions

View file

@ -1,39 +1,28 @@
import { newSessionId } from "./session";
// env.PKG_VERSION is replaced by rollup during build phase
const sdkVersion = "aptabase-web@env.PKG_VERSION";
const sdkVersion = 'aptabase-web@env.PKG_VERSION';
export type AptabaseOptions = {
host?: string;
appVersion?: string;
};
// 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 _appKey = '';
let _apiUrl = '';
let _locale = '';
let _isDebug = false;
let _options: AptabaseOptions | undefined;
const _hosts: { [region: string]: string } = {
US: "https://us.aptabase.com",
EU: "https://eu.aptabase.com",
DEV: "http://localhost:3000",
SH: "",
US: 'https://us.aptabase.com',
EU: 'https://eu.aptabase.com',
DEV: 'http://localhost:3000',
SH: '',
};
function getBaseUrl(
region: string,
options?: AptabaseOptions
): string | undefined {
if (region === "SH") {
function getBaseUrl(region: string, options?: AptabaseOptions): string | undefined {
if (region === 'SH') {
if (!options?.host) {
console.warn(
`Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.`
);
console.warn(`Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.`);
return;
}
return options.host;
@ -46,51 +35,35 @@ export function init(appKey: string, options?: AptabaseOptions) {
_appKey = appKey;
_options = options;
const parts = appKey.split("-");
const parts = appKey.split('-');
if (parts.length !== 3 || _hosts[parts[1]] === undefined) {
console.warn(
`The Aptabase App Key "${appKey}" is invalid. Tracking will be disabled.`
);
console.warn(`The Aptabase App Key "${appKey}" is invalid. Tracking will be disabled.`);
return;
}
const baseUrl = getBaseUrl(parts[1], options);
_apiUrl = `${baseUrl}/api/v0/event`;
if (typeof location !== "undefined") {
_isDebug = location.hostname === "localhost";
if (typeof location !== 'undefined') {
_isDebug = location.hostname === 'localhost';
}
if (typeof navigator !== "undefined") {
_locale =
navigator.languages && navigator.languages.length
? navigator.languages[0]
: navigator.language;
if (typeof navigator !== 'undefined') {
_locale = navigator.languages && navigator.languages.length ? navigator.languages[0] : navigator.language;
}
}
export function trackEvent(
eventName: string,
props?: Record<string, string | number | boolean>
) {
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;
export function trackEvent(eventName: string, props?: Record<string, string | number | boolean>) {
if (!_appKey || typeof window === 'undefined' || !window.fetch) return;
const body = JSON.stringify({
timestamp: new Date().toISOString(),
sessionId: _sessionId,
sessionId: 'CHANGE-THIS',
eventName: eventName,
systemProps: {
isDebug: _isDebug,
locale: _locale,
appVersion: _options?.appVersion ?? "",
appVersion: _options?.appVersion ?? '',
sdkVersion,
},
props: props,
@ -98,19 +71,17 @@ export function trackEvent(
window
.fetch(_apiUrl, {
method: "POST",
method: 'POST',
headers: {
"Content-Type": "application/json",
"App-Key": _appKey,
'Content-Type': 'application/json',
'App-Key': _appKey,
},
credentials: "omit",
credentials: 'omit',
body,
})
.then((response) => {
if (response.status >= 300) {
console.warn(
`Failed to send event "${eventName}": ${response.status} ${response.statusText}`
);
console.warn(`Failed to send event "${eventName}": ${response.status} ${response.statusText}`);
}
})
.catch(console.error);