clarity-js 0.8.42 → 0.8.44

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 (116) hide show
  1. package/README.md +26 -26
  2. package/build/clarity.extended.js +1 -1
  3. package/build/clarity.insight.js +1 -1
  4. package/build/clarity.js +6101 -6071
  5. package/build/clarity.min.js +1 -1
  6. package/build/clarity.module.js +6101 -6071
  7. package/build/clarity.performance.js +1 -1
  8. package/package.json +70 -70
  9. package/rollup.config.ts +161 -161
  10. package/src/clarity.ts +65 -65
  11. package/src/core/api.ts +8 -8
  12. package/src/core/config.ts +29 -29
  13. package/src/core/copy.ts +3 -3
  14. package/src/core/dynamic.ts +13 -7
  15. package/src/core/event.ts +53 -53
  16. package/src/core/hash.ts +19 -19
  17. package/src/core/history.ts +71 -71
  18. package/src/core/index.ts +81 -81
  19. package/src/core/measure.ts +19 -19
  20. package/src/core/report.ts +28 -28
  21. package/src/core/scrub.ts +204 -202
  22. package/src/core/task.ts +181 -181
  23. package/src/core/throttle.ts +46 -46
  24. package/src/core/time.ts +26 -26
  25. package/src/core/timeout.ts +10 -10
  26. package/src/core/version.ts +2 -2
  27. package/src/data/baseline.ts +162 -162
  28. package/src/data/compress.ts +31 -31
  29. package/src/data/consent.ts +77 -77
  30. package/src/data/cookie.ts +90 -0
  31. package/src/data/custom.ts +23 -23
  32. package/src/data/dimension.ts +53 -53
  33. package/src/data/encode.ts +155 -155
  34. package/src/data/envelope.ts +53 -53
  35. package/src/data/extract.ts +211 -211
  36. package/src/data/index.ts +51 -50
  37. package/src/data/limit.ts +44 -44
  38. package/src/data/metadata.ts +321 -408
  39. package/src/data/metric.ts +51 -51
  40. package/src/data/ping.ts +36 -36
  41. package/src/data/signal.ts +30 -30
  42. package/src/data/summary.ts +34 -34
  43. package/src/data/token.ts +39 -39
  44. package/src/data/upgrade.ts +44 -44
  45. package/src/data/upload.ts +333 -333
  46. package/src/data/util.ts +18 -0
  47. package/src/data/variable.ts +83 -83
  48. package/src/diagnostic/encode.ts +40 -40
  49. package/src/diagnostic/fraud.ts +36 -36
  50. package/src/diagnostic/index.ts +13 -13
  51. package/src/diagnostic/internal.ts +28 -28
  52. package/src/diagnostic/script.ts +35 -35
  53. package/src/dynamic/agent/blank.ts +2 -2
  54. package/src/dynamic/agent/crisp.ts +40 -40
  55. package/src/dynamic/agent/encode.ts +25 -25
  56. package/src/dynamic/agent/index.ts +8 -8
  57. package/src/dynamic/agent/livechat.ts +58 -58
  58. package/src/dynamic/agent/tidio.ts +44 -44
  59. package/src/global.ts +6 -6
  60. package/src/index.ts +9 -9
  61. package/src/insight/blank.ts +14 -14
  62. package/src/insight/encode.ts +60 -60
  63. package/src/insight/snapshot.ts +114 -114
  64. package/src/interaction/change.ts +38 -38
  65. package/src/interaction/click.ts +173 -173
  66. package/src/interaction/clipboard.ts +32 -32
  67. package/src/interaction/encode.ts +210 -210
  68. package/src/interaction/index.ts +60 -60
  69. package/src/interaction/input.ts +57 -57
  70. package/src/interaction/pointer.ts +137 -137
  71. package/src/interaction/resize.ts +50 -50
  72. package/src/interaction/scroll.ts +129 -129
  73. package/src/interaction/selection.ts +66 -66
  74. package/src/interaction/submit.ts +30 -30
  75. package/src/interaction/timeline.ts +69 -69
  76. package/src/interaction/unload.ts +26 -26
  77. package/src/interaction/visibility.ts +27 -27
  78. package/src/layout/animation.ts +133 -133
  79. package/src/layout/custom.ts +42 -42
  80. package/src/layout/discover.ts +31 -31
  81. package/src/layout/document.ts +46 -46
  82. package/src/layout/dom.ts +439 -439
  83. package/src/layout/encode.ts +154 -154
  84. package/src/layout/index.ts +42 -42
  85. package/src/layout/mutation.ts +411 -411
  86. package/src/layout/node.ts +294 -294
  87. package/src/layout/offset.ts +19 -19
  88. package/src/layout/region.ts +151 -151
  89. package/src/layout/schema.ts +63 -63
  90. package/src/layout/selector.ts +82 -82
  91. package/src/layout/style.ts +159 -159
  92. package/src/layout/target.ts +32 -32
  93. package/src/layout/traverse.ts +27 -27
  94. package/src/performance/blank.ts +9 -9
  95. package/src/performance/encode.ts +31 -31
  96. package/src/performance/index.ts +12 -12
  97. package/src/performance/interaction.ts +125 -125
  98. package/src/performance/navigation.ts +31 -31
  99. package/src/performance/observer.ts +112 -112
  100. package/src/queue.ts +33 -33
  101. package/test/core.test.ts +139 -139
  102. package/test/helper.ts +162 -162
  103. package/test/html/core.html +27 -27
  104. package/test/stub.test.ts +7 -7
  105. package/test/tsconfig.test.json +5 -5
  106. package/tsconfig.json +21 -21
  107. package/tslint.json +32 -32
  108. package/types/agent.d.ts +39 -39
  109. package/types/core.d.ts +150 -150
  110. package/types/data.d.ts +572 -571
  111. package/types/diagnostic.d.ts +24 -24
  112. package/types/global.d.ts +30 -30
  113. package/types/index.d.ts +40 -40
  114. package/types/interaction.d.ts +177 -177
  115. package/types/layout.d.ts +276 -276
  116. package/types/performance.d.ts +31 -31
