@adhub-so/adhub-nextjs-sdk 0.1.1 → 0.2.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/README.md +160 -80
- package/dist/index.cjs +172 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +172 -13
- package/dist/index.js.map +1 -1
- package/dist/server.cjs +16 -6
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +1 -0
- package/dist/server.d.ts +1 -0
- package/dist/server.js +16 -6
- package/dist/server.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-NF56G46G.js +0 -140
- package/dist/chunk-NF56G46G.js.map +0 -1
- package/dist/testing.cjs +0 -149
- package/dist/testing.cjs.map +0 -1
- package/dist/testing.d.cts +0 -6
- package/dist/testing.d.ts +0 -6
- package/dist/testing.js +0 -72
- package/dist/testing.js.map +0 -1
- package/dist/types-DMUJO8Kj.d.cts +0 -62
- package/dist/types-DMUJO8Kj.d.ts +0 -62
package/dist/index.js
CHANGED
|
@@ -356,13 +356,72 @@ 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.
|
|
359
|
+
var SDK_VERSION = "0.2.0";
|
|
360
360
|
var REQUEST_TIMEOUT_MS = 5e3;
|
|
361
361
|
var RETRY_INTERVAL_MS = 15e3;
|
|
362
362
|
var PAGE_ENGAGEMENT_INTERVAL_MS = 5e3;
|
|
363
363
|
var DEFAULT_SESSION_TIMEOUT_MS = 30 * 60 * 1e3;
|
|
364
364
|
var BEHAVIOR_EVENTS_PATH = "/api/v1/saas/sdk/behavior-events";
|
|
365
365
|
var PAGE_ENGAGEMENTS_PATH = "/api/v1/saas/sdk/page-engagements";
|
|
366
|
+
var SDK_CONFIG_PATH = "/api/v1/saas/sdk/config";
|
|
367
|
+
var GOOGLE_ADS_CONVERSION_ID = /^AW-\d{6,20}$/;
|
|
368
|
+
function decideGoogleAdsTag(payload) {
|
|
369
|
+
if (!payload || typeof payload !== "object") return { action: "skip" };
|
|
370
|
+
const tag = payload.googleAdsWebsiteTag;
|
|
371
|
+
if (!tag || typeof tag !== "object") return { action: "skip" };
|
|
372
|
+
const record = tag;
|
|
373
|
+
if (record.enabled !== true) return { action: "skip" };
|
|
374
|
+
if (typeof record.conversionId !== "string" || record.conversionId.trim() === "") {
|
|
375
|
+
return { action: "skip" };
|
|
376
|
+
}
|
|
377
|
+
const conversionId = record.conversionId.trim();
|
|
378
|
+
if (!GOOGLE_ADS_CONVERSION_ID.test(conversionId)) return { action: "invalid" };
|
|
379
|
+
return { action: "inject", conversionId };
|
|
380
|
+
}
|
|
381
|
+
function adHubGtagMarker() {
|
|
382
|
+
const current = window.__ADHUB_GTAG__;
|
|
383
|
+
if (current && typeof current.js === "boolean" && Array.isArray(current.configuredIds)) {
|
|
384
|
+
return current;
|
|
385
|
+
}
|
|
386
|
+
const marker = { js: false, configuredIds: [] };
|
|
387
|
+
window.__ADHUB_GTAG__ = marker;
|
|
388
|
+
return marker;
|
|
389
|
+
}
|
|
390
|
+
function gtagScriptSrc(conversionId) {
|
|
391
|
+
return `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(conversionId)}`;
|
|
392
|
+
}
|
|
393
|
+
function installGtagShim() {
|
|
394
|
+
window.dataLayer = window.dataLayer || [];
|
|
395
|
+
if (typeof window.gtag === "function") return;
|
|
396
|
+
window.gtag = function gtag() {
|
|
397
|
+
window.dataLayer?.push(arguments);
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
function ensureGtagScript(conversionId) {
|
|
401
|
+
const src = gtagScriptSrc(conversionId);
|
|
402
|
+
for (const script2 of document.getElementsByTagName("script")) {
|
|
403
|
+
if (script2.src === src) return;
|
|
404
|
+
}
|
|
405
|
+
const script = document.createElement("script");
|
|
406
|
+
script.async = true;
|
|
407
|
+
script.src = src;
|
|
408
|
+
script.setAttribute("data-adhub-gtag", conversionId);
|
|
409
|
+
(document.head ?? document.documentElement).appendChild(script);
|
|
410
|
+
}
|
|
411
|
+
function injectGoogleAdsWebsiteTag(conversionId) {
|
|
412
|
+
const marker = adHubGtagMarker();
|
|
413
|
+
if (marker.configuredIds.includes(conversionId)) return;
|
|
414
|
+
installGtagShim();
|
|
415
|
+
ensureGtagScript(conversionId);
|
|
416
|
+
const gtag = window.gtag;
|
|
417
|
+
if (typeof gtag !== "function") return;
|
|
418
|
+
if (!marker.js) {
|
|
419
|
+
marker.js = true;
|
|
420
|
+
gtag("js", /* @__PURE__ */ new Date());
|
|
421
|
+
}
|
|
422
|
+
marker.configuredIds.push(conversionId);
|
|
423
|
+
gtag("config", conversionId);
|
|
424
|
+
}
|
|
366
425
|
var EMPTY_STATE = {
|
|
367
426
|
initialized: false,
|
|
368
427
|
anonymousId: null,
|
|
@@ -488,7 +547,11 @@ var AdHubBrowserClient = class {
|
|
|
488
547
|
this.autoTrackOutgoingLinks = true;
|
|
489
548
|
this.autoTrackDataAttributes = true;
|
|
490
549
|
this.trackHashChanges = false;
|
|
550
|
+
this.waitForConsent = false;
|
|
551
|
+
this.gtagAbort = null;
|
|
491
552
|
this.lastPageSignature = "";
|
|
553
|
+
this.lastTrackedPageUrl = "";
|
|
554
|
+
this.pendingPageReferrer = null;
|
|
492
555
|
this.originalPushState = null;
|
|
493
556
|
this.originalReplaceState = null;
|
|
494
557
|
this.patchedPushState = null;
|
|
@@ -539,10 +602,17 @@ var AdHubBrowserClient = class {
|
|
|
539
602
|
return;
|
|
540
603
|
}
|
|
541
604
|
if (!browserAvailable()) return;
|
|
542
|
-
if (this.state.initialized && this.projectKey === projectKey && this.endpoint === endpoint)
|
|
605
|
+
if (this.state.initialized && this.projectKey === projectKey && this.endpoint === endpoint) {
|
|
606
|
+
if (this.waitForConsent && options.waitForConsent !== true) {
|
|
607
|
+
this.waitForConsent = false;
|
|
608
|
+
this.scheduleGoogleAdsWebsiteTag();
|
|
609
|
+
}
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
543
612
|
this.projectKey = projectKey;
|
|
544
613
|
this.endpoint = endpoint;
|
|
545
614
|
this.debug = options.debug ?? false;
|
|
615
|
+
this.waitForConsent = options.waitForConsent === true;
|
|
546
616
|
this.autoFlush = options.autoFlush ?? true;
|
|
547
617
|
this.autoPageview = options.autoPageview ?? true;
|
|
548
618
|
this.autoPageEngagement = options.autoPageEngagement ?? true;
|
|
@@ -588,6 +658,7 @@ var AdHubBrowserClient = class {
|
|
|
588
658
|
this.installBehaviorTracking();
|
|
589
659
|
}
|
|
590
660
|
this.scheduleFlush();
|
|
661
|
+
this.scheduleGoogleAdsWebsiteTag();
|
|
591
662
|
} catch (error) {
|
|
592
663
|
this.reportError(error);
|
|
593
664
|
}
|
|
@@ -642,7 +713,10 @@ var AdHubBrowserClient = class {
|
|
|
642
713
|
normalized.title,
|
|
643
714
|
normalized.eventId
|
|
644
715
|
].join("|");
|
|
645
|
-
if (!input.eventId && signature === this.lastPageSignature)
|
|
716
|
+
if (!input.eventId && signature === this.lastPageSignature) {
|
|
717
|
+
this.pendingPageReferrer = null;
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
646
720
|
this.lastPageSignature = signature;
|
|
647
721
|
this.finishPageVisit();
|
|
648
722
|
const pageViewId = normalized.eventId ?? randomId("page");
|
|
@@ -650,6 +724,9 @@ var AdHubBrowserClient = class {
|
|
|
650
724
|
if (this.autoPageEngagement) {
|
|
651
725
|
this.startPageVisit(normalized, pageViewId, sessionId);
|
|
652
726
|
}
|
|
727
|
+
this.lastTrackedPageUrl = normalized.pageUrl ?? safePageUrl(window.location.href);
|
|
728
|
+
this.pendingPageReferrer = null;
|
|
729
|
+
this.trackGoogleAdsPageView();
|
|
653
730
|
} catch (error) {
|
|
654
731
|
this.reportError(error);
|
|
655
732
|
}
|
|
@@ -708,6 +785,8 @@ var AdHubBrowserClient = class {
|
|
|
708
785
|
this.store.writeSession(sessionId);
|
|
709
786
|
this.updateState({ anonymousId, sessionId, profileId: null, lastError: null });
|
|
710
787
|
this.lastPageSignature = "";
|
|
788
|
+
this.lastTrackedPageUrl = "";
|
|
789
|
+
this.pendingPageReferrer = null;
|
|
711
790
|
if (this.autoPageview) this.page();
|
|
712
791
|
} catch (error) {
|
|
713
792
|
this.reportError(error);
|
|
@@ -735,6 +814,8 @@ var AdHubBrowserClient = class {
|
|
|
735
814
|
return () => this.listeners.delete(listener);
|
|
736
815
|
}
|
|
737
816
|
destroy() {
|
|
817
|
+
this.gtagAbort?.abort();
|
|
818
|
+
this.gtagAbort = null;
|
|
738
819
|
this.finishPageVisit();
|
|
739
820
|
if (browserAvailable() && this.lifecycleInstalled) {
|
|
740
821
|
window.removeEventListener("online", this.onOnline);
|
|
@@ -800,11 +881,13 @@ var AdHubBrowserClient = class {
|
|
|
800
881
|
return sessionId;
|
|
801
882
|
}
|
|
802
883
|
currentPageFields() {
|
|
884
|
+
const pageUrl = safePageUrl(window.location.href);
|
|
885
|
+
const referrer = this.pendingPageReferrer ?? (this.lastTrackedPageUrl && this.lastTrackedPageUrl !== pageUrl ? this.lastTrackedPageUrl : document.referrer || null);
|
|
803
886
|
return {
|
|
804
|
-
pageUrl
|
|
887
|
+
pageUrl,
|
|
805
888
|
path: window.location.pathname || "/",
|
|
806
889
|
title: document.title,
|
|
807
|
-
referrer
|
|
890
|
+
referrer
|
|
808
891
|
};
|
|
809
892
|
}
|
|
810
893
|
enqueueBehaviorEvent(normalized) {
|
|
@@ -915,13 +998,10 @@ var AdHubBrowserClient = class {
|
|
|
915
998
|
try {
|
|
916
999
|
const response = await fetch(`${this.endpoint}${item.path}`, {
|
|
917
1000
|
method: "POST",
|
|
918
|
-
headers: {
|
|
1001
|
+
headers: this.sdkHeaders({
|
|
919
1002
|
"Content-Type": "application/json",
|
|
920
|
-
"Idempotency-Key": item.requestId
|
|
921
|
-
|
|
922
|
-
"X-AdHub-SDK": SDK_NAME,
|
|
923
|
-
"X-AdHub-SDK-Version": SDK_VERSION
|
|
924
|
-
},
|
|
1003
|
+
"Idempotency-Key": item.requestId
|
|
1004
|
+
}),
|
|
925
1005
|
body: JSON.stringify(item.body),
|
|
926
1006
|
keepalive: true,
|
|
927
1007
|
credentials: "omit",
|
|
@@ -974,6 +1054,74 @@ var AdHubBrowserClient = class {
|
|
|
974
1054
|
this.store?.saveQueue(this.queue);
|
|
975
1055
|
this.updateState({ queuedCount: this.queue.length });
|
|
976
1056
|
}
|
|
1057
|
+
scheduleGoogleAdsWebsiteTag() {
|
|
1058
|
+
if (!browserAvailable() || this.waitForConsent) return;
|
|
1059
|
+
this.gtagAbort?.abort();
|
|
1060
|
+
const abort = new AbortController();
|
|
1061
|
+
this.gtagAbort = abort;
|
|
1062
|
+
queueMicrotask(() => {
|
|
1063
|
+
if (abort.signal.aborted) return;
|
|
1064
|
+
void this.fetchAndInjectGoogleAdsWebsiteTag(abort);
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
async fetchAndInjectGoogleAdsWebsiteTag(abort) {
|
|
1068
|
+
const timeout = setTimeout(() => abort.abort(), REQUEST_TIMEOUT_MS);
|
|
1069
|
+
try {
|
|
1070
|
+
const response = await fetch(`${this.endpoint}${SDK_CONFIG_PATH}`, {
|
|
1071
|
+
method: "GET",
|
|
1072
|
+
headers: this.sdkHeaders(),
|
|
1073
|
+
credentials: "omit",
|
|
1074
|
+
signal: abort.signal
|
|
1075
|
+
});
|
|
1076
|
+
if (abort.signal.aborted) return;
|
|
1077
|
+
if (!response.ok) {
|
|
1078
|
+
this.log(`Google Ads website tag config returned HTTP ${response.status}.`);
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
let payload;
|
|
1082
|
+
try {
|
|
1083
|
+
payload = await response.json();
|
|
1084
|
+
} catch (error) {
|
|
1085
|
+
this.log(`Google Ads website tag config was not applied: ${errorMessage(error)}`);
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
if (abort.signal.aborted) return;
|
|
1089
|
+
const decision = decideGoogleAdsTag(payload);
|
|
1090
|
+
if (decision.action === "skip") return;
|
|
1091
|
+
if (decision.action === "invalid") {
|
|
1092
|
+
this.log("Google Ads website tag config did not include a valid AW- conversion id.");
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1095
|
+
injectGoogleAdsWebsiteTag(decision.conversionId);
|
|
1096
|
+
} 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);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
trackGoogleAdsPageView() {
|
|
1104
|
+
if (!this.autoPageview || !browserAvailable()) return;
|
|
1105
|
+
const marker = window.__ADHUB_GTAG__;
|
|
1106
|
+
if (!marker?.configuredIds.length || typeof window.gtag !== "function") return;
|
|
1107
|
+
try {
|
|
1108
|
+
window.gtag("event", "page_view", {
|
|
1109
|
+
page_location: window.location.href,
|
|
1110
|
+
page_path: `${window.location.pathname}${window.location.search}`,
|
|
1111
|
+
page_title: document.title
|
|
1112
|
+
});
|
|
1113
|
+
} catch (error) {
|
|
1114
|
+
this.log(`Google Ads page_view was not sent: ${errorMessage(error)}`);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
sdkHeaders(extra) {
|
|
1118
|
+
return {
|
|
1119
|
+
"X-AdHub-Project-Key": this.projectKey,
|
|
1120
|
+
"X-AdHub-SDK": SDK_NAME,
|
|
1121
|
+
"X-AdHub-SDK-Version": SDK_VERSION,
|
|
1122
|
+
...extra
|
|
1123
|
+
};
|
|
1124
|
+
}
|
|
977
1125
|
installLifecycle() {
|
|
978
1126
|
if (this.lifecycleInstalled) return;
|
|
979
1127
|
window.addEventListener("online", this.onOnline);
|
|
@@ -1002,14 +1150,25 @@ var AdHubBrowserClient = class {
|
|
|
1002
1150
|
};
|
|
1003
1151
|
const originalPushState = this.originalPushState;
|
|
1004
1152
|
const originalReplaceState = this.originalReplaceState;
|
|
1153
|
+
const client = this;
|
|
1005
1154
|
this.patchedPushState = function pushState(...args) {
|
|
1155
|
+
const previousPageUrl = safePageUrl(window.location.href);
|
|
1006
1156
|
const result = originalPushState.apply(this, args);
|
|
1007
|
-
|
|
1157
|
+
const nextPageUrl = safePageUrl(window.location.href);
|
|
1158
|
+
if (previousPageUrl !== nextPageUrl) {
|
|
1159
|
+
client.pendingPageReferrer ?? (client.pendingPageReferrer = client.lastTrackedPageUrl || previousPageUrl);
|
|
1160
|
+
dispatchLocationChange();
|
|
1161
|
+
}
|
|
1008
1162
|
return result;
|
|
1009
1163
|
};
|
|
1010
1164
|
this.patchedReplaceState = function replaceState(...args) {
|
|
1165
|
+
const previousPageUrl = safePageUrl(window.location.href);
|
|
1011
1166
|
const result = originalReplaceState.apply(this, args);
|
|
1012
|
-
|
|
1167
|
+
const nextPageUrl = safePageUrl(window.location.href);
|
|
1168
|
+
if (previousPageUrl !== nextPageUrl) {
|
|
1169
|
+
client.pendingPageReferrer ?? (client.pendingPageReferrer = client.lastTrackedPageUrl || previousPageUrl);
|
|
1170
|
+
dispatchLocationChange();
|
|
1171
|
+
}
|
|
1013
1172
|
return result;
|
|
1014
1173
|
};
|
|
1015
1174
|
history.pushState = this.patchedPushState;
|