@gem-sdk/clarity-js 0.8.94
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +1 -0
- package/build/clarity.extended.js +1 -0
- package/build/clarity.insight.js +1 -0
- package/build/clarity.js +7148 -0
- package/build/clarity.livechat.js +1 -0
- package/build/clarity.min.js +1 -0
- package/build/clarity.module.js +7144 -0
- package/build/clarity.performance.js +1 -0
- package/build/dynamic/clarity.crisp.js +1 -0
- package/build/dynamic/clarity.tidio.js +1 -0
- package/package.json +49 -0
- package/rollup.config.ts +161 -0
- package/src/clarity.ts +72 -0
- package/src/core/api.ts +8 -0
- package/src/core/config.ts +35 -0
- package/src/core/copy.ts +3 -0
- package/src/core/dynamic.ts +68 -0
- package/src/core/event.ts +53 -0
- package/src/core/hash.ts +19 -0
- package/src/core/history.ts +82 -0
- package/src/core/index.ts +81 -0
- package/src/core/measure.ts +19 -0
- package/src/core/report.ts +28 -0
- package/src/core/scrub.ts +201 -0
- package/src/core/task.ts +181 -0
- package/src/core/throttle.ts +46 -0
- package/src/core/time.ts +26 -0
- package/src/core/timeout.ts +10 -0
- package/src/core/version.ts +2 -0
- package/src/custom/README.md +44 -0
- package/src/custom/dialog.ts +69 -0
- package/src/custom/index.ts +6 -0
- package/src/data/baseline.ts +162 -0
- package/src/data/compress.ts +31 -0
- package/src/data/consent.ts +84 -0
- package/src/data/cookie.ts +90 -0
- package/src/data/custom.ts +23 -0
- package/src/data/dimension.ts +53 -0
- package/src/data/encode.ts +155 -0
- package/src/data/envelope.ts +53 -0
- package/src/data/extract.ts +217 -0
- package/src/data/gemx-upload.ts +51 -0
- package/src/data/gemx.ts +35 -0
- package/src/data/index.ts +57 -0
- package/src/data/limit.ts +44 -0
- package/src/data/metadata.ts +340 -0
- package/src/data/metric.ts +51 -0
- package/src/data/ping.ts +36 -0
- package/src/data/signal.ts +30 -0
- package/src/data/summary.ts +34 -0
- package/src/data/token.ts +39 -0
- package/src/data/upgrade.ts +44 -0
- package/src/data/upload.ts +375 -0
- package/src/data/util.ts +18 -0
- package/src/data/variable.ts +84 -0
- package/src/diagnostic/encode.ts +40 -0
- package/src/diagnostic/fraud.ts +37 -0
- package/src/diagnostic/index.ts +13 -0
- package/src/diagnostic/internal.ts +28 -0
- package/src/diagnostic/script.ts +35 -0
- package/src/dynamic/agent/blank.ts +2 -0
- package/src/dynamic/agent/crisp.ts +40 -0
- package/src/dynamic/agent/encode.ts +25 -0
- package/src/dynamic/agent/index.ts +8 -0
- package/src/dynamic/agent/livechat.ts +58 -0
- package/src/dynamic/agent/tidio.ts +44 -0
- package/src/global.ts +6 -0
- package/src/index.ts +10 -0
- package/src/insight/blank.ts +14 -0
- package/src/insight/encode.ts +64 -0
- package/src/insight/snapshot.ts +115 -0
- package/src/interaction/change.ts +38 -0
- package/src/interaction/click.ts +256 -0
- package/src/interaction/clipboard.ts +32 -0
- package/src/interaction/encode.ts +217 -0
- package/src/interaction/focus.ts +25 -0
- package/src/interaction/index.ts +66 -0
- package/src/interaction/input.ts +66 -0
- package/src/interaction/pageshow.ts +35 -0
- package/src/interaction/pointer.ts +137 -0
- package/src/interaction/resize.ts +50 -0
- package/src/interaction/scroll.ts +134 -0
- package/src/interaction/selection.ts +66 -0
- package/src/interaction/submit.ts +30 -0
- package/src/interaction/timeline.ts +69 -0
- package/src/interaction/unload.ts +32 -0
- package/src/interaction/visibility.ts +28 -0
- package/src/layout/animation.ts +133 -0
- package/src/layout/constants.ts +13 -0
- package/src/layout/custom.ts +48 -0
- package/src/layout/discover.ts +31 -0
- package/src/layout/document.ts +46 -0
- package/src/layout/dom.ts +444 -0
- package/src/layout/encode.ts +157 -0
- package/src/layout/gemx-snapshot.ts +84 -0
- package/src/layout/index.ts +47 -0
- package/src/layout/mutation.ts +424 -0
- package/src/layout/node.ts +302 -0
- package/src/layout/offset.ts +19 -0
- package/src/layout/region.ts +151 -0
- package/src/layout/schema.ts +63 -0
- package/src/layout/selector.ts +113 -0
- package/src/layout/style.ts +160 -0
- package/src/layout/target.ts +34 -0
- package/src/layout/traverse.ts +28 -0
- package/src/performance/blank.ts +10 -0
- package/src/performance/encode.ts +31 -0
- package/src/performance/index.ts +12 -0
- package/src/performance/interaction.ts +125 -0
- package/src/performance/navigation.ts +31 -0
- package/src/performance/observer.ts +112 -0
- package/src/queue.ts +33 -0
- package/test/consentv2.test.ts +1309 -0
- package/test/hash.test.ts +68 -0
- package/test/time.test.ts +42 -0
- package/tsconfig.json +12 -0
- package/tslint.json +33 -0
- package/types/agent.d.ts +39 -0
- package/types/core.d.ts +156 -0
- package/types/data.d.ts +611 -0
- package/types/diagnostic.d.ts +24 -0
- package/types/global.d.ts +43 -0
- package/types/index.d.ts +46 -0
- package/types/interaction.d.ts +187 -0
- package/types/layout.d.ts +271 -0
- package/types/performance.d.ts +34 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { Constant as CoreConstant, Time } from "@clarity-types/core";
|
|
2
|
+
import { BooleanFlag, ConsentData, ConsentSource, ConsentState, Constant, Dimension, Metadata, MetadataCallback, MetadataCallbackOptions, Metric, Session, Setting, User } from "@clarity-types/data";
|
|
3
|
+
import * as clarity from "@src/clarity";
|
|
4
|
+
import * as core from "@src/core";
|
|
5
|
+
import config from "@src/core/config";
|
|
6
|
+
import hash from "@src/core/hash";
|
|
7
|
+
import * as scrub from "@src/core/scrub";
|
|
8
|
+
import * as trackConsent from "@src/data/consent";
|
|
9
|
+
import { COOKIE_SEP, getCookie, setCookie } from "@src/data/cookie";
|
|
10
|
+
import * as dimension from "@src/data/dimension";
|
|
11
|
+
import * as metric from "@src/data/metric";
|
|
12
|
+
import { supported } from "@src/data/util";
|
|
13
|
+
import { set } from "@src/data/variable";
|
|
14
|
+
|
|
15
|
+
export let data: Metadata = null;
|
|
16
|
+
export let callbacks: MetadataCallbackOptions[] = [];
|
|
17
|
+
export let electron = BooleanFlag.False;
|
|
18
|
+
let consentStatus: ConsentState = null;
|
|
19
|
+
let cookiesLogged = false;
|
|
20
|
+
let defaultStatus: ConsentState = { source: ConsentSource.Default, ad_Storage: Constant.Denied, analytics_Storage: Constant.Denied };
|
|
21
|
+
|
|
22
|
+
export function start(): void {
|
|
23
|
+
const ua = navigator && "userAgent" in navigator ? navigator.userAgent : Constant.Empty;
|
|
24
|
+
const timezone = (typeof Intl !== 'undefined' && Intl?.DateTimeFormat()?.resolvedOptions()?.timeZone) ?? '';
|
|
25
|
+
const timezoneOffset = new Date().getTimezoneOffset().toString();
|
|
26
|
+
const ancestorOrigins = window.location.ancestorOrigins ? Array.from(window.location.ancestorOrigins).toString() : '';
|
|
27
|
+
const title = document && document.title ? document.title : Constant.Empty;
|
|
28
|
+
electron = ua.indexOf(Constant.Electron) > 0 ? BooleanFlag.True : BooleanFlag.False;
|
|
29
|
+
|
|
30
|
+
// Populate ids for this page
|
|
31
|
+
let s = session();
|
|
32
|
+
let u = user();
|
|
33
|
+
let projectId = config.projectId || hash(location.host);
|
|
34
|
+
let userId = config.externalSession && config.userId ? config.userId : u.id;
|
|
35
|
+
let sessionId = config.externalSession && config.sessionId ? config.sessionId : s.session;
|
|
36
|
+
data = { projectId, userId, sessionId, pageNum: s.count };
|
|
37
|
+
|
|
38
|
+
// Override configuration based on what's in the session storage, unless it is blank (e.g. using upload callback, like in devtools)
|
|
39
|
+
config.lean = config.track && s.upgrade !== null ? s.upgrade === BooleanFlag.False : config.lean;
|
|
40
|
+
config.upload = config.track && typeof config.upload === Constant.String && s.upload && s.upload.length > Constant.HTTPS.length ? s.upload : config.upload;
|
|
41
|
+
|
|
42
|
+
// Log page metadata as dimensions
|
|
43
|
+
dimension.log(Dimension.UserAgent, ua);
|
|
44
|
+
dimension.log(Dimension.PageTitle, title);
|
|
45
|
+
dimension.log(Dimension.Url, scrub.url(location.href, !!electron));
|
|
46
|
+
dimension.log(Dimension.Referrer, document.referrer);
|
|
47
|
+
dimension.log(Dimension.TabId, tab());
|
|
48
|
+
dimension.log(Dimension.PageLanguage, document.documentElement.lang);
|
|
49
|
+
dimension.log(Dimension.DocumentDirection, document.dir);
|
|
50
|
+
dimension.log(Dimension.DevicePixelRatio, `${window.devicePixelRatio}`);
|
|
51
|
+
dimension.log(Dimension.Dob, u.dob.toString());
|
|
52
|
+
dimension.log(Dimension.CookieVersion, u.version.toString());
|
|
53
|
+
dimension.log(Dimension.AncestorOrigins, ancestorOrigins);
|
|
54
|
+
dimension.log(Dimension.Timezone, timezone);
|
|
55
|
+
dimension.log(Dimension.TimezoneOffset, timezoneOffset);
|
|
56
|
+
|
|
57
|
+
// Capture additional metadata as metrics
|
|
58
|
+
metric.max(Metric.ClientTimestamp, s.ts);
|
|
59
|
+
metric.max(Metric.Playback, BooleanFlag.False);
|
|
60
|
+
metric.max(Metric.Electron, electron);
|
|
61
|
+
|
|
62
|
+
const zone = (window as any)?.[CoreConstant.Zone];
|
|
63
|
+
const isZone = zone && CoreConstant.Symbol in zone;
|
|
64
|
+
|
|
65
|
+
if (isZone) {
|
|
66
|
+
metric.max(Metric.AngularZone, BooleanFlag.True);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Capture navigator specific dimensions
|
|
70
|
+
if (navigator) {
|
|
71
|
+
dimension.log(Dimension.Language, navigator.language);
|
|
72
|
+
metric.max(Metric.HardwareConcurrency, navigator.hardwareConcurrency);
|
|
73
|
+
metric.max(Metric.MaxTouchPoints, navigator.maxTouchPoints);
|
|
74
|
+
metric.max(Metric.DeviceMemory, Math.round((<any>navigator).deviceMemory));
|
|
75
|
+
userAgentData();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (screen) {
|
|
79
|
+
metric.max(Metric.ScreenWidth, Math.round(screen.width));
|
|
80
|
+
metric.max(Metric.ScreenHeight, Math.round(screen.height));
|
|
81
|
+
metric.max(Metric.ColorDepth, Math.round(screen.colorDepth));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Track consent config
|
|
85
|
+
// If consent status is not already set, initialize it based on project configuration. Otherwise, use the existing consent status.
|
|
86
|
+
if (consentStatus === null) {
|
|
87
|
+
consentStatus = {
|
|
88
|
+
source: u.consent ? ConsentSource.Cookie : ConsentSource.Implicit,
|
|
89
|
+
ad_Storage: config.track ? Constant.Granted : Constant.Denied,
|
|
90
|
+
analytics_Storage: config.track ? Constant.Granted : Constant.Denied,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
logCookies();
|
|
95
|
+
|
|
96
|
+
const consent = getConsentData(consentStatus);
|
|
97
|
+
trackConsent.config(consent);
|
|
98
|
+
// Track ids using a cookie if configuration allows it, skip if using external session
|
|
99
|
+
if (!config.externalSession) { track(u); }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function userAgentData(): void {
|
|
103
|
+
let uaData = navigator["userAgentData"];
|
|
104
|
+
if (uaData && uaData.getHighEntropyValues) {
|
|
105
|
+
uaData.getHighEntropyValues(["model", "platform", "platformVersion", "uaFullVersion"]).then(ua => {
|
|
106
|
+
dimension.log(Dimension.Platform, ua.platform);
|
|
107
|
+
dimension.log(Dimension.PlatformVersion, ua.platformVersion);
|
|
108
|
+
ua.brands?.forEach(brand => { dimension.log(Dimension.Brand, brand.name + Constant.Tilde + brand.version); });
|
|
109
|
+
dimension.log(Dimension.Model, ua.model);
|
|
110
|
+
metric.max(Metric.Mobile, ua.mobile ? BooleanFlag.True : BooleanFlag.False);
|
|
111
|
+
});
|
|
112
|
+
} else { dimension.log(Dimension.Platform, navigator.platform); }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function logCookies(): void {
|
|
116
|
+
// Only log cookies if both analytics_Storage and ad_Storage are granted, and we haven't already logged them
|
|
117
|
+
if (cookiesLogged || consentStatus?.analytics_Storage !== Constant.Granted || consentStatus?.ad_Storage !== Constant.Granted) { return; }
|
|
118
|
+
for (let key of config.cookies) {
|
|
119
|
+
let value = getCookie(key);
|
|
120
|
+
if (value) { set(key, value); }
|
|
121
|
+
}
|
|
122
|
+
cookiesLogged = true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function stop(): void {
|
|
126
|
+
data = null;
|
|
127
|
+
// Reset cookiesLogged so cookies are re-logged on restart. Each session generates new metadata
|
|
128
|
+
// (sessionId, pageNum), and cookie values need to be recorded in the new session's data stream.
|
|
129
|
+
cookiesLogged = false;
|
|
130
|
+
callbacks.forEach(cb => { cb.called = false; });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function metadata(cb: MetadataCallback, wait: boolean = true, recall: boolean = false, consentInfo: boolean = false): void {
|
|
134
|
+
let upgraded = config.lean ? BooleanFlag.False : BooleanFlag.True;
|
|
135
|
+
let called = false;
|
|
136
|
+
// if caller hasn't specified that they want to skip waiting for upgrade but we've already upgraded, we need to
|
|
137
|
+
// directly execute the callback in addition to adding to our list as we only process callbacks at the moment
|
|
138
|
+
// we go through the upgrading flow.
|
|
139
|
+
if (data && (upgraded || wait === false)) {
|
|
140
|
+
// Immediately invoke the callback if the caller explicitly doesn't want to wait for the upgrade confirmation
|
|
141
|
+
cb(data, !config.lean, consentInfo ? consentStatus : undefined);
|
|
142
|
+
called = true;
|
|
143
|
+
}
|
|
144
|
+
if (recall || !called) {
|
|
145
|
+
callbacks.push({ callback: cb, wait, recall, called, consentInfo });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function id(): string {
|
|
150
|
+
return data ? [data.userId, data.sessionId, data.pageNum].join(Constant.Dot) : Constant.Empty;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
//TODO: Remove this function once consentv2 is fully released
|
|
154
|
+
export function consent(status = true): void {
|
|
155
|
+
if (!status) {
|
|
156
|
+
consentv2({ source: ConsentSource.APIv1, ad_Storage: Constant.Denied, analytics_Storage: Constant.Denied });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
consentv2({ source: ConsentSource.APIv1, ad_Storage: Constant.Granted, analytics_Storage: Constant.Granted });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function consentv2(consentState: ConsentState = defaultStatus, source: number = ConsentSource.APIv2): void {
|
|
164
|
+
// Guard against calling consent API when Clarity hasn't started (e.g., due to GPC)
|
|
165
|
+
if (!core.active()) { return; }
|
|
166
|
+
|
|
167
|
+
const updatedStatus = {
|
|
168
|
+
source: consentState.source ?? source,
|
|
169
|
+
ad_Storage: normalizeConsent(consentState.ad_Storage, consentStatus?.ad_Storage),
|
|
170
|
+
analytics_Storage: normalizeConsent(consentState.analytics_Storage, consentStatus?.analytics_Storage),
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
if (
|
|
174
|
+
consentStatus &&
|
|
175
|
+
updatedStatus.ad_Storage === consentStatus.ad_Storage &&
|
|
176
|
+
updatedStatus.analytics_Storage === consentStatus.analytics_Storage
|
|
177
|
+
) {
|
|
178
|
+
consentStatus.source = updatedStatus.source;
|
|
179
|
+
trackConsent.trackConsentv2(getConsentData(consentStatus));
|
|
180
|
+
trackConsent.consent();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
consentStatus = updatedStatus;
|
|
185
|
+
callback(true);
|
|
186
|
+
const consentData = getConsentData(consentStatus);
|
|
187
|
+
|
|
188
|
+
if (!consentData.analytics_Storage && config.track) {
|
|
189
|
+
config.track = false;
|
|
190
|
+
clear(true);
|
|
191
|
+
clarity.stop();
|
|
192
|
+
window.setTimeout(clarity.start, Setting.RestartDelay);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (core.active() && consentData.analytics_Storage) {
|
|
197
|
+
config.track = true;
|
|
198
|
+
track(user(), BooleanFlag.True);
|
|
199
|
+
save();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
logCookies();
|
|
203
|
+
trackConsent.trackConsentv2(consentData);
|
|
204
|
+
trackConsent.consent();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function getConsentData(consentState: ConsentState): ConsentData {
|
|
208
|
+
let consent: ConsentData = {
|
|
209
|
+
source: consentState.source ?? ConsentSource.Unknown,
|
|
210
|
+
ad_Storage: consentState.ad_Storage === Constant.Granted ? BooleanFlag.True : BooleanFlag.False,
|
|
211
|
+
analytics_Storage: consentState.analytics_Storage === Constant.Granted ? BooleanFlag.True : BooleanFlag.False,
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
return consent;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function normalizeConsent(value: unknown, fallback: string = Constant.Denied): string {
|
|
218
|
+
return typeof value === 'string' ? value.toLowerCase() : fallback;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function clear(all: boolean = false): void {
|
|
222
|
+
// Clear any stored information in the cookie that tracks session information so we can restart fresh the next time
|
|
223
|
+
setCookie(Constant.SessionKey, Constant.Empty, 0);
|
|
224
|
+
|
|
225
|
+
// Clear user cookie as well if all flag is set
|
|
226
|
+
if (all) {
|
|
227
|
+
setCookie(Constant.CookieKey, Constant.Empty, 0);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function tab(): string {
|
|
232
|
+
let id = shortid();
|
|
233
|
+
if (config.track && supported(window, Constant.SessionStorage)) {
|
|
234
|
+
let value = sessionStorage.getItem(Constant.TabKey);
|
|
235
|
+
id = value ? value : id;
|
|
236
|
+
sessionStorage.setItem(Constant.TabKey, id);
|
|
237
|
+
}
|
|
238
|
+
return id;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function callback(consentUpdate: boolean = false): void {
|
|
242
|
+
let upgrade = config.lean ? BooleanFlag.False : BooleanFlag.True;
|
|
243
|
+
processCallback(upgrade, consentUpdate);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function save(): void {
|
|
247
|
+
if (!data || !config.track || config.externalSession) return;
|
|
248
|
+
let ts = Math.round(Date.now());
|
|
249
|
+
let upload = config.upload && typeof config.upload === Constant.String ? (config.upload as string).replace(Constant.HTTPS, Constant.Empty) : Constant.Empty;
|
|
250
|
+
let upgrade = config.lean ? BooleanFlag.False : BooleanFlag.True;
|
|
251
|
+
setCookie(Constant.SessionKey, [data.sessionId, ts, data.pageNum, upgrade, upload].join(COOKIE_SEP), Setting.SessionExpire);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function processCallback(upgrade: BooleanFlag, consentUpdate: boolean = false): void {
|
|
255
|
+
if (callbacks.length > 0) {
|
|
256
|
+
for (let i = 0; i < callbacks.length; i++) {
|
|
257
|
+
const cb = callbacks[i];
|
|
258
|
+
if (
|
|
259
|
+
cb.callback &&
|
|
260
|
+
((!cb.called && !consentUpdate) || (cb.consentInfo && consentUpdate)) && //If consentUpdate is true, we only call the callback if it has consentInfo
|
|
261
|
+
(!cb.wait || upgrade)
|
|
262
|
+
) {
|
|
263
|
+
cb.callback(data, !config.lean, cb.consentInfo ? consentStatus : undefined);
|
|
264
|
+
cb.called = true;
|
|
265
|
+
if (!cb.recall) {
|
|
266
|
+
callbacks.splice(i, 1);
|
|
267
|
+
i--;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function track(u: User, consent: BooleanFlag = null): void {
|
|
275
|
+
// If consent is not explicitly specified, infer it from the user object
|
|
276
|
+
consent = consent === null ? u.consent : consent;
|
|
277
|
+
// Convert time precision into days to reduce number of bytes we have to write in a cookie
|
|
278
|
+
// E.g. Math.ceil(1628735962643 / (24*60*60*1000)) => 18852 (days) => ejo in base36 (13 bytes => 3 bytes)
|
|
279
|
+
let end = Math.ceil((Date.now() + (Setting.Expire * Time.Day)) / Time.Day);
|
|
280
|
+
// If DOB is not set in the user object, use the date set in the config as a DOB
|
|
281
|
+
let dob = u.dob === 0 ? (config.dob === null ? 0 : config.dob) : u.dob;
|
|
282
|
+
|
|
283
|
+
// To avoid cookie churn, write user id cookie only once every day
|
|
284
|
+
if (u.expiry === null || Math.abs(end - u.expiry) >= Setting.CookieInterval || u.consent !== consent || u.dob !== dob) {
|
|
285
|
+
let cookieParts = [data.userId, Setting.CookieVersion, end.toString(36), consent, dob];
|
|
286
|
+
setCookie(Constant.CookieKey, cookieParts.join(COOKIE_SEP), Setting.Expire);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function shortid(): string {
|
|
291
|
+
let id = Math.floor(Math.random() * Math.pow(2, 32));
|
|
292
|
+
if (window && window.crypto && window.crypto.getRandomValues && Uint32Array) {
|
|
293
|
+
id = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
294
|
+
}
|
|
295
|
+
return id.toString(36);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function session(): Session {
|
|
299
|
+
let output: Session = { session: shortid(), ts: Math.round(Date.now()), count: 1, upgrade: null, upload: Constant.Empty };
|
|
300
|
+
let value = getCookie(Constant.SessionKey, !config.includeSubdomains);
|
|
301
|
+
if (value) {
|
|
302
|
+
// Maintaining support for pipe separator for backward compatibility, this can be removed in future releases
|
|
303
|
+
let parts = value.includes(Constant.Caret) ? value.split(Constant.Caret) : value.split(Constant.Pipe);
|
|
304
|
+
// Making it backward & forward compatible by using greater than comparison (v0.6.21)
|
|
305
|
+
// In future version, we can reduce the parts length to be 5 where the last part contains the full upload URL
|
|
306
|
+
if (parts.length >= 5 && output.ts - num(parts[1]) < Setting.SessionTimeout) {
|
|
307
|
+
output.session = parts[0];
|
|
308
|
+
output.count = num(parts[2]) + 1;
|
|
309
|
+
output.upgrade = num(parts[3]);
|
|
310
|
+
output.upload = parts.length >= 6 ? `${Constant.HTTPS}${parts[5]}/${parts[4]}` : `${Constant.HTTPS}${parts[4]}`;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return output;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function num(string: string, base: number = 10): number {
|
|
317
|
+
return parseInt(string, base);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function user(): User {
|
|
321
|
+
let output: User = { id: shortid(), version: 0, expiry: null, consent: BooleanFlag.False, dob: 0 };
|
|
322
|
+
let cookie = getCookie(Constant.CookieKey, !config.includeSubdomains);
|
|
323
|
+
if (cookie && cookie.length > 0) {
|
|
324
|
+
// Splitting and looking up first part for forward compatibility, in case we wish to store additional information in a cookie
|
|
325
|
+
// Maintaining support for pipe separator for backward compatibility, this can be removed in future releases
|
|
326
|
+
let parts = cookie.includes(Constant.Caret) ? cookie.split(Constant.Caret) : cookie.split(Constant.Pipe);
|
|
327
|
+
// Read version information and timestamp from cookie, if available
|
|
328
|
+
if (parts.length > 1) { output.version = num(parts[1]); }
|
|
329
|
+
if (parts.length > 2) { output.expiry = num(parts[2], 36); }
|
|
330
|
+
// Check if we have explicit consent to track this user
|
|
331
|
+
if (parts.length > 3 && num(parts[3]) === 1) { output.consent = BooleanFlag.True; }
|
|
332
|
+
if (parts.length > 4 && num(parts[1]) > 1) { output.dob = num(parts[4]); }
|
|
333
|
+
// Set track configuration to true for this user if we have explicit consent, regardless of project setting
|
|
334
|
+
config.track = config.track || output.consent === BooleanFlag.True;
|
|
335
|
+
// Get user id from cookie only if we tracking is enabled, otherwise fallback to a random id
|
|
336
|
+
output.id = config.track ? parts[0] : output.id;
|
|
337
|
+
}
|
|
338
|
+
return output;
|
|
339
|
+
}
|
|
340
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Event, Metric, MetricData } from "@clarity-types/data";
|
|
2
|
+
import encode from "./encode";
|
|
3
|
+
|
|
4
|
+
export let data: MetricData = null;
|
|
5
|
+
export let updates: MetricData = null;
|
|
6
|
+
|
|
7
|
+
export function start(): void {
|
|
8
|
+
data = {};
|
|
9
|
+
updates = {};
|
|
10
|
+
count(Metric.InvokeCount);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function stop(): void {
|
|
14
|
+
data = {};
|
|
15
|
+
updates = {};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function count(metric: Metric): void {
|
|
19
|
+
if (!(metric in data)) { data[metric] = 0; }
|
|
20
|
+
if (!(metric in updates)) { updates[metric] = 0; }
|
|
21
|
+
data[metric]++;
|
|
22
|
+
updates[metric]++;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function sum(metric: Metric, value: number): void {
|
|
26
|
+
if (value !== null) {
|
|
27
|
+
if (!(metric in data)) { data[metric] = 0; }
|
|
28
|
+
if (!(metric in updates)) { updates[metric] = 0; }
|
|
29
|
+
data[metric] += value;
|
|
30
|
+
updates[metric] += value;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function max(metric: Metric, value: number): void {
|
|
35
|
+
// Ensure that we do not process null or NaN values
|
|
36
|
+
if (value !== null && isNaN(value) === false) {
|
|
37
|
+
if (!(metric in data)) { data[metric] = 0; }
|
|
38
|
+
if (value > data[metric] || data[metric] === 0) {
|
|
39
|
+
updates[metric] = value;
|
|
40
|
+
data[metric] = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function compute(): void {
|
|
46
|
+
encode(Event.Metric);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function reset(): void {
|
|
50
|
+
updates = {};
|
|
51
|
+
}
|
package/src/data/ping.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Event, PingData, Setting } from "@clarity-types/data";
|
|
2
|
+
import { suspend } from "@src/core";
|
|
3
|
+
import { time } from "@src/core/time";
|
|
4
|
+
import { clearTimeout, setTimeout } from "@src/core/timeout";
|
|
5
|
+
import encode from "./encode";
|
|
6
|
+
|
|
7
|
+
export let data: PingData;
|
|
8
|
+
let last = 0;
|
|
9
|
+
let interval = 0;
|
|
10
|
+
let timeout: number = null;
|
|
11
|
+
|
|
12
|
+
export function start(): void {
|
|
13
|
+
interval = Setting.PingInterval;
|
|
14
|
+
last = 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function reset(): void {
|
|
18
|
+
if (timeout) { clearTimeout(timeout); }
|
|
19
|
+
timeout = setTimeout(ping, interval);
|
|
20
|
+
last = time();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function ping(): void {
|
|
24
|
+
let now = time();
|
|
25
|
+
data = { gap: now - last };
|
|
26
|
+
encode(Event.Ping);
|
|
27
|
+
if (data.gap < Setting.PingTimeout) {
|
|
28
|
+
timeout = setTimeout(ping, interval);
|
|
29
|
+
} else { suspend(); }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function stop(): void {
|
|
33
|
+
clearTimeout(timeout);
|
|
34
|
+
last = 0;
|
|
35
|
+
interval = 0;
|
|
36
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ClaritySignal, SignalCallback } from '@clarity-types/data';
|
|
2
|
+
|
|
3
|
+
export let signalCallback: SignalCallback = null;
|
|
4
|
+
|
|
5
|
+
export function signal(cb: SignalCallback): void {
|
|
6
|
+
signalCallback = cb;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function parseSignals(signalsPayload: string): ClaritySignal[] {
|
|
10
|
+
try{
|
|
11
|
+
const parsedSignals: ClaritySignal[] = JSON.parse(signalsPayload);
|
|
12
|
+
return parsedSignals;
|
|
13
|
+
}catch{
|
|
14
|
+
return []
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function signalsEvent(signalsPayload: string) {
|
|
19
|
+
try {
|
|
20
|
+
if (!signalCallback) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const signals = parseSignals(signalsPayload);
|
|
24
|
+
signals.forEach((signal) => {
|
|
25
|
+
signalCallback(signal);
|
|
26
|
+
});
|
|
27
|
+
} catch {
|
|
28
|
+
//do nothing
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Event, SummaryData, Setting } from "@clarity-types/data";
|
|
2
|
+
import encode from "./encode";
|
|
3
|
+
|
|
4
|
+
export let data: SummaryData = null;
|
|
5
|
+
|
|
6
|
+
export function start(): void {
|
|
7
|
+
data = {};
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function stop(): void {
|
|
11
|
+
data = {};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function track(event: Event, time: number): void {
|
|
15
|
+
if (!(event in data)) {
|
|
16
|
+
data[event] = [[time, 0]];
|
|
17
|
+
} else {
|
|
18
|
+
let e = data[event];
|
|
19
|
+
let last = e[e.length - 1];
|
|
20
|
+
// Add a new entry only if the new event occurs after configured interval
|
|
21
|
+
// Otherwise, extend the duration of the previous entry
|
|
22
|
+
if (time - last[0] > Setting.SummaryInterval) {
|
|
23
|
+
data[event].push([time, 0]);
|
|
24
|
+
} else { last[1] = time - last[0]; }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function compute(): void {
|
|
29
|
+
encode(Event.Summary);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function reset(): void {
|
|
33
|
+
data = {};
|
|
34
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {Constant, Token} from "@clarity-types/data";
|
|
2
|
+
|
|
3
|
+
// Following code takes an array of tokens and transforms it to optimize for repeating tokens and make it efficient to send over the wire
|
|
4
|
+
// The way it works is that it iterate over all tokens and checks if the current token was already seen in the tokens array so far
|
|
5
|
+
// If so, it replaces the token with its reference (index). This helps us save bytes by not repeating the same value twice.
|
|
6
|
+
// E.g. If tokens array is: ["hello", "world", "coding", "language", "world", "language", "example"]
|
|
7
|
+
// Then the resulting tokens array after following code execution would be: ["hello", "world", "coding", "language", [1, 3], "example"]
|
|
8
|
+
// Where [1,3] points to tokens[1] => "world" and tokens[3] => "language"
|
|
9
|
+
export default function(tokens: Token[]): Token[] {
|
|
10
|
+
let output: Token[] = [];
|
|
11
|
+
let lookup: {[key: string]: number} = {};
|
|
12
|
+
let pointer = 0;
|
|
13
|
+
let reference = null;
|
|
14
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
15
|
+
// Only optimize for string values
|
|
16
|
+
if (typeof tokens[i] === Constant.String) {
|
|
17
|
+
let token = tokens[i] as string;
|
|
18
|
+
let index = lookup[token] || -1;
|
|
19
|
+
if (index >= 0) {
|
|
20
|
+
if (reference) { reference.push(index); } else {
|
|
21
|
+
reference = [index];
|
|
22
|
+
output.push(reference);
|
|
23
|
+
pointer++;
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
reference = null;
|
|
27
|
+
output.push(token);
|
|
28
|
+
lookup[token] = pointer++;
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
// If the value is anything other than string, append it as it is to the output array
|
|
32
|
+
// And, also increment the pointer to stay in sync with output array
|
|
33
|
+
reference = null;
|
|
34
|
+
output.push(tokens[i]);
|
|
35
|
+
pointer++;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return output;
|
|
39
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Constant, Event, UpgradeData } from "@clarity-types/data";
|
|
2
|
+
import * as core from "@src/core";
|
|
3
|
+
import config from "@src/core/config";
|
|
4
|
+
import encode from "@src/data/encode";
|
|
5
|
+
import * as metadata from "@src/data/metadata";
|
|
6
|
+
import * as discover from "@src/layout/discover";
|
|
7
|
+
import * as style from "@src/layout/style";
|
|
8
|
+
|
|
9
|
+
export let data: UpgradeData = null;
|
|
10
|
+
|
|
11
|
+
export function start(): void {
|
|
12
|
+
if (!config.lean && config.upgrade) { config.upgrade(Constant.Config); }
|
|
13
|
+
data = null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Following call will upgrade the session from lean mode into the full mode retroactively from the start of the page.
|
|
17
|
+
// As part of the lean mode, we do not send back any layout information - including discovery of DOM and mutations.
|
|
18
|
+
// However, if there's a need for full fidelity playback, calling this function will disable lean mode
|
|
19
|
+
// and send all backed up layout events to the server.
|
|
20
|
+
export function upgrade(key: string): void {
|
|
21
|
+
// Upgrade only if Clarity was successfully activated on the page
|
|
22
|
+
if (core.active() && config.lean) {
|
|
23
|
+
config.lean = false;
|
|
24
|
+
data = { key };
|
|
25
|
+
|
|
26
|
+
// Update metadata to track we have upgraded this session
|
|
27
|
+
metadata.callback();
|
|
28
|
+
metadata.save();
|
|
29
|
+
|
|
30
|
+
// Callback upgrade handler, if configured
|
|
31
|
+
if (config.upgrade) { config.upgrade(key); }
|
|
32
|
+
|
|
33
|
+
encode(Event.Upgrade);
|
|
34
|
+
|
|
35
|
+
if (config.lite) {
|
|
36
|
+
discover.start();
|
|
37
|
+
style.start();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function stop(): void {
|
|
43
|
+
data = null;
|
|
44
|
+
}
|