@@ -1,162 +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
- }
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
+ }
@@ -1,31 +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
- }
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
+ }
@@ -1,78 +1,78 @@
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
- const analytics_storage = ics.getConsentState(Constant.AnalyticsStorage);
32
- const ad_storage = ics.getConsentState(Constant.AdStorage);
33
- const consentState = getConsentState({ ad_Storage: ad_storage, analytics_Storage: analytics_storage });
34
- consentv2(consentState, ConsentSource.GCM);
35
- }
36
-
37
- function getConsentState(googleConsent: GCMConsentState): ConsentState {
38
- const consentState: ConsentState = {
39
- ad_Storage: googleConsent.ad_Storage === GCMConsent.Granted ? Constant.Granted : Constant.Denied,
40
- analytics_Storage: googleConsent.analytics_Storage === GCMConsent.Granted ? Constant.Granted : Constant.Denied,
41
- };
42
-
43
- return consentState;
44
- }
45
-
46
-
47
- export function config(consent: ConsentData): void {
48
- trackConsent(consent.analytics_Storage ? ConsentType.Implicit : ConsentType.None);
49
- data = consent;
50
- }
51
-
52
- // When we get consent signal as false, we restart the service and track config as false.
53
- export function consent(): void {
54
- trackConsent(ConsentType.General);
55
- }
56
-
57
- function trackConsent(consent: ConsentType): void {
58
- dimension.log(Dimension.Consent, consent.toString());
59
- }
60
-
61
- export function trackConsentv2(consent: ConsentData): void {
62
- data = consent;
63
- encode(Event.Consent);
64
- }
65
-
66
- // Compute function is called every upload, but we only want to send consent data once.
67
- export function compute(): void {
68
- if (updateConsent) {
69
- encode(Event.Consent);
70
- updateConsent = false;
71
- if (!coreConfig.track) {
72
- const ics = window.google_tag_data?.ics;
73
- if (ics?.usedUpdate) {
74
- processConsent();
75
- }
76
- }
77
- }
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
+ const analytics_storage = ics.getConsentState(Constant.AnalyticsStorage);
32
+ const ad_storage = ics.getConsentState(Constant.AdStorage);
33
+ const consentState = getConsentState({ ad_Storage: ad_storage, analytics_Storage: analytics_storage });
34
+ consentv2(consentState, ConsentSource.GCM);
35
+ }
36
+
37
+ function getConsentState(googleConsent: GCMConsentState): ConsentState {
38
+ const consentState: ConsentState = {
39
+ ad_Storage: googleConsent.ad_Storage === GCMConsent.Granted ? Constant.Granted : Constant.Denied,
40
+ analytics_Storage: googleConsent.analytics_Storage === GCMConsent.Granted ? Constant.Granted : Constant.Denied,
41
+ };
42
+
43
+ return consentState;
44
+ }
45
+
46
+
47
+ export function config(consent: ConsentData): void {
48
+ trackConsent(consent.analytics_Storage ? ConsentType.Implicit : ConsentType.None);
49
+ data = consent;
50
+ }
51
+
52
+ // When we get consent signal as false, we restart the service and track config as false.
53
+ export function consent(): void {
54
+ trackConsent(ConsentType.General);
55
+ }
56
+
57
+ function trackConsent(consent: ConsentType): void {
58
+ dimension.log(Dimension.Consent, consent.toString());
59
+ }
60
+
61
+ export function trackConsentv2(consent: ConsentData): void {
62
+ data = consent;
63
+ encode(Event.Consent);
64
+ }
65
+
66
+ // Compute function is called every upload, but we only want to send consent data once.
67
+ export function compute(): void {
68
+ if (updateConsent) {
69
+ encode(Event.Consent);
70
+ updateConsent = false;
71
+ if (!coreConfig.track) {
72
+ const ics = window.google_tag_data?.ics;
73
+ if (ics?.usedUpdate) {
74
+ processConsent();
75
+ }
76
+ }
77
+ }
78
78
  }
@@ -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
+ }