@adhub-so/adhub-nextjs-sdk 0.2.0 → 0.3.0

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/dist/index.js CHANGED
@@ -356,7 +356,7 @@ var BrowserStore = class {
356
356
  // src/client.ts
357
357
  var DEFAULT_ENDPOINT = "https://api.adhub.so";
358
358
  var SDK_NAME = "nextjs";
359
- var SDK_VERSION = "0.2.0";
359
+ var SDK_VERSION = "0.3.0";
360
360
  var REQUEST_TIMEOUT_MS = 5e3;
361
361
  var RETRY_INTERVAL_MS = 15e3;
362
362
  var PAGE_ENGAGEMENT_INTERVAL_MS = 5e3;
@@ -365,6 +365,7 @@ var BEHAVIOR_EVENTS_PATH = "/api/v1/saas/sdk/behavior-events";
365
365
  var PAGE_ENGAGEMENTS_PATH = "/api/v1/saas/sdk/page-engagements";
366
366
  var SDK_CONFIG_PATH = "/api/v1/saas/sdk/config";
367
367
  var GOOGLE_ADS_CONVERSION_ID = /^AW-\d{6,20}$/;
368
+ var CLARITY_PROJECT_ID = /^[0-9a-z]{4,32}$/;
368
369
  function decideGoogleAdsTag(payload) {
369
370
  if (!payload || typeof payload !== "object") return { action: "skip" };
370
371
  const tag = payload.googleAdsWebsiteTag;
@@ -422,6 +423,62 @@ function injectGoogleAdsWebsiteTag(conversionId) {
422
423
  marker.configuredIds.push(conversionId);
423
424
  gtag("config", conversionId);
424
425
  }
426
+ function decideClarity(payload) {
427
+ if (!payload || typeof payload !== "object") return { action: "skip" };
428
+ const clarity = payload.clarity;
429
+ if (!clarity || typeof clarity !== "object") return { action: "skip" };
430
+ const record = clarity;
431
+ if (record.enabled !== true) return { action: "skip" };
432
+ if (typeof record.projectId !== "string" || record.projectId.trim() === "") {
433
+ return { action: "skip" };
434
+ }
435
+ const projectId = record.projectId.trim();
436
+ if (!CLARITY_PROJECT_ID.test(projectId)) return { action: "invalid" };
437
+ return { action: "inject", projectId };
438
+ }
439
+ function clarityScriptSrc(projectId) {
440
+ return `https://www.clarity.ms/tag/${projectId}`;
441
+ }
442
+ function installClarityQueue() {
443
+ window.clarity = window.clarity || function clarity() {
444
+ const stub = window.clarity;
445
+ if (!stub) return;
446
+ (stub.q = stub.q || []).push(arguments);
447
+ };
448
+ }
449
+ function clarityProjectOnPage() {
450
+ const marker = window.__ADHUB_CLARITY__;
451
+ if (marker && typeof marker.projectId === "string" && CLARITY_PROJECT_ID.test(marker.projectId)) {
452
+ return marker.projectId;
453
+ }
454
+ const match = /^https:\/\/www\.clarity\.ms\/tag\/([0-9a-z]{4,32})$/;
455
+ for (const script of document.getElementsByTagName("script")) {
456
+ const projectId = match.exec(script.src)?.[1];
457
+ if (projectId) return projectId;
458
+ }
459
+ return null;
460
+ }
461
+ function injectClarity(projectId) {
462
+ const installed = clarityProjectOnPage();
463
+ if (installed) {
464
+ if (!window.__ADHUB_CLARITY__) window.__ADHUB_CLARITY__ = { projectId: installed };
465
+ if (installed === projectId) installClarityQueue();
466
+ return installed === projectId;
467
+ }
468
+ installClarityQueue();
469
+ const script = document.createElement("script");
470
+ script.async = true;
471
+ script.src = clarityScriptSrc(projectId);
472
+ script.setAttribute("data-adhub-clarity", projectId);
473
+ const firstScript = document.getElementsByTagName("script")[0];
474
+ if (firstScript?.parentNode) {
475
+ firstScript.parentNode.insertBefore(script, firstScript);
476
+ } else {
477
+ (document.head ?? document.documentElement).appendChild(script);
478
+ }
479
+ window.__ADHUB_CLARITY__ = { projectId };
480
+ return true;
481
+ }
425
482
  var EMPTY_STATE = {
426
483
  initialized: false,
427
484
  anonymousId: null,
@@ -548,7 +605,8 @@ var AdHubBrowserClient = class {
548
605
  this.autoTrackDataAttributes = true;
549
606
  this.trackHashChanges = false;
550
607
  this.waitForConsent = false;
551
- this.gtagAbort = null;
608
+ this.sdkConfigAbort = null;
609
+ this.clarityIdentifiedProfileId = null;
552
610
  this.lastPageSignature = "";
553
611
  this.lastTrackedPageUrl = "";
554
612
  this.pendingPageReferrer = null;
@@ -605,7 +663,7 @@ var AdHubBrowserClient = class {
605
663
  if (this.state.initialized && this.projectKey === projectKey && this.endpoint === endpoint) {
606
664
  if (this.waitForConsent && options.waitForConsent !== true) {
607
665
  this.waitForConsent = false;
608
- this.scheduleGoogleAdsWebsiteTag();
666
+ this.scheduleSdkConfigTags();
609
667
  }
610
668
  return;
611
669
  }
@@ -658,7 +716,7 @@ var AdHubBrowserClient = class {
658
716
  this.installBehaviorTracking();
659
717
  }
660
718
  this.scheduleFlush();
661
- this.scheduleGoogleAdsWebsiteTag();
719
+ this.scheduleSdkConfigTags();
662
720
  } catch (error) {
663
721
  this.reportError(error);
664
722
  }
@@ -693,6 +751,7 @@ var AdHubBrowserClient = class {
693
751
  this.updateState({ profileId: profile.profileId, lastError: null });
694
752
  this.enqueue("/api/v1/saas/sdk/identify", "profile.identify", data);
695
753
  this.scheduleFlush();
754
+ this.identifyInClarity(profile.profileId);
696
755
  } catch (error) {
697
756
  this.reportError(error);
698
757
  }
@@ -784,6 +843,7 @@ var AdHubBrowserClient = class {
784
843
  const sessionId = randomId("sess");
785
844
  this.store.writeSession(sessionId);
786
845
  this.updateState({ anonymousId, sessionId, profileId: null, lastError: null });
846
+ this.clarityIdentifiedProfileId = null;
787
847
  this.lastPageSignature = "";
788
848
  this.lastTrackedPageUrl = "";
789
849
  this.pendingPageReferrer = null;
@@ -814,8 +874,8 @@ var AdHubBrowserClient = class {
814
874
  return () => this.listeners.delete(listener);
815
875
  }
816
876
  destroy() {
817
- this.gtagAbort?.abort();
818
- this.gtagAbort = null;
877
+ this.sdkConfigAbort?.abort();
878
+ this.sdkConfigAbort = null;
819
879
  this.finishPageVisit();
820
880
  if (browserAvailable() && this.lifecycleInstalled) {
821
881
  window.removeEventListener("online", this.onOnline);
@@ -1054,17 +1114,17 @@ var AdHubBrowserClient = class {
1054
1114
  this.store?.saveQueue(this.queue);
1055
1115
  this.updateState({ queuedCount: this.queue.length });
1056
1116
  }
1057
- scheduleGoogleAdsWebsiteTag() {
1117
+ scheduleSdkConfigTags() {
1058
1118
  if (!browserAvailable() || this.waitForConsent) return;
1059
- this.gtagAbort?.abort();
1119
+ this.sdkConfigAbort?.abort();
1060
1120
  const abort = new AbortController();
1061
- this.gtagAbort = abort;
1121
+ this.sdkConfigAbort = abort;
1062
1122
  queueMicrotask(() => {
1063
1123
  if (abort.signal.aborted) return;
1064
- void this.fetchAndInjectGoogleAdsWebsiteTag(abort);
1124
+ void this.fetchAndApplySdkConfig(abort);
1065
1125
  });
1066
1126
  }
1067
- async fetchAndInjectGoogleAdsWebsiteTag(abort) {
1127
+ async fetchAndApplySdkConfig(abort) {
1068
1128
  const timeout = setTimeout(() => abort.abort(), REQUEST_TIMEOUT_MS);
1069
1129
  try {
1070
1130
  const response = await fetch(`${this.endpoint}${SDK_CONFIG_PATH}`, {
@@ -1075,29 +1135,58 @@ var AdHubBrowserClient = class {
1075
1135
  });
1076
1136
  if (abort.signal.aborted) return;
1077
1137
  if (!response.ok) {
1078
- this.log(`Google Ads website tag config returned HTTP ${response.status}.`);
1138
+ this.log(`SDK config returned HTTP ${response.status}.`);
1079
1139
  return;
1080
1140
  }
1081
1141
  let payload;
1082
1142
  try {
1083
1143
  payload = await response.json();
1084
1144
  } catch (error) {
1085
- this.log(`Google Ads website tag config was not applied: ${errorMessage(error)}`);
1145
+ this.log(`SDK config was not applied: ${errorMessage(error)}`);
1086
1146
  return;
1087
1147
  }
1088
1148
  if (abort.signal.aborted) return;
1089
- const decision = decideGoogleAdsTag(payload);
1149
+ this.applyGoogleAdsWebsiteTag(payload);
1150
+ this.applyClarity(payload);
1151
+ } catch (error) {
1152
+ if (abort.signal.aborted) return;
1153
+ this.log(`SDK config was not applied: ${errorMessage(error)}`);
1154
+ } finally {
1155
+ clearTimeout(timeout);
1156
+ }
1157
+ }
1158
+ applyGoogleAdsWebsiteTag(payload) {
1159
+ const decision = decideGoogleAdsTag(payload);
1160
+ if (decision.action === "skip") return;
1161
+ if (decision.action === "invalid") {
1162
+ this.log("Google Ads website tag config did not include a valid AW- conversion id.");
1163
+ return;
1164
+ }
1165
+ injectGoogleAdsWebsiteTag(decision.conversionId);
1166
+ }
1167
+ applyClarity(payload) {
1168
+ try {
1169
+ const decision = decideClarity(payload);
1090
1170
  if (decision.action === "skip") return;
1091
1171
  if (decision.action === "invalid") {
1092
- this.log("Google Ads website tag config did not include a valid AW- conversion id.");
1172
+ this.log("Microsoft Clarity config did not include a valid project id.");
1093
1173
  return;
1094
1174
  }
1095
- injectGoogleAdsWebsiteTag(decision.conversionId);
1175
+ if (!injectClarity(decision.projectId)) return;
1176
+ const profileId = this.state.profileId;
1177
+ if (profileId) this.identifyInClarity(profileId);
1096
1178
  } catch (error) {
1097
- if (abort.signal.aborted) return;
1098
- this.log(`Google Ads website tag config was not applied: ${errorMessage(error)}`);
1099
- } finally {
1100
- clearTimeout(timeout);
1179
+ this.log(`Microsoft Clarity was not applied: ${errorMessage(error)}`);
1180
+ }
1181
+ }
1182
+ identifyInClarity(profileId) {
1183
+ if (!profileId || this.clarityIdentifiedProfileId === profileId) return;
1184
+ if (!browserAvailable() || typeof window.clarity !== "function") return;
1185
+ try {
1186
+ window.clarity("identify", profileId);
1187
+ this.clarityIdentifiedProfileId = profileId;
1188
+ } catch (error) {
1189
+ this.log(`Microsoft Clarity identify was not sent: ${errorMessage(error)}`);
1101
1190
  }
1102
1191
  }
1103
1192
  trackGoogleAdsPageView() {