-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalytics.js
More file actions
147 lines (127 loc) · 4.27 KB
/
Copy pathanalytics.js
File metadata and controls
147 lines (127 loc) · 4.27 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// --- GA Proxy config ---
const PROXY_BASE_URL = "https://asia-south1-extensions-analytics-prod.cloudfunctions.net/extensions-ga-proxy";
export const TOKEN_STORAGE_KEY = "forcepaster_install_token";
export const CLIENT_ID_STORAGE_KEY = "forcepaster_ga_client_id";
const SESSION_STORAGE_KEY = "forcepaster_ga_session";
const SESSION_EXPIRATION_MIN = 30;
const DEFAULT_ENGAGEMENT_TIME_MS = 100;
const VALID_CLIENT_ID_RE = /^\d+\.\d+$/;
function randomUint32() {
const arr = new Uint32Array(1);
crypto.getRandomValues(arr);
return (arr[0] >>> 0) || 1;
}
async function getOrCreateClientId() {
const result = await chrome.storage.local.get(CLIENT_ID_STORAGE_KEY);
let clientId = result[CLIENT_ID_STORAGE_KEY];
if (!clientId || !VALID_CLIENT_ID_RE.test(clientId)) {
const unixSeconds = Math.floor(Date.now() / 1000);
clientId = `${randomUint32()}.${unixSeconds}`;
await chrome.storage.local.set({ [CLIENT_ID_STORAGE_KEY]: clientId });
}
return clientId;
}
async function getOrCreateSessionId() {
const now = Date.now();
const result = await chrome.storage.session.get(SESSION_STORAGE_KEY);
let session = result[SESSION_STORAGE_KEY];
if (session?.timestamp) {
const durationMin = (now - Number(session.timestamp)) / 60000;
if (durationMin <= SESSION_EXPIRATION_MIN) {
session.timestamp = String(now);
await chrome.storage.session.set({ [SESSION_STORAGE_KEY]: session });
return session.session_id;
}
}
session = { session_id: String(now), timestamp: String(now) };
await chrome.storage.session.set({ [SESSION_STORAGE_KEY]: session });
return session.session_id;
}
async function registerInstallToken() {
const res = await fetch(`${PROXY_BASE_URL}/v1/register`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Extension-App": "forcepaster",
"X-Extension-Id": chrome.runtime.id
},
body: JSON.stringify({})
});
if (!res.ok) {
const txt = await res.text();
throw new Error(`register failed: ${res.status} ${txt}`);
}
const data = await res.json();
if (!data?.token) throw new Error("register failed: missing token");
await chrome.storage.local.set({ [TOKEN_STORAGE_KEY]: data.token });
return data.token;
}
async function getOrRegisterToken(force = false) {
if (!force) {
const result = await chrome.storage.local.get(TOKEN_STORAGE_KEY);
const token = result[TOKEN_STORAGE_KEY];
if (token) return token;
}
return registerInstallToken();
}
export async function sendProxyEvent(eventName, params = {}, opts = {}) {
const client_id = await getOrCreateClientId();
const session_id = await getOrCreateSessionId();
const token = await getOrRegisterToken(false);
const debug = opts.debug ?? false;
const payload = {
client_id,
events: [
{
name: eventName,
params: {
session_id,
engagement_time_msec: DEFAULT_ENGAGEMENT_TIME_MS,
ext_version: chrome.runtime.getManifest().version,
...params,
}
}
]
};
if (opts.userProperties) {
payload.user_properties = toUserProperties(opts.userProperties);
}
const url = debug
? `${PROXY_BASE_URL}/v1/collect?debug_view=1`
: `${PROXY_BASE_URL}/v1/collect`;
async function doSend(bearerToken) {
return fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${bearerToken}`,
"X-Extension-App": "forcepaster",
"X-Extension-Id": chrome.runtime.id
},
body: JSON.stringify(payload)
});
}
let res = await doSend(token);
if (res.status === 401) {
// Token missing/invalid. Re-register once and retry once.
const newToken = await getOrRegisterToken(true);
res = await doSend(newToken);
}
if (!res.ok && !debug) {
const txt = await res.text();
throw new Error(`collect failed: ${res.status} ${txt}`);
}
return res;
}
const GA4_PROPERTY_NAME_LIMIT = 24;
const GA4_PROPERTY_VALUE_LIMIT = 36;
function toUserProperties(obj = {}) {
const out = {};
for (const [k, v] of Object.entries(obj)) {
if (v === undefined || v === null) continue;
const key = String(k).slice(0, GA4_PROPERTY_NAME_LIMIT);
const val = String(v).slice(0, GA4_PROPERTY_VALUE_LIMIT);
out[key] = { value: val };
}
return out;
}