shared-library/analytics/sdk.ts

61 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-07-13 11:05:20 +00:00
import type { TrackRequestParams } from "./types";
2024-07-13 10:47:43 +00:00
type AnalyticsBody = Record<string, unknown> & {
type: "event" | "request";
timestamp: number;
};
export class AnalyticsSDK {
constructor(private readonly apiURL: string) {}
trackRequest(request: Request, { clientAddress, locale, responseStatus }: TrackRequestParams) {
2024-07-13 10:47:43 +00:00
const userAgent = request.headers.get("User-Agent");
const acceptLanguage = request.headers.get("Accept-Language");
const { method, url: stringUrl, referrer } = request;
const url = new URL(stringUrl);
this.track({
type: "request",
timestamp: Date.now(),
payload: {
user: {
address: clientAddress,
attributes: {
locale,
},
},
request: {
method,
pathname: url.pathname,
referrer,
...(acceptLanguage ? { acceptLanguage } : {}),
...(userAgent ? { userAgent } : {}),
},
response: {
status: responseStatus,
},
},
});
}
trackEvent(eventName: string) {
this.track({
type: "event",
timestamp: Date.now(),
eventName,
});
}
private async track(body: AnalyticsBody) {
try {
await fetch(this.apiURL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
} catch (e) {
console.warn("Couldn't send analytics", e);
}
}
}