@coframe/sdk 1.1.53 → 1.1.54

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.
@@ -1164,6 +1164,11 @@ const SDK_VERSION = '2.10.0';
1164
1164
  const queuedEvents = [];
1165
1165
  const queuedMonitoringEvents = [];
1166
1166
  const queuedSessions = [];
1167
+ const heatmapContentKeys = new WeakMap();
1168
+ let beforeFlush;
1169
+ function setBeforeFlush(hook) {
1170
+ beforeFlush = hook;
1171
+ }
1167
1172
  let ingestUrl;
1168
1173
  const COMPILED_INGEST_BASE = "https://ingest.app.coframe.com/ingest/v2";
1169
1174
  function configureIngestUrl(url) {
@@ -1195,6 +1200,18 @@ function chunkEventsByByteLimit(events) {
1195
1200
  }
1196
1201
  return chunks;
1197
1202
  }
1203
+ function createMonitoringPayload(coframe, monitoringType, metadata) {
1204
+ return {
1205
+ event_id: generateUUID(),
1206
+ monitoring_type: monitoringType,
1207
+ local_time: new Date().toISOString(),
1208
+ project_id: coframe.projectId,
1209
+ session_id: coframe.sessionId,
1210
+ url: location.href,
1211
+ org_id: coframe.orgId,
1212
+ metadata,
1213
+ };
1214
+ }
1198
1215
  function queueMonitoringEvent(coframe, data) {
1199
1216
  queuedMonitoringEvents.push(data);
1200
1217
  if (queuedMonitoringEvents.length >= coframe.batchSize) {
@@ -1246,6 +1263,7 @@ function flushQueuedEvents(projectId) {
1246
1263
  if (!shouldSendEvents()) {
1247
1264
  return;
1248
1265
  }
1266
+ beforeFlush === null || beforeFlush === void 0 ? void 0 : beforeFlush();
1249
1267
  if (queuedEvents.length > 0) {
1250
1268
  for (const chunk of chunkEventsByByteLimit(queuedEvents)) {
1251
1269
  sendEventToBackend(projectId, chunk, 'batched_events');
@@ -1290,6 +1308,8 @@ function queueEvent(coframe, sessionInfo, heatmap) {
1290
1308
  screen_size: { width: window.innerWidth, height: window.innerHeight },
1291
1309
  }
1292
1310
  : { metadata, ...heatmap === null || heatmap === void 0 ? void 0 : heatmap.content };
1311
+ if (heatmap === null || heatmap === void 0 ? void 0 : heatmap.content)
1312
+ heatmapContentKeys.set(data, Object.keys(heatmap.content));
1293
1313
  queueEventForBatchedSending(coframe, data);
1294
1314
  }
1295
1315
  function queueVariantViewEvent(coframe, { projectId, sessionId, orgId, userToken, experimentId, variantId, }) {
@@ -3746,6 +3766,241 @@ function notifyEventsFlushed() {
3746
3766
  }
3747
3767
  }
3748
3768
 
3769
+ function isRecord(value) {
3770
+ return !!value && typeof value === 'object' && !Array.isArray(value);
3771
+ }
3772
+
3773
+ const OPT_IN_COUNTRIES = new Set(('AT BE BG HR CY CZ DK EE FI FR DE GR HU IE IT LV LT LU MT NL PL PT RO SK SI ES SE ' +
3774
+ 'AX GF GP MF MQ RE YT ' +
3775
+ 'IS LI NO ' +
3776
+ 'GB GI GG IM JE ' +
3777
+ 'CH').split(' '));
3778
+ function collectsWithoutChoice(model, country) {
3779
+ if (model === 'opt_out')
3780
+ return true;
3781
+ if (model === 'opt_in')
3782
+ return false;
3783
+ const code = (country || '').trim().toUpperCase();
3784
+ if (!/^[A-Z]{2}$/.test(code) || code === 'XX' || code === 'T1')
3785
+ return false;
3786
+ return !OPT_IN_COUNTRIES.has(code);
3787
+ }
3788
+ function readRawCookie(name) {
3789
+ const prefix = `${name}=`;
3790
+ const cookies = document.cookie.split(';');
3791
+ for (let i = 0; i < cookies.length; i++) {
3792
+ const cookie = cookies[i].trim();
3793
+ if (cookie.indexOf(prefix) === 0)
3794
+ return cookie.slice(prefix.length);
3795
+ }
3796
+ return undefined;
3797
+ }
3798
+ function readCookie(name) {
3799
+ const raw = readRawCookie(name);
3800
+ return raw === undefined ? undefined : decodeURIComponent(raw);
3801
+ }
3802
+ function readClientCommand() {
3803
+ try {
3804
+ const cmd = findLastCmd((c) => typeof c.cookiesEnabled === 'boolean');
3805
+ if (!cmd)
3806
+ return undefined;
3807
+ return cmd.cookiesEnabled === true ? 'granted' : 'denied';
3808
+ }
3809
+ catch (_a) {
3810
+ return undefined;
3811
+ }
3812
+ }
3813
+ function readOneTrustCookie() {
3814
+ const cookie = readRawCookie('OptanonConsent');
3815
+ if (cookie === undefined)
3816
+ return undefined;
3817
+ const fields = cookie.split('&');
3818
+ let groups = '';
3819
+ for (let i = 0; i < fields.length; i++) {
3820
+ if (fields[i].indexOf('groups=') === 0)
3821
+ groups = decodeURIComponent(fields[i].slice('groups='.length));
3822
+ }
3823
+ if (groups.split(',').indexOf('C0002:1') !== -1)
3824
+ return 'granted';
3825
+ return readCookie('OptanonAlertBoxClosed') !== undefined
3826
+ ? 'denied'
3827
+ : 'pending';
3828
+ }
3829
+ function readOneTrust() {
3830
+ var _a;
3831
+ try {
3832
+ const { OnetrustActiveGroups: groups, OneTrust } = window;
3833
+ if (typeof groups !== 'string' && OneTrust === undefined) {
3834
+ if (window.Cookiebot !== undefined)
3835
+ return undefined;
3836
+ return readOneTrustCookie();
3837
+ }
3838
+ if (typeof groups === 'string' && groups.split(',').indexOf('C0002') !== -1)
3839
+ return 'granted';
3840
+ return ((_a = OneTrust === null || OneTrust === void 0 ? void 0 : OneTrust.IsAlertBoxClosed) === null || _a === void 0 ? void 0 : _a.call(OneTrust)) === true ? 'denied' : 'pending';
3841
+ }
3842
+ catch (_b) {
3843
+ return undefined;
3844
+ }
3845
+ }
3846
+ function readCookiebotCookie() {
3847
+ const cookie = readCookie('CookieConsent');
3848
+ if (cookie === undefined)
3849
+ return undefined;
3850
+ if (cookie === '-1')
3851
+ return 'granted';
3852
+ const statistics = /[{,]statistics:(true|false)[,}]/.exec(cookie);
3853
+ if (!statistics)
3854
+ return undefined;
3855
+ return statistics[1] === 'true' ? 'granted' : 'denied';
3856
+ }
3857
+ function readCookiebot() {
3858
+ var _a;
3859
+ try {
3860
+ const { Cookiebot, OnetrustActiveGroups, OneTrust } = window;
3861
+ if (Cookiebot === undefined) {
3862
+ if (typeof OnetrustActiveGroups === 'string' || OneTrust !== undefined)
3863
+ return undefined;
3864
+ return readCookiebotCookie();
3865
+ }
3866
+ if (((_a = Cookiebot.consent) === null || _a === void 0 ? void 0 : _a.statistics) === true)
3867
+ return 'granted';
3868
+ return Cookiebot.hasResponse === true ? 'denied' : 'pending';
3869
+ }
3870
+ catch (_b) {
3871
+ return undefined;
3872
+ }
3873
+ }
3874
+ function readTrueVault() {
3875
+ try {
3876
+ const cookie = readCookie('polaris_consent_settings');
3877
+ if (cookie === undefined)
3878
+ return undefined;
3879
+ const parsed = JSON.parse(cookie);
3880
+ if (parsed.analyticsPermitted === false || parsed.notOptedOut === false) {
3881
+ return 'denied';
3882
+ }
3883
+ if (parsed.implicit === false && parsed.analyticsPermitted === true)
3884
+ return 'granted';
3885
+ return 'pending';
3886
+ }
3887
+ catch (_a) {
3888
+ return undefined;
3889
+ }
3890
+ }
3891
+ function readUsPrivacy() {
3892
+ try {
3893
+ const cookie = readCookie('us_privacy');
3894
+ if (cookie !== undefined &&
3895
+ /^1[YN-]{3}$/i.test(cookie) &&
3896
+ (cookie[2] === 'Y' || cookie[2] === 'y')) {
3897
+ return 'denied';
3898
+ }
3899
+ return undefined;
3900
+ }
3901
+ catch (_a) {
3902
+ return undefined;
3903
+ }
3904
+ }
3905
+ function readConsentMode() {
3906
+ var _a;
3907
+ try {
3908
+ const { dataLayer } = window;
3909
+ if (!Array.isArray(dataLayer))
3910
+ return undefined;
3911
+ let update;
3912
+ let defaultGranted = false;
3913
+ let defaultDenied = false;
3914
+ for (let i = 0; i < dataLayer.length; i++) {
3915
+ const entry = dataLayer[i];
3916
+ if (entry === null ||
3917
+ typeof entry !== 'object' ||
3918
+ entry[0] !== 'consent' ||
3919
+ (entry[1] !== 'default' && entry[1] !== 'update')) {
3920
+ continue;
3921
+ }
3922
+ const storage = (_a = entry[2]) === null || _a === void 0 ? void 0 : _a.analytics_storage;
3923
+ if (storage !== 'granted' && storage !== 'denied')
3924
+ continue;
3925
+ if (entry[1] === 'update')
3926
+ update = storage;
3927
+ else if (storage === 'denied')
3928
+ defaultDenied = true;
3929
+ else
3930
+ defaultGranted = true;
3931
+ }
3932
+ if (update !== undefined)
3933
+ return update;
3934
+ if (defaultDenied)
3935
+ return 'pending';
3936
+ return defaultGranted ? 'granted' : undefined;
3937
+ }
3938
+ catch (_b) {
3939
+ return undefined;
3940
+ }
3941
+ }
3942
+ const CONSENT_WAIT_MS = 5000;
3943
+ const CONSENT_DETECT_WAIT_MS = 10000;
3944
+ const EXPLICIT_CONSENT_WAIT_MS = 60000;
3945
+ function readVisitorConsent(optOut) {
3946
+ try {
3947
+ if (navigator
3948
+ .globalPrivacyControl === true) {
3949
+ return { state: 'denied', reason: 'gpc' };
3950
+ }
3951
+ const shopify = window.Shopify;
3952
+ if (shopify === undefined) {
3953
+ if (readUsPrivacy() === 'denied')
3954
+ return { state: 'denied', reason: 'consent_denied' };
3955
+ if (isWaitingForCookies()) {
3956
+ const signal = readClientCommand();
3957
+ if (signal === 'granted')
3958
+ return { state: 'allowed', source: 'client' };
3959
+ if (signal === 'denied')
3960
+ return { state: 'denied', reason: 'consent_denied' };
3961
+ return { state: 'pending', detected: true };
3962
+ }
3963
+ const readers = [
3964
+ [readClientCommand, 'client'],
3965
+ [readOneTrust, 'onetrust'],
3966
+ [readCookiebot, 'cookiebot'],
3967
+ [readTrueVault, 'truevault'],
3968
+ [readConsentMode, 'consent_mode'],
3969
+ ];
3970
+ let granted;
3971
+ for (let i = 0; i < readers.length; i++) {
3972
+ const [read, source] = readers[i];
3973
+ const signal = read();
3974
+ if (signal === 'granted') {
3975
+ if (source === 'client')
3976
+ return { state: 'allowed', source };
3977
+ granted = granted || source;
3978
+ }
3979
+ else if (signal === 'denied')
3980
+ return { state: 'denied', reason: 'consent_denied' };
3981
+ else if (signal === 'pending' && !optOut && !granted)
3982
+ return { state: 'pending', detected: true };
3983
+ }
3984
+ if (granted)
3985
+ return { state: 'allowed', source: granted };
3986
+ if (optOut)
3987
+ return { state: 'allowed', source: 'opt_out' };
3988
+ return { state: 'pending', detected: isWaitingForCookies() };
3989
+ }
3990
+ const customerPrivacy = shopify.customerPrivacy;
3991
+ const analyticsProcessingAllowed = customerPrivacy === null || customerPrivacy === void 0 ? void 0 : customerPrivacy.analyticsProcessingAllowed;
3992
+ if (typeof analyticsProcessingAllowed === 'function') {
3993
+ return analyticsProcessingAllowed.call(customerPrivacy) === true
3994
+ ? { state: 'allowed', source: 'shopify' }
3995
+ : { state: 'denied', reason: 'consent_denied' };
3996
+ }
3997
+ return { state: 'pending' };
3998
+ }
3999
+ catch (_a) {
4000
+ return { state: 'denied', reason: 'consent_unavailable' };
4001
+ }
4002
+ }
4003
+
3749
4004
  const DEFAULT_BLOCK_SELECTOR = '[data-cf-block],.cf-block';
3750
4005
  const INTERACTIVE_SELECTOR = 'a,button,input,select,textarea,label,summary,[href],[onclick],[contenteditable],[tabindex]:not([tabindex="-1"]),[role=button],[role=link],[role=tab],[role=checkbox],[role=menuitem]';
3751
4006
  const HOLD_MS = 1000;
@@ -3754,6 +4009,7 @@ const RAGE_RADIUS_PX = 30;
3754
4009
  const RAGE_CLICKS = 3;
3755
4010
  let enabled = false;
3756
4011
  let allPages = false;
4012
+ let optOut = false;
3757
4013
  let coframe = null;
3758
4014
  let blockSelector = DEFAULT_BLOCK_SELECTOR;
3759
4015
  const held = [];
@@ -3764,13 +4020,26 @@ let pageView = '';
3764
4020
  let maxDepth = 0;
3765
4021
  let engagedMs = 0;
3766
4022
  let visibleSince = null;
4023
+ let measuring = false;
4024
+ let consentCmdSubscribed = false;
4025
+ const CONSENT_EVENTS = [
4026
+ 'OneTrustGroupsUpdated',
4027
+ 'CookiebotOnAccept',
4028
+ 'CookiebotOnDecline',
4029
+ ];
3767
4030
  let navigated = false;
4031
+ let consentView = '';
4032
+ let consentSince = 0;
4033
+ let consentDone = false;
4034
+ let consentTimer;
3768
4035
  function meta(name) {
3769
4036
  const tag = document.querySelector(`meta[name="coframe:${name}"]`);
3770
4037
  return (tag === null || tag === void 0 ? void 0 : tag.getAttribute('content')) || undefined;
3771
4038
  }
3772
4039
  function heatmapActive() {
3773
- return enabled && (allPages || !!meta('page'));
4040
+ return (enabled &&
4041
+ (allPages || !!meta('page')) &&
4042
+ readVisitorConsent(optOut).state === 'allowed');
3774
4043
  }
3775
4044
  function pageFields(withHeight) {
3776
4045
  const fields = {
@@ -3797,6 +4066,84 @@ function restartCounters(depth) {
3797
4066
  engagedMs = 0;
3798
4067
  visibleSince = document.hidden ? null : Date.now();
3799
4068
  }
4069
+ function syncConsent() {
4070
+ const consent = readVisitorConsent(optOut);
4071
+ reportConsent(consent);
4072
+ const allowed = consent.state === 'allowed';
4073
+ if (allowed && !measuring)
4074
+ restartCounters(viewportBottom());
4075
+ else if (!allowed && measuring) {
4076
+ stripQueued();
4077
+ maxDepth = 0;
4078
+ engagedMs = 0;
4079
+ visibleSince = null;
4080
+ }
4081
+ measuring = allowed;
4082
+ return allowed;
4083
+ }
4084
+ function reportConsent(consent) {
4085
+ if (!coframe)
4086
+ return;
4087
+ if (consentView !== coframe.sessionId) {
4088
+ clearTimeout(consentTimer);
4089
+ consentTimer = undefined;
4090
+ consentView = coframe.sessionId;
4091
+ consentSince = Date.now();
4092
+ consentDone = !allPages && !meta('page');
4093
+ }
4094
+ if (consentDone)
4095
+ return;
4096
+ let type = 'heatmap_skipped';
4097
+ let metadata;
4098
+ if (consent.state === 'allowed') {
4099
+ type = 'heatmap_consent_allowed';
4100
+ metadata = { source: consent.source };
4101
+ }
4102
+ else if (consent.state === 'denied') {
4103
+ metadata = { reason: consent.reason };
4104
+ }
4105
+ else {
4106
+ const shopify = window.Shopify !== undefined;
4107
+ const left = consentSince +
4108
+ (shopify
4109
+ ? CONSENT_WAIT_MS
4110
+ : consent.detected
4111
+ ? EXPLICIT_CONSENT_WAIT_MS
4112
+ : CONSENT_DETECT_WAIT_MS) -
4113
+ Date.now();
4114
+ if (left > 0) {
4115
+ if (!consentTimer)
4116
+ consentTimer = setTimeout(() => {
4117
+ consentTimer = undefined;
4118
+ syncConsent();
4119
+ }, left);
4120
+ return;
4121
+ }
4122
+ metadata = {
4123
+ reason: shopify || consent.detected
4124
+ ? 'consent_unavailable'
4125
+ : 'consent_not_configured',
4126
+ };
4127
+ }
4128
+ consentDone = true;
4129
+ clearTimeout(consentTimer);
4130
+ consentTimer = undefined;
4131
+ try {
4132
+ queueMonitoringEvent(coframe, createMonitoringPayload(coframe, type, metadata));
4133
+ }
4134
+ catch (_a) { }
4135
+ }
4136
+ function syncConsentBeforeFlush() {
4137
+ if (!syncConsent())
4138
+ stripQueued();
4139
+ }
4140
+ function heatmapPageView() {
4141
+ if (enabled)
4142
+ syncConsent();
4143
+ }
4144
+ function syncConsentSoon() {
4145
+ setTimeout(syncConsent, 0);
4146
+ }
3800
4147
  function syncPageView() {
3801
4148
  if (!coframe || coframe.sessionId === pageView)
3802
4149
  return;
@@ -3809,9 +4156,15 @@ function syncPageView() {
3809
4156
  function onScroll() {
3810
4157
  markActivity();
3811
4158
  syncPageView();
3812
- maxDepth = Math.max(maxDepth, viewportBottom());
4159
+ if (syncConsent())
4160
+ maxDepth = Math.max(maxDepth, viewportBottom());
3813
4161
  }
3814
4162
  function onVisibility() {
4163
+ if (!syncConsent()) {
4164
+ if (document.hidden)
4165
+ releaseHeldClicks();
4166
+ return;
4167
+ }
3815
4168
  if (document.hidden) {
3816
4169
  if (visibleSince !== null)
3817
4170
  engagedMs += Date.now() - visibleSince;
@@ -3834,15 +4187,18 @@ function readBlockSelector(selector) {
3834
4187
  }
3835
4188
  }
3836
4189
  function startHeatmaps(target, project) {
3837
- var _a, _b;
4190
+ var _a, _b, _c;
3838
4191
  const settings = (_a = project === null || project === void 0 ? void 0 : project.settings) === null || _a === void 0 ? void 0 : _a.heatmaps;
3839
4192
  if (enabled || !(settings === null || settings === void 0 ? void 0 : settings.enabled))
3840
4193
  return;
3841
4194
  enabled = true;
3842
4195
  allPages = !!settings.all_pages;
3843
- blockSelector = readBlockSelector((_b = project.settings.session_replay) === null || _b === void 0 ? void 0 : _b.block_selector);
4196
+ optOut = collectsWithoutChoice(settings.consent_model, (_b = target.personalizationHeaders) === null || _b === void 0 ? void 0 : _b['x-viewer-country']);
4197
+ blockSelector = readBlockSelector((_c = project.settings.session_replay) === null || _c === void 0 ? void 0 : _c.block_selector);
3844
4198
  coframe = target;
3845
4199
  syncPageView();
4200
+ syncConsent();
4201
+ setBeforeFlush(syncConsentBeforeFlush);
3846
4202
  document.addEventListener('scroll', onScroll, {
3847
4203
  capture: true,
3848
4204
  passive: true,
@@ -3850,11 +4206,19 @@ function startHeatmaps(target, project) {
3850
4206
  document.addEventListener('visibilitychange', onVisibility);
3851
4207
  window.addEventListener('pagehide', releaseHeldClicks);
3852
4208
  window.addEventListener('beforeunload', releaseHeldClicks);
4209
+ document.addEventListener('click', syncConsentSoon, true);
4210
+ for (const name of CONSENT_EVENTS)
4211
+ window.addEventListener(name, syncConsentSoon);
4212
+ if (!consentCmdSubscribed) {
4213
+ consentCmdSubscribed = true;
4214
+ subscribeToCmd((cmd) => typeof cmd.cookiesEnabled === 'boolean', () => enabled && syncConsentSoon());
4215
+ }
3853
4216
  }
3854
4217
  function leaveFields() {
3855
4218
  if (!heatmapActive())
3856
4219
  return undefined;
3857
4220
  syncPageView();
4221
+ syncConsent();
3858
4222
  if (navigated)
3859
4223
  return undefined;
3860
4224
  return {
@@ -3870,6 +4234,8 @@ function pageLeaveFields() {
3870
4234
  return undefined;
3871
4235
  const fields = leaveFields();
3872
4236
  restartCounters(0);
4237
+ if (!measuring)
4238
+ visibleSince = null;
3873
4239
  navigated = true;
3874
4240
  return fields;
3875
4241
  }
@@ -3884,6 +4250,37 @@ function watchMutations() {
3884
4250
  subtree: true,
3885
4251
  });
3886
4252
  }
4253
+ function stripFields(event) {
4254
+ var _a;
4255
+ if (!isRecord(event) || !isRecord(event.content))
4256
+ return;
4257
+ const content = event.content;
4258
+ for (const key of (_a = heatmapContentKeys.get(event)) !== null && _a !== void 0 ? _a : [])
4259
+ delete content[key];
4260
+ heatmapContentKeys.delete(event);
4261
+ const metadata = content.metadata;
4262
+ if (!isRecord(metadata) || !('hm' in metadata))
4263
+ return;
4264
+ delete metadata.hm;
4265
+ if (Object.keys(metadata).length === 0)
4266
+ delete content.metadata;
4267
+ }
4268
+ function stripQueued() {
4269
+ for (let i = queuedEvents.length - 1; i >= 0; i--) {
4270
+ const event = queuedEvents[i];
4271
+ if (isRecord(event) && event.type === 'page_leave')
4272
+ queuedEvents.splice(i, 1);
4273
+ else
4274
+ stripFields(event);
4275
+ }
4276
+ }
4277
+ function queueHeld(click) {
4278
+ if (!coframe)
4279
+ return;
4280
+ if (readVisitorConsent(optOut).state !== 'allowed')
4281
+ stripFields(click.data);
4282
+ queueEventForBatchedSending(coframe, click.data);
4283
+ }
3887
4284
  function settle(click) {
3888
4285
  const index = held.indexOf(click);
3889
4286
  if (index < 0)
@@ -3896,8 +4293,7 @@ function settle(click) {
3896
4293
  location.href === click.href;
3897
4294
  click.hm.kind = dead ? 'dead' : 'click';
3898
4295
  }
3899
- if (coframe)
3900
- queueEventForBatchedSending(coframe, click.data);
4296
+ queueHeld(click);
3901
4297
  if (observer && !held.some((h) => h.candidate)) {
3902
4298
  observer.disconnect();
3903
4299
  observer = null;
@@ -3907,8 +4303,7 @@ function releaseHeldClicks() {
3907
4303
  for (const click of held.splice(0)) {
3908
4304
  if (click.hm.kind === undefined)
3909
4305
  click.hm.kind = null;
3910
- if (coframe)
3911
- queueEventForBatchedSending(coframe, click.data);
4306
+ queueHeld(click);
3912
4307
  }
3913
4308
  if (observer) {
3914
4309
  observer.disconnect();
@@ -3994,6 +4389,7 @@ function startSession(Coframe, cookiesEnabled, executionStartTime, personalizati
3994
4389
  {
3995
4390
  queueCreateSession(...sessionPayload, undefined, undefined, personalizationHeaders, sessionContext);
3996
4391
  }
4392
+ heatmapPageView();
3997
4393
  queueEvent(Coframe, {
3998
4394
  projectId: Coframe.projectId,
3999
4395
  sessionId: Coframe.sessionId,
@@ -4665,131 +5061,10 @@ async function modifyPage(Coframe, configResult, projectResult, analytics, lifec
4665
5061
  setupFlushInterval(Coframe);
4666
5062
  }
4667
5063
 
4668
- function readCookie(name) {
4669
- const prefix = `${name}=`;
4670
- const cookies = document.cookie.split(';');
4671
- for (let i = 0; i < cookies.length; i++) {
4672
- const cookie = cookies[i].trim();
4673
- if (cookie.indexOf(prefix) === 0)
4674
- return decodeURIComponent(cookie.slice(prefix.length));
4675
- }
4676
- return undefined;
4677
- }
4678
- function readClientCommand() {
4679
- try {
4680
- const cmd = findLastCmd((c) => typeof c.cookiesEnabled === 'boolean');
4681
- if (!cmd)
4682
- return undefined;
4683
- return cmd.cookiesEnabled === true ? 'granted' : 'denied';
4684
- }
4685
- catch (_a) {
4686
- return undefined;
4687
- }
4688
- }
4689
- function readOneTrust() {
4690
- var _a;
4691
- try {
4692
- const { OnetrustActiveGroups: groups, OneTrust } = window;
4693
- if (typeof groups !== 'string' && OneTrust === undefined)
4694
- return undefined;
4695
- if (typeof groups === 'string' && groups.split(',').indexOf('C0002') !== -1)
4696
- return 'granted';
4697
- return ((_a = OneTrust === null || OneTrust === void 0 ? void 0 : OneTrust.IsAlertBoxClosed) === null || _a === void 0 ? void 0 : _a.call(OneTrust)) === true ? 'denied' : 'pending';
4698
- }
4699
- catch (_b) {
4700
- return undefined;
4701
- }
4702
- }
4703
- function readCookiebot() {
4704
- var _a;
4705
- try {
4706
- const { Cookiebot } = window;
4707
- if (Cookiebot === undefined)
4708
- return undefined;
4709
- if (((_a = Cookiebot.consent) === null || _a === void 0 ? void 0 : _a.statistics) === true)
4710
- return 'granted';
4711
- return Cookiebot.hasResponse === true ? 'denied' : 'pending';
4712
- }
4713
- catch (_b) {
4714
- return undefined;
4715
- }
4716
- }
4717
- function readTrueVault() {
4718
- try {
4719
- const cookie = readCookie('polaris_consent_settings');
4720
- if (cookie === undefined)
4721
- return undefined;
4722
- const parsed = JSON.parse(cookie);
4723
- if (parsed.analyticsPermitted === false || parsed.notOptedOut === false) {
4724
- return 'denied';
4725
- }
4726
- if (parsed.implicit === false && parsed.analyticsPermitted === true)
4727
- return 'granted';
4728
- return 'pending';
4729
- }
4730
- catch (_a) {
4731
- return undefined;
4732
- }
4733
- }
4734
- function readUsPrivacy() {
4735
- try {
4736
- const cookie = readCookie('us_privacy');
4737
- if (cookie !== undefined &&
4738
- /^1[YN-]{3}$/i.test(cookie) &&
4739
- (cookie[2] === 'Y' || cookie[2] === 'y')) {
4740
- return 'denied';
4741
- }
4742
- return undefined;
4743
- }
4744
- catch (_a) {
4745
- return undefined;
4746
- }
4747
- }
4748
- function readConsentMode() {
4749
- var _a;
4750
- try {
4751
- const { dataLayer } = window;
4752
- if (!Array.isArray(dataLayer))
4753
- return undefined;
4754
- let update;
4755
- let defaultGranted = false;
4756
- let defaultDenied = false;
4757
- for (let i = 0; i < dataLayer.length; i++) {
4758
- const entry = dataLayer[i];
4759
- if (entry === null ||
4760
- typeof entry !== 'object' ||
4761
- entry[0] !== 'consent' ||
4762
- (entry[1] !== 'default' && entry[1] !== 'update')) {
4763
- continue;
4764
- }
4765
- const storage = (_a = entry[2]) === null || _a === void 0 ? void 0 : _a.analytics_storage;
4766
- if (storage !== 'granted' && storage !== 'denied')
4767
- continue;
4768
- if (entry[1] === 'update')
4769
- update = storage;
4770
- else if (storage === 'denied')
4771
- defaultDenied = true;
4772
- else
4773
- defaultGranted = true;
4774
- }
4775
- if (update !== undefined)
4776
- return update;
4777
- if (defaultDenied)
4778
- return 'pending';
4779
- return defaultGranted ? 'granted' : undefined;
4780
- }
4781
- catch (_b) {
4782
- return undefined;
4783
- }
4784
- }
4785
-
4786
5064
  let startRequested = false;
4787
5065
  let replayScriptUrl;
4788
5066
  const COMPILED_REPLAY_SCRIPT_URL = `${"https://edge.cofra.me"}/replay/latest.js`;
4789
5067
  const CONSENT_POLL_MS = 100;
4790
- const CONSENT_WAIT_MS = 5000;
4791
- const CONSENT_DETECT_WAIT_MS = 10000;
4792
- const EXPLICIT_CONSENT_WAIT_MS = 60000;
4793
5068
  function configureReplayScriptUrl(url) {
4794
5069
  replayScriptUrl = (url === null || url === void 0 ? void 0 : url.trim()) || undefined;
4795
5070
  }
@@ -4797,71 +5072,7 @@ function isOptOut(settings) {
4797
5072
  return settings.consent_model === 'opt_out';
4798
5073
  }
4799
5074
  function readReplayConsent(settings) {
4800
- try {
4801
- if (navigator
4802
- .globalPrivacyControl === true) {
4803
- return { state: 'denied', reason: 'gpc' };
4804
- }
4805
- const shopify = window.Shopify;
4806
- if (shopify === undefined) {
4807
- if (readUsPrivacy() === 'denied')
4808
- return { state: 'denied', reason: 'consent_denied' };
4809
- if (isWaitingForCookies()) {
4810
- const signal = readClientCommand();
4811
- if (signal === 'granted')
4812
- return { state: 'allowed', source: 'client' };
4813
- if (signal === 'denied')
4814
- return { state: 'denied', reason: 'consent_denied' };
4815
- return { state: 'pending', detected: true };
4816
- }
4817
- const readers = [
4818
- [readClientCommand, 'client'],
4819
- [readOneTrust, 'onetrust'],
4820
- [readCookiebot, 'cookiebot'],
4821
- [readTrueVault, 'truevault'],
4822
- [readConsentMode, 'consent_mode'],
4823
- ];
4824
- for (let i = 0; i < readers.length; i++) {
4825
- const [read, source] = readers[i];
4826
- const signal = read();
4827
- if (signal === 'granted')
4828
- return { state: 'allowed', source };
4829
- if (signal === 'denied')
4830
- return { state: 'denied', reason: 'consent_denied' };
4831
- if (signal === 'pending') {
4832
- if (isOptOut(settings))
4833
- continue;
4834
- return { state: 'pending', detected: true };
4835
- }
4836
- }
4837
- if (isOptOut(settings))
4838
- return { state: 'allowed', source: 'opt_out' };
4839
- return { state: 'pending', detected: isWaitingForCookies() };
4840
- }
4841
- const customerPrivacy = shopify.customerPrivacy;
4842
- const analyticsProcessingAllowed = customerPrivacy === null || customerPrivacy === void 0 ? void 0 : customerPrivacy.analyticsProcessingAllowed;
4843
- if (typeof analyticsProcessingAllowed === 'function') {
4844
- return analyticsProcessingAllowed.call(customerPrivacy) === true
4845
- ? { state: 'allowed', source: 'shopify' }
4846
- : { state: 'denied', reason: 'consent_denied' };
4847
- }
4848
- return { state: 'pending' };
4849
- }
4850
- catch (_a) {
4851
- return { state: 'denied', reason: 'consent_unavailable' };
4852
- }
4853
- }
4854
- function createMonitoringPayload(coframe, monitoringType, metadata) {
4855
- return {
4856
- event_id: generateUUID(),
4857
- monitoring_type: monitoringType,
4858
- local_time: new Date().toISOString(),
4859
- project_id: coframe.projectId,
4860
- session_id: coframe.sessionId,
4861
- url: location.href,
4862
- org_id: coframe.orgId,
4863
- metadata,
4864
- };
5075
+ return readVisitorConsent(isOptOut(settings));
4865
5076
  }
4866
5077
  function reportStartFailure(coframe, reason) {
4867
5078
  try {