remove hardcoded data
This commit is contained in:
parent
4b1aae55c6
commit
0f28be3d2b
3 changed files with 80 additions and 54 deletions
3
packages/node/src/global.d.ts
vendored
3
packages/node/src/global.d.ts
vendored
|
@ -1,5 +1,6 @@
|
|||
type AptabaseState = {
|
||||
appKey?: string;
|
||||
appKey: string;
|
||||
apiUrl: string;
|
||||
options?: import('./types').AptabaseOptions;
|
||||
};
|
||||
|
||||
|
|
|
@ -4,8 +4,23 @@ export { AptabaseOptions };
|
|||
|
||||
const isDebug = process.env.NODE_ENV === 'development';
|
||||
|
||||
const _hosts: { [region: string]: string } = {
|
||||
US: 'https://us.aptabase.com',
|
||||
EU: 'https://eu.aptabase.com',
|
||||
DEV: 'http://localhost:3000',
|
||||
SH: '',
|
||||
};
|
||||
|
||||
export function init(appKey: string, options?: AptabaseOptions) {
|
||||
globalThis.__APTABASE__ = { appKey, options };
|
||||
const parts = appKey.split('-');
|
||||
if (parts.length !== 3 || _hosts[parts[1]] === undefined) {
|
||||
console.warn(`Aptabase: The App Key "${appKey}" is invalid. Tracking will be disabled.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const baseUrl = getBaseUrl(parts[1], options);
|
||||
const apiUrl = `${baseUrl}/api/v0/event`;
|
||||
globalThis.__APTABASE__ = { appKey, apiUrl, options };
|
||||
}
|
||||
|
||||
// We only need the headers from the request object
|
||||
|
@ -16,24 +31,11 @@ export async function trackEvent(
|
|||
eventName: string,
|
||||
props?: Record<string, string | number | boolean>,
|
||||
): Promise<void> {
|
||||
const { appKey } = globalThis.__APTABASE__ || {};
|
||||
const { appKey, apiUrl, options } = globalThis.__APTABASE__ || {};
|
||||
if (!appKey) return Promise.resolve();
|
||||
|
||||
const body = JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: 'CHANGE-THIS',
|
||||
eventName: eventName,
|
||||
systemProps: {
|
||||
isDebug,
|
||||
locale: extractLocale(req.headers.get('accept-language')),
|
||||
appVersion: '',
|
||||
sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-node@${process.env.PKG_VERSION}`,
|
||||
},
|
||||
props: props,
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch('http://localhost:3000/api/v0/event', {
|
||||
const response = await fetch(apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'User-Agent': req.headers.get('user-agent') ?? '',
|
||||
|
@ -41,7 +43,18 @@ export async function trackEvent(
|
|||
'App-Key': appKey,
|
||||
},
|
||||
credentials: 'omit',
|
||||
body,
|
||||
body: JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: 'CHANGE-THIS',
|
||||
eventName: eventName,
|
||||
systemProps: {
|
||||
isDebug,
|
||||
locale: extractLocale(req.headers.get('accept-language')),
|
||||
appVersion: options,
|
||||
sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-node@${process.env.PKG_VERSION}`,
|
||||
},
|
||||
props: props,
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.status >= 300) {
|
||||
|
@ -53,6 +66,18 @@ export async function trackEvent(
|
|||
}
|
||||
}
|
||||
|
||||
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.`);
|
||||
return;
|
||||
}
|
||||
return options.host;
|
||||
}
|
||||
|
||||
return _hosts[region];
|
||||
}
|
||||
|
||||
function extractLocale(locale: string | null): string | null {
|
||||
if (!locale) {
|
||||
return null;
|
||||
|
|
|
@ -17,18 +17,6 @@ const _hosts: { [region: string]: string } = {
|
|||
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.`);
|
||||
return;
|
||||
}
|
||||
return options.host;
|
||||
}
|
||||
|
||||
return _hosts[region];
|
||||
}
|
||||
|
||||
export function init(appKey: string, options?: AptabaseOptions) {
|
||||
_appKey = appKey;
|
||||
_options = options;
|
||||
|
@ -43,7 +31,7 @@ export function init(appKey: string, options?: AptabaseOptions) {
|
|||
_apiUrl = `${baseUrl}/api/v0/event`;
|
||||
}
|
||||
|
||||
export function trackEvent(eventName: string, props?: Record<string, string | number | boolean>) {
|
||||
export async function trackEvent(eventName: string, props?: Record<string, string | number | boolean>): Promise<void> {
|
||||
if (!_appKey) return;
|
||||
|
||||
if (typeof window === 'undefined' || !window.fetch) {
|
||||
|
@ -53,35 +41,47 @@ export function trackEvent(eventName: string, props?: Record<string, string | nu
|
|||
return;
|
||||
}
|
||||
|
||||
const body = JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: 'CHANGE-THIS',
|
||||
eventName: eventName,
|
||||
systemProps: {
|
||||
isDebug,
|
||||
locale,
|
||||
appVersion: _options?.appVersion ?? '',
|
||||
sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-web@${process.env.PKG_VERSION}`,
|
||||
},
|
||||
props: props,
|
||||
});
|
||||
|
||||
window
|
||||
.fetch(_apiUrl, {
|
||||
try {
|
||||
const response = await window.fetch(_apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Key': _appKey,
|
||||
},
|
||||
credentials: 'omit',
|
||||
body,
|
||||
})
|
||||
.then((response) => {
|
||||
if (response.status >= 300) {
|
||||
console.warn(`Failed to send event "${eventName}": ${response.status} ${response.statusText}`);
|
||||
}
|
||||
})
|
||||
.catch(console.error);
|
||||
body: JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: 'CHANGE-THIS',
|
||||
eventName: eventName,
|
||||
systemProps: {
|
||||
isDebug,
|
||||
locale,
|
||||
appVersion: _options?.appVersion ?? '',
|
||||
sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-web@${process.env.PKG_VERSION}`,
|
||||
},
|
||||
props: props,
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.status >= 300) {
|
||||
const responseBody = await response.text();
|
||||
console.warn(`Failed to send event "${eventName}": ${response.status} ${responseBody}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`Failed to send event "${eventName}": ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
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.`);
|
||||
return;
|
||||
}
|
||||
return options.host;
|
||||
}
|
||||
|
||||
return _hosts[region];
|
||||
}
|
||||
|
||||
function getBrowserLocale(): string | null {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue