@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.
Files changed (127) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +1 -0
  3. package/build/clarity.extended.js +1 -0
  4. package/build/clarity.insight.js +1 -0
  5. package/build/clarity.js +7148 -0
  6. package/build/clarity.livechat.js +1 -0
  7. package/build/clarity.min.js +1 -0
  8. package/build/clarity.module.js +7144 -0
  9. package/build/clarity.performance.js +1 -0
  10. package/build/dynamic/clarity.crisp.js +1 -0
  11. package/build/dynamic/clarity.tidio.js +1 -0
  12. package/package.json +49 -0
  13. package/rollup.config.ts +161 -0
  14. package/src/clarity.ts +72 -0
  15. package/src/core/api.ts +8 -0
  16. package/src/core/config.ts +35 -0
  17. package/src/core/copy.ts +3 -0
  18. package/src/core/dynamic.ts +68 -0
  19. package/src/core/event.ts +53 -0
  20. package/src/core/hash.ts +19 -0
  21. package/src/core/history.ts +82 -0
  22. package/src/core/index.ts +81 -0
  23. package/src/core/measure.ts +19 -0
  24. package/src/core/report.ts +28 -0
  25. package/src/core/scrub.ts +201 -0
  26. package/src/core/task.ts +181 -0
  27. package/src/core/throttle.ts +46 -0
  28. package/src/core/time.ts +26 -0
  29. package/src/core/timeout.ts +10 -0
  30. package/src/core/version.ts +2 -0
  31. package/src/custom/README.md +44 -0
  32. package/src/custom/dialog.ts +69 -0
  33. package/src/custom/index.ts +6 -0
  34. package/src/data/baseline.ts +162 -0
  35. package/src/data/compress.ts +31 -0
  36. package/src/data/consent.ts +84 -0
  37. package/src/data/cookie.ts +90 -0
  38. package/src/data/custom.ts +23 -0
  39. package/src/data/dimension.ts +53 -0
  40. package/src/data/encode.ts +155 -0
  41. package/src/data/envelope.ts +53 -0
  42. package/src/data/extract.ts +217 -0
  43. package/src/data/gemx-upload.ts +51 -0
  44. package/src/data/gemx.ts +35 -0
  45. package/src/data/index.ts +57 -0
  46. package/src/data/limit.ts +44 -0
  47. package/src/data/metadata.ts +340 -0
  48. package/src/data/metric.ts +51 -0
  49. package/src/data/ping.ts +36 -0
  50. package/src/data/signal.ts +30 -0
  51. package/src/data/summary.ts +34 -0
  52. package/src/data/token.ts +39 -0
  53. package/src/data/upgrade.ts +44 -0
  54. package/src/data/upload.ts +375 -0
  55. package/src/data/util.ts +18 -0
  56. package/src/data/variable.ts +84 -0
  57. package/src/diagnostic/encode.ts +40 -0
  58. package/src/diagnostic/fraud.ts +37 -0
  59. package/src/diagnostic/index.ts +13 -0
  60. package/src/diagnostic/internal.ts +28 -0
  61. package/src/diagnostic/script.ts +35 -0
  62. package/src/dynamic/agent/blank.ts +2 -0
  63. package/src/dynamic/agent/crisp.ts +40 -0
  64. package/src/dynamic/agent/encode.ts +25 -0
  65. package/src/dynamic/agent/index.ts +8 -0
  66. package/src/dynamic/agent/livechat.ts +58 -0
  67. package/src/dynamic/agent/tidio.ts +44 -0
  68. package/src/global.ts +6 -0
  69. package/src/index.ts +10 -0
  70. package/src/insight/blank.ts +14 -0
  71. package/src/insight/encode.ts +64 -0
  72. package/src/insight/snapshot.ts +115 -0
  73. package/src/interaction/change.ts +38 -0
  74. package/src/interaction/click.ts +256 -0
  75. package/src/interaction/clipboard.ts +32 -0
  76. package/src/interaction/encode.ts +217 -0
  77. package/src/interaction/focus.ts +25 -0
  78. package/src/interaction/index.ts +66 -0
  79. package/src/interaction/input.ts +66 -0
  80. package/src/interaction/pageshow.ts +35 -0
  81. package/src/interaction/pointer.ts +137 -0
  82. package/src/interaction/resize.ts +50 -0
  83. package/src/interaction/scroll.ts +134 -0
  84. package/src/interaction/selection.ts +66 -0
  85. package/src/interaction/submit.ts +30 -0
  86. package/src/interaction/timeline.ts +69 -0
  87. package/src/interaction/unload.ts +32 -0
  88. package/src/interaction/visibility.ts +28 -0
  89. package/src/layout/animation.ts +133 -0
  90. package/src/layout/constants.ts +13 -0
  91. package/src/layout/custom.ts +48 -0
  92. package/src/layout/discover.ts +31 -0
  93. package/src/layout/document.ts +46 -0
  94. package/src/layout/dom.ts +444 -0
  95. package/src/layout/encode.ts +157 -0
  96. package/src/layout/gemx-snapshot.ts +84 -0
  97. package/src/layout/index.ts +47 -0
  98. package/src/layout/mutation.ts +424 -0
  99. package/src/layout/node.ts +302 -0
  100. package/src/layout/offset.ts +19 -0
  101. package/src/layout/region.ts +151 -0
  102. package/src/layout/schema.ts +63 -0
  103. package/src/layout/selector.ts +113 -0
  104. package/src/layout/style.ts +160 -0
  105. package/src/layout/target.ts +34 -0
  106. package/src/layout/traverse.ts +28 -0
  107. package/src/performance/blank.ts +10 -0
  108. package/src/performance/encode.ts +31 -0
  109. package/src/performance/index.ts +12 -0
  110. package/src/performance/interaction.ts +125 -0
  111. package/src/performance/navigation.ts +31 -0
  112. package/src/performance/observer.ts +112 -0
  113. package/src/queue.ts +33 -0
  114. package/test/consentv2.test.ts +1309 -0
  115. package/test/hash.test.ts +68 -0
  116. package/test/time.test.ts +42 -0
  117. package/tsconfig.json +12 -0
  118. package/tslint.json +33 -0
  119. package/types/agent.d.ts +39 -0
  120. package/types/core.d.ts +156 -0
  121. package/types/data.d.ts +611 -0
  122. package/types/diagnostic.d.ts +24 -0
  123. package/types/global.d.ts +43 -0
  124. package/types/index.d.ts +46 -0
  125. package/types/interaction.d.ts +187 -0
  126. package/types/layout.d.ts +271 -0
  127. package/types/performance.d.ts +34 -0
@@ -0,0 +1,53 @@
1
+ import { BooleanFlag, Token, Upload, Envelope, ApplicationPlatform } from "@clarity-types/data";
2
+ import { time } from "@src/core/time";
3
+ import version from "@src/core/version";
4
+ import * as metadata from "@src/data/metadata";
5
+ import * as scrub from "@src/core/scrub";
6
+
7
+ export let data: Envelope = null;
8
+
9
+ export function start(): void {
10
+ const m = metadata.data;
11
+ data = {
12
+ version,
13
+ sequence: 0,
14
+ start: 0,
15
+ duration: 0,
16
+ projectId: m.projectId,
17
+ userId: m.userId,
18
+ sessionId: m.sessionId,
19
+ pageNum: m.pageNum,
20
+ upload: Upload.Async,
21
+ end: BooleanFlag.False,
22
+ applicationPlatform: ApplicationPlatform.WebApp,
23
+ url: ''
24
+ };
25
+ }
26
+
27
+ export function stop(): void {
28
+ data = null;
29
+ }
30
+
31
+ export function envelope(last: boolean): Token[] {
32
+ data.start = data.start + data.duration;
33
+ data.duration = time() - data.start;
34
+ data.sequence++;
35
+ data.upload = last && "sendBeacon" in navigator ? Upload.Beacon : Upload.Async;
36
+ data.end = last ? BooleanFlag.True : BooleanFlag.False;
37
+ data.applicationPlatform = ApplicationPlatform.WebApp;
38
+ data.url = scrub.url(location.href, false, true);
39
+ return [
40
+ data.version,
41
+ data.sequence,
42
+ data.start,
43
+ data.duration,
44
+ data.projectId,
45
+ data.userId,
46
+ data.sessionId,
47
+ data.pageNum,
48
+ data.upload,
49
+ data.end,
50
+ data.applicationPlatform,
51
+ data.url
52
+ ];
53
+ }
@@ -0,0 +1,217 @@
1
+ import { ExtractSource, Syntax, Type } from "@clarity-types/core";
2
+ import { Event, Setting, ExtractData } from "@clarity-types/data";
3
+ import encode from "./encode";
4
+ import * as internal from "@src/diagnostic/internal";
5
+ import { Code, Constant, Severity } from "@clarity-types/data";
6
+ import { hashText } from "@src/clarity";
7
+ import hash from "@src/core/hash";
8
+
9
+ export let data: ExtractData = {};
10
+ export let keys: Set<number> = new Set();
11
+
12
+ let variables : { [key: number]: { [key: number]: Syntax[] }} = {};
13
+ let selectors : { [key: number]: { [key: number]: string }} = {};
14
+ let hashes : { [key: number]: { [key: number]: string }} = {};
15
+ let validation : { [key: number]: string } = {};
16
+
17
+ export function start(): void {
18
+ reset();
19
+ }
20
+
21
+ // Input string is of the following form:
22
+ // EXTRACT 101|element { "1": ".class1", "2": "~window.a.b", "3": "!abc"}
23
+ // if element is present on the page it will set up event 101 to grab the contents of the class1 selector into component 1,
24
+ // the javascript evaluated contents of window.a.b into component 2,
25
+ // and the contents of Clarity's hash abc into component 3
26
+ export function trigger(input: string): void {
27
+ try {
28
+ var parts = input && input.length > 0 ? input.split(/ (.*)/) : [Constant.Empty];
29
+ var keyparts = parts[0].split(/\|(.*)/);
30
+ var key = parseInt(keyparts[0]);
31
+ var element = keyparts.length > 1 ? keyparts[1] : Constant.Empty;
32
+ var values = parts.length > 1 ? JSON.parse(parts[1]) : {};
33
+ variables[key] = {};
34
+ selectors[key] = {};
35
+ hashes[key] = {};
36
+ validation[key] = element;
37
+ for (var v in values) {
38
+ // values is a set of strings for proper JSON parsing, but it's more efficient
39
+ // to interact with them as numbers
40
+ let id = parseInt(v);
41
+ let value = values[v] as string;
42
+ let source = ExtractSource.Text;
43
+ if (value.startsWith(Constant.Tilde)) {
44
+ source = ExtractSource.Javascript
45
+ } else if (value.startsWith(Constant.Bang)) {
46
+ source = ExtractSource.Hash
47
+ }
48
+ switch (source) {
49
+ case ExtractSource.Javascript:
50
+ let variable = value.slice(1);
51
+ variables[key][id] = parse(variable);
52
+ break;
53
+ case ExtractSource.Text:
54
+ selectors[key][id] = value;
55
+ break;
56
+ case ExtractSource.Hash:
57
+ let hash = value.slice(1);
58
+ hashes[key][id] = hash;
59
+ break;
60
+ }
61
+ }
62
+ }
63
+ catch(e) {
64
+ internal.log(Code.Config, Severity.Warning, e ? e.name : null);
65
+ }
66
+ }
67
+
68
+ export function clone(v: Syntax[]): Syntax[] {
69
+ return JSON.parse(JSON.stringify(v));
70
+ }
71
+
72
+ export function compute(): void {
73
+ try {
74
+ for (let v in variables) {
75
+ let key = parseInt(v);
76
+ if (validation[key] == Constant.Empty || document.querySelector(validation[key]))
77
+ {
78
+ let variableData = variables[key];
79
+ for (let v in variableData) {
80
+ let variableKey = parseInt(v);
81
+ let value = str(evaluate(clone(variableData[variableKey])));
82
+ if (value) {
83
+ update(key, variableKey, value);
84
+ }
85
+ }
86
+
87
+ let selectorData = selectors[key];
88
+ for (let s in selectorData) {
89
+ let shouldMask = false;
90
+ let selectorKey = parseInt(s);
91
+ let selector = selectorData[selectorKey];
92
+ if (selector.startsWith(Constant.At)){
93
+ shouldMask = true;
94
+ selector = selector.slice(1);
95
+ }
96
+ let nodes = document.querySelectorAll(selector) as NodeListOf<HTMLElement>;
97
+ if (nodes) {
98
+ let text = Array.from(nodes).map(e => {
99
+ if (e.tagName === "IMG") {
100
+ let img = e as HTMLImageElement;
101
+ return img.src || img.currentSrc || Constant.Empty;
102
+ }
103
+ return e.textContent;
104
+ }).join(Constant.Seperator);
105
+ update(key, selectorKey, (shouldMask ? hash(text).trim() : text).slice(0, Setting.ExtractLimit));
106
+ }
107
+ }
108
+
109
+ let hashData = hashes[key];
110
+ for (let h in hashData) {
111
+ let hashKey = parseInt(h);
112
+ let content = hashText(hashData[hashKey]).trim().slice(0, Setting.ExtractLimit);
113
+ update(key, hashKey, content);
114
+ }
115
+ }
116
+ }
117
+
118
+ if (keys.size > 0) {
119
+ encode(Event.Extract);
120
+ }
121
+ }
122
+ catch (e) { internal.log(Code.Selector, Severity.Warning, e ? e.name : null); }
123
+ }
124
+
125
+ export function reset(): void {
126
+ keys.clear();
127
+ }
128
+
129
+ export function update(key: number, subkey: number, value: string): void {
130
+ var update = false;
131
+ if (!(key in data)) {
132
+ data[key] = {};
133
+ update = true;
134
+ }
135
+
136
+ if (!isEmpty(hashes[key])
137
+ && (!(subkey in data[key]) || data[key][subkey] != value))
138
+ {
139
+ update = true;
140
+ }
141
+
142
+ data[key][subkey] = value;
143
+ if (update) {
144
+ keys.add(key);
145
+ }
146
+
147
+ return;
148
+ }
149
+
150
+ export function stop(): void {
151
+ reset();
152
+ }
153
+
154
+ function parse(variable: string): Syntax[] {
155
+ let syntax: Syntax[] = [];
156
+ let parts = variable.split(Constant.Dot);
157
+ while (parts.length > 0) {
158
+ let part = parts.shift();
159
+ let arrayStart = part.indexOf(Constant.ArrayStart);
160
+ let conditionStart = part.indexOf(Constant.ConditionStart);
161
+ let conditionEnd = part.indexOf(Constant.ConditionEnd);
162
+ syntax.push({
163
+ name : arrayStart > 0 ? part.slice(0, arrayStart) : (conditionStart > 0 ? part.slice(0, conditionStart) : part),
164
+ type : arrayStart > 0 ? Type.Array : (conditionStart > 0 ? Type.Object : Type.Simple),
165
+ condition : conditionStart > 0 ? part.slice(conditionStart + 1, conditionEnd) : null
166
+ });
167
+ }
168
+
169
+ return syntax;
170
+ }
171
+
172
+ // The function below takes in a variable name in following format: "a.b.c" and safely evaluates its value in javascript context
173
+ // For instance, for a.b.c, it will first check window["a"]. If it exists, it will recursively look at: window["a"]["b"] and finally,
174
+ // return the value for window["a"]["b"]["c"].
175
+ function evaluate(variable: Syntax[], base: Object = window): any {
176
+ if (variable.length == 0) { return base; }
177
+ let part = variable.shift();
178
+ let output;
179
+ if (base && base[part.name]) {
180
+ let obj = base[part.name];
181
+ if (part.type !== Type.Array && match(obj, part.condition)) {
182
+ output = evaluate(variable, obj);
183
+ }
184
+ else if (Array.isArray(obj)) {
185
+ let filtered = [];
186
+ for (var value of obj) {
187
+ if (match(value, part.condition)) {
188
+ let op = evaluate(variable, value)
189
+ if (op) { filtered.push(op); }
190
+ }
191
+ }
192
+ output = filtered;
193
+ }
194
+
195
+ return output;
196
+ }
197
+
198
+ return null;
199
+ }
200
+
201
+ function str(input: string): string {
202
+ // Automatically trim string to max of Setting.ExtractLimit to avoid fetching long strings
203
+ return input ? JSON.stringify(input).slice(0, Setting.ExtractLimit) : input;
204
+ }
205
+
206
+ function match(base: Object, condition: string): boolean {
207
+ if (condition) {
208
+ let prop = condition.split(":");
209
+ return prop.length > 1 ? base[prop[0]] == prop[1] : base[prop[0]]
210
+ }
211
+
212
+ return true;
213
+ }
214
+
215
+ function isEmpty(obj: Object): boolean {
216
+ return Object.keys(obj).length == 0;
217
+ }
@@ -0,0 +1,51 @@
1
+ import { Token } from "@clarity-types/data";
2
+
3
+ // Deferred upload buffer for GemX mode.
4
+ // Buffers ALL playback events until discoverTrigger conditions are met.
5
+ // Scheduling and trigger logic lives in layout/gemx-snapshot.ts.
6
+ let buffer: Token[][] = [];
7
+ let bufferBytes = 0;
8
+ let active = false;
9
+ let maxBytes = 0;
10
+
11
+ let onBytesLimit: (() => void) | null = null;
12
+
13
+ export function start(): void {
14
+ buffer = [];
15
+ bufferBytes = 0;
16
+ active = true;
17
+ }
18
+
19
+ export function config(bytesLimit?: number, onLimit?: () => void): boolean {
20
+ maxBytes = bytesLimit ?? 0;
21
+ onBytesLimit = onLimit ?? null;
22
+ return true;
23
+ }
24
+
25
+ export function stop(): void {
26
+ buffer = [];
27
+ bufferBytes = 0;
28
+ active = false;
29
+ maxBytes = 0;
30
+ onBytesLimit = null;
31
+ }
32
+
33
+ export function isActive(): boolean { return active; }
34
+
35
+ export function getBytes(): number { return bufferBytes; }
36
+
37
+ export function hold(tokens: Token[], byteLength: number): void {
38
+ buffer.push(tokens);
39
+ bufferBytes += byteLength;
40
+ if (maxBytes > 0 && bufferBytes >= maxBytes && onBytesLimit) {
41
+ onBytesLimit();
42
+ }
43
+ }
44
+
45
+ export function drain(): Token[][] {
46
+ active = false;
47
+ const pending = buffer;
48
+ buffer = [];
49
+ bufferBytes = 0;
50
+ return pending;
51
+ }
@@ -0,0 +1,35 @@
1
+ import { GemXConfig } from "@clarity-types/data";
2
+
3
+ const defaultConfig: GemXConfig = {
4
+ debug: false,
5
+ excludeClassNames: [],
6
+ ignoreMutationClassNames: [],
7
+ discoverTrigger: null,
8
+ };
9
+
10
+ let _config: GemXConfig = { ...defaultConfig };
11
+
12
+ export function start(): void {
13
+ _config = {
14
+ debug: new URLSearchParams(window.location.search).has("gemx_debug") || (window?.GemXHeatmap?.debug ?? false),
15
+ excludeClassNames: (window?.GemXHeatmap?.excludeClassNames ?? []),
16
+ ignoreMutationClassNames: (window?.GemXHeatmap?.ignoreMutationClassNames ?? []),
17
+ discoverTrigger: (window?.GemXHeatmap?.discoverTrigger ?? null),
18
+ };
19
+ }
20
+
21
+ export function stop(): void {
22
+ _config = { ...defaultConfig };
23
+ }
24
+
25
+ export function set(config: Partial<GemXConfig>): void {
26
+ _config = { ..._config, ...config };
27
+ }
28
+
29
+ export function get(): GemXConfig {
30
+ return _config;
31
+ }
32
+
33
+ export function isDebug(): boolean {
34
+ return _config.debug;
35
+ }
@@ -0,0 +1,57 @@
1
+ import measure from "@src/core/measure";
2
+ import * as baseline from "@src/data/baseline";
3
+ import * as consent from "@src/data/consent";
4
+ import * as envelope from "@src/data/envelope";
5
+ import * as dimension from "@src/data/dimension";
6
+ import * as metadata from "@src/data/metadata";
7
+ import { Module } from "@clarity-types/core";
8
+ import config from "@src/core/config";
9
+ import * as metric from "@src/data/metric";
10
+ import * as ping from "@src/data/ping";
11
+ import * as limit from "@src/data/limit";
12
+ import * as summary from "@src/data/summary";
13
+ import * as upgrade from "@src/data/upgrade";
14
+ import * as upload from "@src/data/upload";
15
+ import * as variable from "@src/data/variable";
16
+ import * as extract from "@src/data/extract";
17
+ import * as cookie from "@src/data/cookie";
18
+ import * as gemx from "@src/data/gemx";
19
+ import * as gemxUpload from "@src/data/gemx-upload";
20
+ export { event } from "@src/data/custom";
21
+ export { consent, consentv2, metadata } from "@src/data/metadata";
22
+ export { upgrade } from "@src/data/upgrade";
23
+ export { set, identify } from "@src/data/variable";
24
+ export { signal } from "@src/data/signal";
25
+ export { max as maxMetric } from "@src/data/metric";
26
+ export { log as dlog } from "@src/data/dimension";
27
+
28
+ const modules: Module[] = [gemx, gemxUpload, baseline, dimension, variable, limit, summary, cookie, consent, metadata, envelope, upload, ping, upgrade, extract];
29
+
30
+ export function start(): void {
31
+ // Metric needs to be initialized before we can start measuring. so metric is not wrapped in measure
32
+ metric.start();
33
+ modules.forEach(x => measure(x.start)());
34
+ }
35
+
36
+ export function stop(): void {
37
+ // Stop modules in the reverse order of their initialization
38
+ // The ordering below should respect inter-module dependency.
39
+ // E.g. if upgrade depends on upload, then upgrade needs to end before upload.
40
+ // Similarly, if upload depends on metadata, upload needs to end before metadata.
41
+ modules.slice().reverse().forEach(x => measure(x.stop)());
42
+ metric.stop();
43
+ }
44
+
45
+ export function compute(): void {
46
+ baseline.compute();
47
+ limit.compute();
48
+ consent.compute();
49
+
50
+ if (config.mode !== "gemx") {
51
+ variable.compute();
52
+ dimension.compute();
53
+ metric.compute();
54
+ summary.compute();
55
+ extract.compute();
56
+ }
57
+ }
@@ -0,0 +1,44 @@
1
+ import { Check, Event, LimitData, Setting } from "@clarity-types/data";
2
+ import * as clarity from "@src/clarity";
3
+ import { time } from "@src/core/time";
4
+ import * as envelope from "@src/data/envelope";
5
+ import * as metadata from "@src/data/metadata";
6
+ import encode from "./encode";
7
+
8
+ export let data: LimitData;
9
+
10
+ export function start(): void {
11
+ data = { check: Check.None };
12
+ }
13
+
14
+ export function check(bytes: number): void {
15
+ if (data.check === Check.None) {
16
+ let reason = data.check;
17
+ reason = envelope.data.sequence >= Setting.PayloadLimit ? Check.Payload : reason;
18
+ reason = envelope.data.pageNum >= Setting.PageLimit ? Check.Page : reason;
19
+ reason = time() > Setting.ShutdownLimit ? Check.Shutdown : reason;
20
+ reason = bytes > Setting.PlaybackBytesLimit ? Check.Shutdown : reason;
21
+ if (reason !== data.check) {
22
+ trigger(reason);
23
+ }
24
+ }
25
+ }
26
+
27
+ export function trigger(reason: Check): void {
28
+ data.check = reason;
29
+ // limit the dimensions we collect, but we don't need to stop Clarity entirely if we hit the limit
30
+ if (reason !== Check.Collection) {
31
+ metadata.clear();
32
+ clarity.stop();
33
+ }
34
+ }
35
+
36
+ export function compute(): void {
37
+ if (data.check !== Check.None) {
38
+ encode(Event.Limit);
39
+ }
40
+ }
41
+
42
+ export function stop(): void {
43
+ data = null;
44
+ }