general fixes
This commit is contained in:
parent
c407842754
commit
f6de1cd18b
4 changed files with 33 additions and 44 deletions
|
@ -22,8 +22,8 @@ export async function trackEvent(
|
||||||
|
|
||||||
function getHeaders(req?: NextIncomingMessage): Headers | undefined {
|
function getHeaders(req?: NextIncomingMessage): Headers | undefined {
|
||||||
if (req) {
|
if (req) {
|
||||||
// we only need the user-agent header
|
|
||||||
return new Headers({
|
return new Headers({
|
||||||
|
'accept-language': req.headers['accept-language'] ?? '',
|
||||||
'user-agent': req.headers['user-agent'] ?? '',
|
'user-agent': req.headers['user-agent'] ?? '',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ import { AptabaseOptions } from './types';
|
||||||
|
|
||||||
export { AptabaseOptions };
|
export { AptabaseOptions };
|
||||||
|
|
||||||
|
const isDebug = process.env.NODE_ENV === 'development';
|
||||||
|
|
||||||
export function init(appKey: string, options?: AptabaseOptions) {
|
export function init(appKey: string, options?: AptabaseOptions) {
|
||||||
globalThis.__APTABASE__ = { appKey, options };
|
globalThis.__APTABASE__ = { appKey, options };
|
||||||
}
|
}
|
||||||
|
@ -15,18 +17,15 @@ export async function trackEvent(
|
||||||
props?: Record<string, string | number | boolean>,
|
props?: Record<string, string | number | boolean>,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { appKey } = globalThis.__APTABASE__ || {};
|
const { appKey } = globalThis.__APTABASE__ || {};
|
||||||
|
|
||||||
if (!appKey) return Promise.resolve();
|
if (!appKey) return Promise.resolve();
|
||||||
|
|
||||||
const userAgent = req.headers.get('user-agent') ?? '';
|
|
||||||
|
|
||||||
const body = JSON.stringify({
|
const body = JSON.stringify({
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
sessionId: 'CHANGE-THIS',
|
sessionId: 'CHANGE-THIS',
|
||||||
eventName: eventName,
|
eventName: eventName,
|
||||||
systemProps: {
|
systemProps: {
|
||||||
isDebug: true,
|
isDebug,
|
||||||
locale: 'en',
|
locale: extractLocale(req.headers.get('accept-language')),
|
||||||
appVersion: '',
|
appVersion: '',
|
||||||
sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-node@${process.env.PKG_VERSION}`,
|
sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-node@${process.env.PKG_VERSION}`,
|
||||||
},
|
},
|
||||||
|
@ -37,7 +36,7 @@ export async function trackEvent(
|
||||||
const response = await fetch('http://localhost:3000/api/v0/event', {
|
const response = await fetch('http://localhost:3000/api/v0/event', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': userAgent,
|
'User-Agent': req.headers.get('user-agent') ?? '',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'App-Key': appKey,
|
'App-Key': appKey,
|
||||||
},
|
},
|
||||||
|
@ -53,3 +52,17 @@ export async function trackEvent(
|
||||||
console.warn(`Failed to send event "${eventName}": ${e}`);
|
console.warn(`Failed to send event "${eventName}": ${e}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extractLocale(locale: string | null): string | null {
|
||||||
|
if (!locale) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const languageLocales = locale.split(',');
|
||||||
|
const firstLanguageLocale = languageLocales[0];
|
||||||
|
if (!firstLanguageLocale) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const [language] = firstLanguageLocale.split(';');
|
||||||
|
return language || null;
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
export type AptabaseOptions = {
|
export type AptabaseOptions = {
|
||||||
host?: string;
|
host?: string;
|
||||||
appVersion?: string;
|
appVersion?: string;
|
||||||
__sdkVersion?: string;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const locale = navigator?.languages && navigator?.languages.length ? navigator?.languages[0] : navigator?.language;
|
||||||
|
const isDebug = location?.hostname === 'localhost';
|
||||||
|
|
||||||
let _appKey = '';
|
let _appKey = '';
|
||||||
let _apiUrl = '';
|
let _apiUrl = '';
|
||||||
let _locale = '';
|
|
||||||
let _isDebug = false;
|
|
||||||
let _options: AptabaseOptions | undefined;
|
let _options: AptabaseOptions | undefined;
|
||||||
|
|
||||||
const _hosts: { [region: string]: string } = {
|
const _hosts: { [region: string]: string } = {
|
||||||
|
@ -41,26 +41,25 @@ export function init(appKey: string, options?: AptabaseOptions) {
|
||||||
|
|
||||||
const baseUrl = getBaseUrl(parts[1], options);
|
const baseUrl = getBaseUrl(parts[1], options);
|
||||||
_apiUrl = `${baseUrl}/api/v0/event`;
|
_apiUrl = `${baseUrl}/api/v0/event`;
|
||||||
|
|
||||||
if (typeof location !== 'undefined') {
|
|
||||||
_isDebug = location.hostname === 'localhost';
|
|
||||||
}
|
|
||||||
|
|
||||||
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>) {
|
export function trackEvent(eventName: string, props?: Record<string, string | number | boolean>) {
|
||||||
if (!_appKey || typeof window === 'undefined' || !window.fetch) return;
|
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?`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const body = JSON.stringify({
|
const body = JSON.stringify({
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
sessionId: 'CHANGE-THIS',
|
sessionId: 'CHANGE-THIS',
|
||||||
eventName: eventName,
|
eventName: eventName,
|
||||||
systemProps: {
|
systemProps: {
|
||||||
isDebug: _isDebug,
|
isDebug,
|
||||||
locale: _locale,
|
locale,
|
||||||
appVersion: _options?.appVersion ?? '',
|
appVersion: _options?.appVersion ?? '',
|
||||||
sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-web@${process.env.PKG_VERSION}`,
|
sdkVersion: globalThis.__APTABASE_SDK_VERSION__ ?? `aptabase-web@${process.env.PKG_VERSION}`,
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
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