Added analytics sdk

This commit is contained in:
DrMint 2024-07-13 12:47:43 +02:00
parent c52b9baa91
commit 8550485f1d
2 changed files with 68 additions and 0 deletions

63
analytics/sdk.ts Normal file
View File

@ -0,0 +1,63 @@
import { TrackRequestParams } from "./types";
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
) {
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);
}
}
}

5
analytics/types.ts Normal file
View File

@ -0,0 +1,5 @@
export type TrackRequestParams = {
clientAddress: string;
locale: string;
responseStatus: number;
};