-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage-analytics.ts
More file actions
61 lines (52 loc) · 1.56 KB
/
Copy pathusage-analytics.ts
File metadata and controls
61 lines (52 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const SDK_VERSION = '0.1.0';
const DEFAULT_ENDPOINT = 'https://analytics.iln.finance/event';
// Fields that must never appear in an analytics payload
const PII_FIELDS = [
'address',
'freelancer',
'payer',
'funder',
'secretKey',
'publicKey',
'amount',
'invoiceId',
];
export interface UsageEvent {
method: string;
success: boolean;
errorCode?: string;
network: string;
version: string;
}
function isEnabled(): boolean {
if (typeof process === 'undefined') return false;
return process.env['ILN_ANALYTICS'] === '1';
}
function endpoint(): string {
if (typeof process !== 'undefined' && process.env['ILN_ANALYTICS_ENDPOINT']) {
return process.env['ILN_ANALYTICS_ENDPOINT'];
}
return DEFAULT_ENDPOINT;
}
function hasPiiFields(payload: Record<string, unknown>): boolean {
return PII_FIELDS.some((f) => f in payload);
}
export function track(method: string, network: string, success: boolean, errorCode?: string): void {
if (!isEnabled()) return;
const payload: UsageEvent = { method, success, network, version: SDK_VERSION };
if (errorCode) payload.errorCode = errorCode;
// Safety guard: never send if PII fields were somehow included
if (hasPiiFields(payload as unknown as Record<string, unknown>)) return;
const url = endpoint();
try {
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
}).catch(() => {
// fire-and-forget: ignore network errors silently
});
} catch {
// fetch itself threw (e.g. not available in env) — ignore
}
}