@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,69 @@
|
|
|
1
|
+
import { Constant, NodeInfo } from "@clarity-types/layout";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom module for tracking HTML Dialog elements
|
|
5
|
+
* Handles detection of modal vs non-modal dialogs for proper visualization
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface DialogTrackingResult {
|
|
9
|
+
attributes: { [key: string]: string };
|
|
10
|
+
isModal: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Tracks dialog element state and determines if it's a modal dialog
|
|
15
|
+
* Modal dialogs are opened via showModal() and rendered in the browser's top-layer
|
|
16
|
+
*
|
|
17
|
+
* @param dialogElement - The dialog element to track
|
|
18
|
+
* @param attributes - Current attributes of the dialog
|
|
19
|
+
* @returns Tracking result with updated attributes and modal state
|
|
20
|
+
*/
|
|
21
|
+
export function trackDialog(
|
|
22
|
+
dialogElement: HTMLDialogElement,
|
|
23
|
+
attributes: { [key: string]: string }
|
|
24
|
+
): DialogTrackingResult {
|
|
25
|
+
const result: DialogTrackingResult = {
|
|
26
|
+
attributes: { ...attributes },
|
|
27
|
+
isModal: false
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Capture the open attribute which indicates if dialog is currently shown
|
|
31
|
+
if (dialogElement.open) {
|
|
32
|
+
result.attributes["open"] = "";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Detect if this is a modal dialog by checking for ::backdrop pseudo-element
|
|
36
|
+
// Modal dialogs shown via showModal() have a backdrop, non-modal dialogs don't
|
|
37
|
+
if (dialogElement.open) {
|
|
38
|
+
try {
|
|
39
|
+
const backdropStyle = window.getComputedStyle(dialogElement, "::backdrop");
|
|
40
|
+
const backdropDisplay = backdropStyle.getPropertyValue("display");
|
|
41
|
+
|
|
42
|
+
// Modal dialogs have ::backdrop with display !== 'none'
|
|
43
|
+
if (backdropDisplay && backdropDisplay !== "none") {
|
|
44
|
+
result.isModal = true;
|
|
45
|
+
result.attributes[Constant.GXDialogModal] = "true";
|
|
46
|
+
}
|
|
47
|
+
} catch (e) {
|
|
48
|
+
// If we can't check for backdrop (older browsers),
|
|
49
|
+
// we can't reliably detect modal state
|
|
50
|
+
console.warn("Error detecting modal dialog:", e);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Creates dialog node data for tracking
|
|
59
|
+
*
|
|
60
|
+
* @param tag - Tag name (should be "DIALOG")
|
|
61
|
+
* @param attributes - Dialog attributes
|
|
62
|
+
* @returns NodeInfo object ready for tracking
|
|
63
|
+
*/
|
|
64
|
+
export function createDialogNodeData(
|
|
65
|
+
tag: string,
|
|
66
|
+
attributes: { [key: string]: string }
|
|
67
|
+
): NodeInfo {
|
|
68
|
+
return { tag, attributes };
|
|
69
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { Event, BooleanFlag } from "@clarity-types/data";
|
|
2
|
+
import { BaselineData, BaselineState } from "@clarity-types/data";
|
|
3
|
+
import { time } from "@src/core/time";
|
|
4
|
+
import encode from "@src/data/encode";
|
|
5
|
+
|
|
6
|
+
export let state: BaselineState = null;
|
|
7
|
+
let buffer: BaselineData = null;
|
|
8
|
+
let update: boolean = false;
|
|
9
|
+
|
|
10
|
+
export function start(): void {
|
|
11
|
+
update = false;
|
|
12
|
+
reset();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function reset(): void {
|
|
16
|
+
// Baseline state holds the previous values - if it is updated in the current payload,
|
|
17
|
+
// reset the state to current value after sending the previous state
|
|
18
|
+
if (update) {
|
|
19
|
+
state = { time: time(), event: Event.Baseline, data: {
|
|
20
|
+
visible: buffer.visible,
|
|
21
|
+
docWidth: buffer.docWidth,
|
|
22
|
+
docHeight: buffer.docHeight,
|
|
23
|
+
screenWidth: buffer.screenWidth,
|
|
24
|
+
screenHeight: buffer.screenHeight,
|
|
25
|
+
scrollX: buffer.scrollX,
|
|
26
|
+
scrollY: buffer.scrollY,
|
|
27
|
+
pointerX: buffer.pointerX,
|
|
28
|
+
pointerY: buffer.pointerY,
|
|
29
|
+
activityTime: buffer.activityTime,
|
|
30
|
+
scrollTime: buffer.scrollTime,
|
|
31
|
+
pointerTime: buffer.pointerTime,
|
|
32
|
+
moveX: buffer.moveX,
|
|
33
|
+
moveY: buffer.moveY,
|
|
34
|
+
moveTime: buffer.moveTime,
|
|
35
|
+
downX: buffer.downX,
|
|
36
|
+
downY: buffer.downY,
|
|
37
|
+
downTime: buffer.downTime,
|
|
38
|
+
upX: buffer.upX,
|
|
39
|
+
upY: buffer.upY,
|
|
40
|
+
upTime: buffer.upTime,
|
|
41
|
+
pointerPrevX: buffer.pointerPrevX,
|
|
42
|
+
pointerPrevY: buffer.pointerPrevY,
|
|
43
|
+
pointerPrevTime: buffer.pointerPrevTime,
|
|
44
|
+
modules: buffer.modules,
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
buffer = buffer ? buffer : {
|
|
49
|
+
visible: BooleanFlag.True,
|
|
50
|
+
docWidth: 0,
|
|
51
|
+
docHeight: 0,
|
|
52
|
+
screenWidth: 0,
|
|
53
|
+
screenHeight: 0,
|
|
54
|
+
scrollX: 0,
|
|
55
|
+
scrollY: 0,
|
|
56
|
+
pointerX: 0,
|
|
57
|
+
pointerY: 0,
|
|
58
|
+
activityTime: 0,
|
|
59
|
+
scrollTime: 0,
|
|
60
|
+
pointerTime: undefined,
|
|
61
|
+
moveX: undefined,
|
|
62
|
+
moveY: undefined,
|
|
63
|
+
moveTime: undefined,
|
|
64
|
+
downX: undefined,
|
|
65
|
+
downY: undefined,
|
|
66
|
+
downTime: undefined,
|
|
67
|
+
upX: undefined,
|
|
68
|
+
upY: undefined,
|
|
69
|
+
upTime: undefined,
|
|
70
|
+
pointerPrevX: undefined,
|
|
71
|
+
pointerPrevY: undefined,
|
|
72
|
+
pointerPrevTime: undefined,
|
|
73
|
+
modules: null,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function track(event: Event, x: number, y: number, time?: number): void {
|
|
78
|
+
switch (event) {
|
|
79
|
+
case Event.Document:
|
|
80
|
+
buffer.docWidth = x;
|
|
81
|
+
buffer.docHeight = y;
|
|
82
|
+
break;
|
|
83
|
+
case Event.Resize:
|
|
84
|
+
buffer.screenWidth = x;
|
|
85
|
+
buffer.screenHeight = y;
|
|
86
|
+
break;
|
|
87
|
+
case Event.Scroll:
|
|
88
|
+
buffer.scrollX = x;
|
|
89
|
+
buffer.scrollY = y;
|
|
90
|
+
buffer.scrollTime = time;
|
|
91
|
+
break;
|
|
92
|
+
case Event.MouseMove:
|
|
93
|
+
buffer.moveX = x;
|
|
94
|
+
buffer.moveY = y;
|
|
95
|
+
buffer.moveTime = time;
|
|
96
|
+
buffer.pointerPrevX = buffer.pointerX;
|
|
97
|
+
buffer.pointerPrevY = buffer.pointerY;
|
|
98
|
+
buffer.pointerPrevTime = buffer.pointerTime;
|
|
99
|
+
buffer.pointerX = x;
|
|
100
|
+
buffer.pointerY = y;
|
|
101
|
+
buffer.pointerTime = time;
|
|
102
|
+
break;
|
|
103
|
+
case Event.MouseDown:
|
|
104
|
+
buffer.downX = x;
|
|
105
|
+
buffer.downY = y;
|
|
106
|
+
buffer.downTime = time;
|
|
107
|
+
buffer.pointerPrevX = buffer.pointerX;
|
|
108
|
+
buffer.pointerPrevY = buffer.pointerY;
|
|
109
|
+
buffer.pointerPrevTime = buffer.pointerTime;
|
|
110
|
+
buffer.pointerX = x;
|
|
111
|
+
buffer.pointerY = y;
|
|
112
|
+
buffer.pointerTime = time;
|
|
113
|
+
break;
|
|
114
|
+
case Event.MouseUp:
|
|
115
|
+
buffer.upX = x;
|
|
116
|
+
buffer.upY = y;
|
|
117
|
+
buffer.upTime = time;
|
|
118
|
+
buffer.pointerPrevX = buffer.pointerX;
|
|
119
|
+
buffer.pointerPrevY = buffer.pointerY;
|
|
120
|
+
buffer.pointerPrevTime = buffer.pointerTime;
|
|
121
|
+
buffer.pointerX = x;
|
|
122
|
+
buffer.pointerY = y;
|
|
123
|
+
buffer.pointerTime = time;
|
|
124
|
+
break;
|
|
125
|
+
default:
|
|
126
|
+
buffer.pointerPrevX = buffer.pointerX;
|
|
127
|
+
buffer.pointerPrevY = buffer.pointerY;
|
|
128
|
+
buffer.pointerPrevTime = buffer.pointerTime;
|
|
129
|
+
buffer.pointerX = x;
|
|
130
|
+
buffer.pointerY = y;
|
|
131
|
+
buffer.pointerTime = time;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
update = true;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function activity(t: number): void {
|
|
138
|
+
buffer.activityTime = t;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function visibility(t: number, visible: BooleanFlag): void {
|
|
142
|
+
buffer.visible = visible;
|
|
143
|
+
if (!buffer.visible) {
|
|
144
|
+
activity(t);
|
|
145
|
+
}
|
|
146
|
+
update = true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function dynamic(modules: Set<number>): void {
|
|
150
|
+
buffer.modules = Array.from(modules);
|
|
151
|
+
update = true;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function compute(): void {
|
|
155
|
+
if (update) {
|
|
156
|
+
encode(Event.Baseline);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function stop(): void {
|
|
161
|
+
reset();
|
|
162
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Constant } from "@clarity-types/data";
|
|
2
|
+
|
|
3
|
+
const supported = Constant.CompressionStream in window;
|
|
4
|
+
|
|
5
|
+
export default async function(input: string): Promise<Uint8Array> {
|
|
6
|
+
try {
|
|
7
|
+
if (supported) {
|
|
8
|
+
// Create a readable stream from given input string and
|
|
9
|
+
// pipe it through text encoder and compression stream to gzip it
|
|
10
|
+
const stream = new ReadableStream({async start(controller) {
|
|
11
|
+
controller.enqueue(input);
|
|
12
|
+
controller.close();
|
|
13
|
+
}}).pipeThrough(new TextEncoderStream()).pipeThrough(new window[Constant.CompressionStream]("gzip"));
|
|
14
|
+
return new Uint8Array(await read(stream));
|
|
15
|
+
}
|
|
16
|
+
} catch { /* do nothing */ }
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function read(stream: ReadableStream): Promise<number[]> {
|
|
21
|
+
const reader = stream.getReader();
|
|
22
|
+
const chunks:number[] = [];
|
|
23
|
+
let done = false;
|
|
24
|
+
let value: number[] = [];
|
|
25
|
+
while (!done) {
|
|
26
|
+
({ done, value } = await reader.read());
|
|
27
|
+
if (done) { return chunks; }
|
|
28
|
+
chunks.push(...value);
|
|
29
|
+
}
|
|
30
|
+
return chunks;
|
|
31
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ConsentData, ConsentSource, ConsentState, ConsentType, Constant, Dimension, Event, GCMConsent, GCMConsentState } from "@clarity-types/data";
|
|
2
|
+
import * as dimension from "@src/data/dimension";
|
|
3
|
+
import encode from "./encode";
|
|
4
|
+
import { consentv2 } from "@src/data/metadata";
|
|
5
|
+
import coreConfig from "@src/core/config";
|
|
6
|
+
|
|
7
|
+
export let data: ConsentData = null;
|
|
8
|
+
let updateConsent: boolean = true;
|
|
9
|
+
|
|
10
|
+
export function start(): void {
|
|
11
|
+
const ics = window.google_tag_data?.ics;
|
|
12
|
+
updateConsent = true;
|
|
13
|
+
if (ics?.addListener) {
|
|
14
|
+
ics.addListener(
|
|
15
|
+
[Constant.AdStorage, Constant.AnalyticsStorage],
|
|
16
|
+
processConsent
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function stop(): void {
|
|
22
|
+
updateConsent = true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function processConsent(): void {
|
|
26
|
+
const ics = window.google_tag_data?.ics;
|
|
27
|
+
if (!ics?.getConsentState) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const analytics_storage = ics.getConsentState(Constant.AnalyticsStorage);
|
|
33
|
+
const ad_storage = ics.getConsentState(Constant.AdStorage);
|
|
34
|
+
const consentState = getGcmConsentState({ ad_Storage: ad_storage, analytics_Storage: analytics_storage });
|
|
35
|
+
consentv2(consentState);
|
|
36
|
+
} catch {
|
|
37
|
+
// Handle GTM errors gracefully (e.g., misconfigured consent initialization)
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function getGcmConsentState(googleConsent: GCMConsentState): ConsentState {
|
|
43
|
+
const consentState: ConsentState = {
|
|
44
|
+
source: ConsentSource.GCM,
|
|
45
|
+
ad_Storage: googleConsent.ad_Storage === GCMConsent.Granted ? Constant.Granted : Constant.Denied,
|
|
46
|
+
analytics_Storage: googleConsent.analytics_Storage === GCMConsent.Granted ? Constant.Granted : Constant.Denied,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return consentState;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
export function config(consent: ConsentData): void {
|
|
54
|
+
trackConsent(consent.analytics_Storage ? ConsentType.Implicit : ConsentType.None);
|
|
55
|
+
data = consent;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// When we get consent signal as false, we restart the service and track config as false.
|
|
59
|
+
export function consent(): void {
|
|
60
|
+
trackConsent(ConsentType.General);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function trackConsent(consent: ConsentType): void {
|
|
64
|
+
dimension.log(Dimension.Consent, consent.toString());
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function trackConsentv2(consent: ConsentData): void {
|
|
68
|
+
data = consent;
|
|
69
|
+
encode(Event.Consent);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Compute function is called every upload, but we only want to send consent data once.
|
|
73
|
+
export function compute(): void {
|
|
74
|
+
if (updateConsent) {
|
|
75
|
+
encode(Event.Consent);
|
|
76
|
+
updateConsent = false;
|
|
77
|
+
if (!coreConfig.track) {
|
|
78
|
+
const ics = window.google_tag_data?.ics;
|
|
79
|
+
if (ics?.usedUpdate) {
|
|
80
|
+
processConsent();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Constant } from "@clarity-types/data";
|
|
2
|
+
import config from "@src/core/config";
|
|
3
|
+
import { decodeCookieValue, encodeCookieValue, supported } from "@src/data/util";
|
|
4
|
+
|
|
5
|
+
let rootDomain = null;
|
|
6
|
+
export const COOKIE_SEP = Constant.Caret;
|
|
7
|
+
|
|
8
|
+
export function start() {
|
|
9
|
+
rootDomain = null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function stop() {
|
|
13
|
+
rootDomain = null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getCookie(key: string, limit = false): string {
|
|
17
|
+
if (supported(document, Constant.Cookie)) {
|
|
18
|
+
let cookies: string[] = document.cookie.split(Constant.Semicolon);
|
|
19
|
+
if (cookies) {
|
|
20
|
+
for (let i = 0; i < cookies.length; i++) {
|
|
21
|
+
let pair: string[] = cookies[i].split(Constant.Equals);
|
|
22
|
+
if (pair.length > 1 && pair[0] && pair[0].trim() === key) {
|
|
23
|
+
// Some browsers automatically url encode cookie values if they are not url encoded.
|
|
24
|
+
// We therefore encode and decode cookie values ourselves.
|
|
25
|
+
// For backwards compatability we need to consider 3 cases:
|
|
26
|
+
// * Cookie was previously not encoded by Clarity and browser did not encode it
|
|
27
|
+
// * Cookie was previously not encoded by Clarity and browser encoded it once or more
|
|
28
|
+
// * Cookie was previously encoded by Clarity and browser did not encode it
|
|
29
|
+
let [isEncoded, decodedValue] = decodeCookieValue(pair[1]);
|
|
30
|
+
|
|
31
|
+
while (isEncoded) {
|
|
32
|
+
[isEncoded, decodedValue] = decodeCookieValue(decodedValue);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// If we are limiting cookies, check if the cookie value is limited
|
|
36
|
+
if (limit) {
|
|
37
|
+
return decodedValue.endsWith(`${Constant.Tilde}1`) ? decodedValue.substring(0, decodedValue.length - 2) : null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return decodedValue;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function setCookie(key: string, value: string, time: number): void {
|
|
49
|
+
// only write cookies if we are currently in a cookie writing mode (and they are supported)
|
|
50
|
+
// OR if we are trying to write an empty cookie (i.e. clear the cookie value out)
|
|
51
|
+
if ((config.track || value == Constant.Empty) && ((navigator && navigator.cookieEnabled) || supported(document, Constant.Cookie))) {
|
|
52
|
+
// Some browsers automatically url encode cookie values if they are not url encoded.
|
|
53
|
+
// We therefore encode and decode cookie values ourselves.
|
|
54
|
+
let encodedValue = encodeCookieValue(value);
|
|
55
|
+
|
|
56
|
+
let expiry = new Date();
|
|
57
|
+
expiry.setDate(expiry.getDate() + time);
|
|
58
|
+
let expires = expiry ? Constant.Expires + expiry.toUTCString() : Constant.Empty;
|
|
59
|
+
let cookie = `${key}=${encodedValue}${Constant.Semicolon}${expires}${Constant.Path}`;
|
|
60
|
+
try {
|
|
61
|
+
// Attempt to get the root domain only once and fall back to writing cookie on the current domain.
|
|
62
|
+
if (rootDomain === null) {
|
|
63
|
+
let hostname = location.hostname ? location.hostname.split(Constant.Dot) : [];
|
|
64
|
+
// Walk backwards on a domain and attempt to set a cookie, until successful
|
|
65
|
+
for (let i = hostname.length - 1; i >= 0; i--) {
|
|
66
|
+
rootDomain = `.${hostname[i]}${rootDomain ? rootDomain : Constant.Empty}`;
|
|
67
|
+
// We do not wish to attempt writing a cookie on the absolute last part of the domain, e.g. .com or .net.
|
|
68
|
+
// So we start attempting after second-last part, e.g. .domain.com (PASS) or .co.uk (FAIL)
|
|
69
|
+
if (i < hostname.length - 1) {
|
|
70
|
+
// Write the cookie on the current computed top level domain
|
|
71
|
+
document.cookie = `${cookie}${Constant.Semicolon}${Constant.Domain}${rootDomain}`;
|
|
72
|
+
// Once written, check if the cookie exists and its value matches exactly with what we intended to set
|
|
73
|
+
// Checking for exact value match helps us eliminate a corner case where the cookie may already be present with a different value
|
|
74
|
+
// If the check is successful, no more action is required and we can return from the function since rootDomain cookie is already set
|
|
75
|
+
// If the check fails, continue with the for loop until we can successfully set and verify the cookie
|
|
76
|
+
if (getCookie(key) === value) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// Finally, if we were not successful and gone through all the options, play it safe and reset rootDomain to be empty
|
|
82
|
+
// This forces our code to fall back to always writing cookie to the current domain
|
|
83
|
+
rootDomain = Constant.Empty;
|
|
84
|
+
}
|
|
85
|
+
} catch {
|
|
86
|
+
rootDomain = Constant.Empty;
|
|
87
|
+
}
|
|
88
|
+
document.cookie = rootDomain ? `${cookie}${Constant.Semicolon}${Constant.Domain}${rootDomain}` : cookie;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Constant, CustomData, Event } from "@clarity-types/data";
|
|
2
|
+
import * as core from "@src/core";
|
|
3
|
+
import encode from "./encode";
|
|
4
|
+
|
|
5
|
+
export let data: CustomData = null;
|
|
6
|
+
|
|
7
|
+
// custom events allow 2 parameters or 1 parameter to be passed. If 2 are passed we
|
|
8
|
+
// consider it a key value pair. If only 1 is passed we only consider the event to have a value.
|
|
9
|
+
export function event(a: string, b: string): void {
|
|
10
|
+
if (core.active() &&
|
|
11
|
+
a &&
|
|
12
|
+
typeof a === Constant.String &&
|
|
13
|
+
a.length < 255
|
|
14
|
+
) {
|
|
15
|
+
if (b && typeof b === Constant.String && b.length < 255) {
|
|
16
|
+
data = { key: a, value: b};
|
|
17
|
+
} else {
|
|
18
|
+
data = { value: a }
|
|
19
|
+
}
|
|
20
|
+
encode(Event.Custom);
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Check, Event, Dimension, DimensionData, Setting } from "@clarity-types/data";
|
|
2
|
+
import * as limit from "@src/data/limit";
|
|
3
|
+
import encode from "./encode";
|
|
4
|
+
|
|
5
|
+
export let data: DimensionData = null;
|
|
6
|
+
export let updates: DimensionData = null;
|
|
7
|
+
let limited = false;
|
|
8
|
+
|
|
9
|
+
export function start(): void {
|
|
10
|
+
data = {};
|
|
11
|
+
updates = {};
|
|
12
|
+
limited = false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function stop(): void {
|
|
16
|
+
data = {};
|
|
17
|
+
updates = {};
|
|
18
|
+
limited = false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function log(dimension: Dimension, value: string): void {
|
|
22
|
+
// Check valid value before moving ahead
|
|
23
|
+
if (value) {
|
|
24
|
+
// Ensure received value is casted into a string if it wasn't a string to begin with
|
|
25
|
+
value = `${value}`;
|
|
26
|
+
if (!(dimension in data)) { data[dimension] = []; }
|
|
27
|
+
if (data[dimension].indexOf(value) < 0) {
|
|
28
|
+
// Limit check to ensure we have a cap on number of dimensions we can collect
|
|
29
|
+
if (data[dimension].length > Setting.CollectionLimit) {
|
|
30
|
+
if (!limited) {
|
|
31
|
+
limited = true;
|
|
32
|
+
limit.trigger(Check.Collection);
|
|
33
|
+
}
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
data[dimension].push(value);
|
|
38
|
+
// If this is a new value, track it as part of updates object
|
|
39
|
+
// This allows us to only send back new values in subsequent payloads
|
|
40
|
+
if (!(dimension in updates)) { updates[dimension] = []; }
|
|
41
|
+
updates[dimension].push(value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function compute(): void {
|
|
47
|
+
encode(Event.Dimension);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function reset(): void {
|
|
51
|
+
updates = {};
|
|
52
|
+
limited = false;
|
|
53
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { Event, Token } from "@clarity-types/data";
|
|
2
|
+
import { time } from "@src/core/time";
|
|
3
|
+
import * as baseline from "@src/data/baseline";
|
|
4
|
+
import * as consent from "@src/data/consent";
|
|
5
|
+
import * as custom from "@src/data/custom";
|
|
6
|
+
import * as dimension from "@src/data/dimension";
|
|
7
|
+
import * as limit from "@src/data/limit";
|
|
8
|
+
import * as metric from "@src/data/metric";
|
|
9
|
+
import * as ping from "@src/data/ping";
|
|
10
|
+
import * as summary from "@src/data/summary";
|
|
11
|
+
import * as upgrade from "@src/data/upgrade";
|
|
12
|
+
import * as variable from "@src/data/variable";
|
|
13
|
+
import * as extract from "@src/data/extract";
|
|
14
|
+
import { queue, track } from "./upload";
|
|
15
|
+
|
|
16
|
+
export default function (event: Event): void {
|
|
17
|
+
const t = time();
|
|
18
|
+
let tokens: Token[] = [t, event];
|
|
19
|
+
switch (event) {
|
|
20
|
+
case Event.Baseline: {
|
|
21
|
+
const b = baseline.state;
|
|
22
|
+
if (b && b.data) {
|
|
23
|
+
tokens = [b.time, b.event];
|
|
24
|
+
tokens.push(b.data.visible);
|
|
25
|
+
tokens.push(b.data.docWidth);
|
|
26
|
+
tokens.push(b.data.docHeight);
|
|
27
|
+
tokens.push(b.data.screenWidth);
|
|
28
|
+
tokens.push(b.data.screenHeight);
|
|
29
|
+
tokens.push(b.data.scrollX);
|
|
30
|
+
tokens.push(b.data.scrollY);
|
|
31
|
+
tokens.push(b.data.pointerX);
|
|
32
|
+
tokens.push(b.data.pointerY);
|
|
33
|
+
tokens.push(b.data.activityTime);
|
|
34
|
+
tokens.push(b.data.scrollTime);
|
|
35
|
+
tokens.push(b.data.pointerTime);
|
|
36
|
+
tokens.push(b.data.moveX);
|
|
37
|
+
tokens.push(b.data.moveY);
|
|
38
|
+
tokens.push(b.data.moveTime);
|
|
39
|
+
tokens.push(b.data.downX);
|
|
40
|
+
tokens.push(b.data.downY);
|
|
41
|
+
tokens.push(b.data.downTime);
|
|
42
|
+
tokens.push(b.data.upX);
|
|
43
|
+
tokens.push(b.data.upY);
|
|
44
|
+
tokens.push(b.data.upTime);
|
|
45
|
+
tokens.push(b.data.pointerPrevX);
|
|
46
|
+
tokens.push(b.data.pointerPrevY);
|
|
47
|
+
tokens.push(b.data.pointerPrevTime);
|
|
48
|
+
tokens.push(b.data.modules);
|
|
49
|
+
queue(tokens, false);
|
|
50
|
+
}
|
|
51
|
+
baseline.reset();
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case Event.Ping:
|
|
55
|
+
tokens.push(ping.data.gap);
|
|
56
|
+
queue(tokens);
|
|
57
|
+
break;
|
|
58
|
+
case Event.Limit:
|
|
59
|
+
tokens.push(limit.data.check);
|
|
60
|
+
queue(tokens, false);
|
|
61
|
+
break;
|
|
62
|
+
case Event.Upgrade:
|
|
63
|
+
tokens.push(upgrade.data.key);
|
|
64
|
+
queue(tokens);
|
|
65
|
+
break;
|
|
66
|
+
case Event.Upload:
|
|
67
|
+
tokens.push(track.sequence);
|
|
68
|
+
tokens.push(track.attempts);
|
|
69
|
+
tokens.push(track.status);
|
|
70
|
+
queue(tokens, false);
|
|
71
|
+
break;
|
|
72
|
+
case Event.Custom:
|
|
73
|
+
// not all custom events have a key - if it wasn't passed server handles just value
|
|
74
|
+
custom.data.key && tokens.push(custom.data.key);
|
|
75
|
+
tokens.push(custom.data.value);
|
|
76
|
+
queue(tokens);
|
|
77
|
+
break;
|
|
78
|
+
case Event.Variable: {
|
|
79
|
+
const variableKeys = Object.keys(variable.data);
|
|
80
|
+
if (variableKeys.length > 0) {
|
|
81
|
+
for (const v of variableKeys) {
|
|
82
|
+
tokens.push(v);
|
|
83
|
+
tokens.push(variable.data[v]);
|
|
84
|
+
}
|
|
85
|
+
variable.reset();
|
|
86
|
+
queue(tokens, false);
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case Event.Metric: {
|
|
91
|
+
const metricKeys = Object.keys(metric.updates);
|
|
92
|
+
if (metricKeys.length > 0) {
|
|
93
|
+
for (const m of metricKeys) {
|
|
94
|
+
const key = parseInt(m, 10);
|
|
95
|
+
tokens.push(key);
|
|
96
|
+
// For computation, we need microseconds precision that performance.now() API offers
|
|
97
|
+
// However, for data over the wire, we round it off to milliseconds precision.
|
|
98
|
+
tokens.push(Math.round(metric.updates[m]));
|
|
99
|
+
}
|
|
100
|
+
metric.reset();
|
|
101
|
+
queue(tokens, false);
|
|
102
|
+
}
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case Event.Dimension: {
|
|
106
|
+
const dimensionKeys = Object.keys(dimension.updates);
|
|
107
|
+
if (dimensionKeys.length > 0) {
|
|
108
|
+
for (const d of dimensionKeys) {
|
|
109
|
+
const key = parseInt(d, 10);
|
|
110
|
+
tokens.push(key);
|
|
111
|
+
tokens.push(dimension.updates[d]);
|
|
112
|
+
}
|
|
113
|
+
dimension.reset();
|
|
114
|
+
queue(tokens, false);
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case Event.Summary: {
|
|
119
|
+
const eventKeys = Object.keys(summary.data);
|
|
120
|
+
if (eventKeys.length > 0) {
|
|
121
|
+
for (const e of eventKeys) {
|
|
122
|
+
const key = parseInt(e, 10);
|
|
123
|
+
tokens.push(key);
|
|
124
|
+
tokens.push([].concat(...summary.data[e]));
|
|
125
|
+
}
|
|
126
|
+
summary.reset();
|
|
127
|
+
queue(tokens, false);
|
|
128
|
+
}
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case Event.Extract: {
|
|
132
|
+
const extractKeys = extract.keys;
|
|
133
|
+
extractKeys.forEach(e => {
|
|
134
|
+
tokens.push(e);
|
|
135
|
+
const token = []
|
|
136
|
+
for (const d in extract.data[e]) {
|
|
137
|
+
const key = parseInt(d, 10);
|
|
138
|
+
token.push(key);
|
|
139
|
+
token.push(extract.data[e][d]);
|
|
140
|
+
}
|
|
141
|
+
tokens.push(token);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
extract.reset();
|
|
145
|
+
queue(tokens, false);
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
case Event.Consent:
|
|
149
|
+
tokens.push(consent.data.source);
|
|
150
|
+
tokens.push(consent.data.ad_Storage);
|
|
151
|
+
tokens.push(consent.data.analytics_Storage);
|
|
152
|
+
queue(tokens, false);
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|