@adhub-so/adhub-nextjs-sdk 0.1.1 → 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/README.md +182 -80
- package/dist/index.cjs +261 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +261 -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/testing.cjs
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// src/utils.ts
|
|
4
|
-
var MAX_PARAM_LENGTH = 2048;
|
|
5
|
-
var MAX_ROUTE_LENGTH = 255;
|
|
6
|
-
var MAX_URL_LENGTH = 8192;
|
|
7
|
-
var MAX_UTM_KEYS = 50;
|
|
8
|
-
var MAX_VALUES_PER_PARAM = 10;
|
|
9
|
-
var UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
10
|
-
var MAX_PROFILE_ID_LENGTH = 255;
|
|
11
|
-
var PROFILE_ID_CONTROL_PATTERN = /[\p{Cc}\p{Cf}]/u;
|
|
12
|
-
function nowIso() {
|
|
13
|
-
return (/* @__PURE__ */ new Date()).toISOString();
|
|
14
|
-
}
|
|
15
|
-
function randomId(prefix) {
|
|
16
|
-
const uuid = globalThis.crypto?.randomUUID?.();
|
|
17
|
-
if (uuid) return `${prefix}_${uuid}`;
|
|
18
|
-
const random = Math.random().toString(36).slice(2);
|
|
19
|
-
return `${prefix}_${Date.now().toString(36)}${random}`;
|
|
20
|
-
}
|
|
21
|
-
function truncate(value) {
|
|
22
|
-
return value.slice(0, MAX_PARAM_LENGTH);
|
|
23
|
-
}
|
|
24
|
-
function unique(values) {
|
|
25
|
-
return [...new Set(values.filter(Boolean).map(truncate))];
|
|
26
|
-
}
|
|
27
|
-
function valuesFor(searchParams, wantedKey) {
|
|
28
|
-
const values = [];
|
|
29
|
-
for (const [key, value] of searchParams.entries()) {
|
|
30
|
-
if (key.toLowerCase() === wantedKey) values.push(value);
|
|
31
|
-
}
|
|
32
|
-
return unique(values).slice(0, MAX_VALUES_PER_PARAM);
|
|
33
|
-
}
|
|
34
|
-
function isUuid(value) {
|
|
35
|
-
return UUID_PATTERN.test(value);
|
|
36
|
-
}
|
|
37
|
-
function normalizeProfileId(value) {
|
|
38
|
-
const normalized = value.trim();
|
|
39
|
-
if (!normalized || [...normalized].length > MAX_PROFILE_ID_LENGTH || PROFILE_ID_CONTROL_PATTERN.test(normalized)) {
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
return normalized;
|
|
43
|
-
}
|
|
44
|
-
function safePageUrl(input) {
|
|
45
|
-
try {
|
|
46
|
-
const url = new URL(input);
|
|
47
|
-
return `${url.origin}${url.pathname}`.slice(0, MAX_URL_LENGTH);
|
|
48
|
-
} catch {
|
|
49
|
-
return "";
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
function safeReferrer(input) {
|
|
53
|
-
if (!input) return null;
|
|
54
|
-
const safe = safePageUrl(input);
|
|
55
|
-
return safe || null;
|
|
56
|
-
}
|
|
57
|
-
function captureTouchpoint(location, referrer, capturedAt = nowIso()) {
|
|
58
|
-
const url = new URL(location.href);
|
|
59
|
-
const clickIds = {
|
|
60
|
-
gclid: valuesFor(url.searchParams, "gclid"),
|
|
61
|
-
gbraid: valuesFor(url.searchParams, "gbraid"),
|
|
62
|
-
wbraid: valuesFor(url.searchParams, "wbraid")
|
|
63
|
-
};
|
|
64
|
-
const utm = {};
|
|
65
|
-
for (const key of [...new Set([...url.searchParams.keys()].map((key2) => key2.toLowerCase()))].filter((candidate) => candidate.startsWith("utm_")).slice(0, MAX_UTM_KEYS)) {
|
|
66
|
-
utm[key] = valuesFor(url.searchParams, key);
|
|
67
|
-
}
|
|
68
|
-
return {
|
|
69
|
-
ahRoute: valuesFor(url.searchParams, "ah_route")[0]?.slice(0, MAX_ROUTE_LENGTH) ?? null,
|
|
70
|
-
clickIds,
|
|
71
|
-
utm,
|
|
72
|
-
landingUrl: safePageUrl(url.href),
|
|
73
|
-
referrer: safeReferrer(referrer),
|
|
74
|
-
// Browsers do not know Google's server-side click timestamp. This is the
|
|
75
|
-
// first-party observation time and is the upper bound available to the SDK.
|
|
76
|
-
clickTime: capturedAt
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// src/testing.ts
|
|
81
|
-
var CLICK_ID_TYPES = /* @__PURE__ */ new Set(["gclid", "gbraid", "wbraid"]);
|
|
82
|
-
function envelope(projectKey, anonymousId, type, data, sentAt) {
|
|
83
|
-
return {
|
|
84
|
-
schemaVersion: "1.0",
|
|
85
|
-
type,
|
|
86
|
-
requestId: randomId("req_test"),
|
|
87
|
-
sentAt,
|
|
88
|
-
projectKey,
|
|
89
|
-
anonymousId,
|
|
90
|
-
data
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
function createAttributionTestEnvelopes(input) {
|
|
94
|
-
const projectKey = input.projectKey.trim();
|
|
95
|
-
const attributionRouteKey = input.attributionRouteKey.trim();
|
|
96
|
-
const clickId = input.clickId.trim();
|
|
97
|
-
const profileId = normalizeProfileId(input.profileId);
|
|
98
|
-
if (!isUuid(projectKey)) {
|
|
99
|
-
throw new Error("Attribution test requires a UUID project identifier.");
|
|
100
|
-
}
|
|
101
|
-
if (!isUuid(attributionRouteKey)) {
|
|
102
|
-
throw new Error("Attribution test requires a UUID attribution route identifier.");
|
|
103
|
-
}
|
|
104
|
-
if (!profileId) {
|
|
105
|
-
throw new Error(
|
|
106
|
-
"Attribution test profileId must be 1-255 characters without control characters."
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
if (!CLICK_ID_TYPES.has(input.clickIdType) || !clickId || clickId.length > 2048 || /\s/.test(clickId)) {
|
|
110
|
-
throw new Error("Attribution test requires one valid Google click ID.");
|
|
111
|
-
}
|
|
112
|
-
const capturedAt = input.capturedAt ?? nowIso();
|
|
113
|
-
const testUrl = new URL(input.pageUrl);
|
|
114
|
-
const trackingKeys = /* @__PURE__ */ new Set(["ah_route", "gclid", "gbraid", "wbraid"]);
|
|
115
|
-
for (const key of [...testUrl.searchParams.keys()]) {
|
|
116
|
-
if (trackingKeys.has(key.toLowerCase())) testUrl.searchParams.delete(key);
|
|
117
|
-
}
|
|
118
|
-
testUrl.searchParams.set("ah_route", attributionRouteKey);
|
|
119
|
-
testUrl.searchParams.set(input.clickIdType, clickId);
|
|
120
|
-
const captured = captureTouchpoint(
|
|
121
|
-
{ href: testUrl.toString() },
|
|
122
|
-
input.referrer ?? "",
|
|
123
|
-
capturedAt
|
|
124
|
-
);
|
|
125
|
-
const anonymousId = randomId("anon_test");
|
|
126
|
-
const sessionId = randomId("sess_test");
|
|
127
|
-
const touchpointData = {
|
|
128
|
-
...captured,
|
|
129
|
-
sessionId,
|
|
130
|
-
locale: input.locale ?? null,
|
|
131
|
-
timezone: input.timezone ?? null
|
|
132
|
-
};
|
|
133
|
-
const identityData = {
|
|
134
|
-
profile: {
|
|
135
|
-
profileId,
|
|
136
|
-
properties: { source: "adhub_attribution_test" }
|
|
137
|
-
},
|
|
138
|
-
identifiedAt: capturedAt,
|
|
139
|
-
pageUrl: safePageUrl(input.pageUrl)
|
|
140
|
-
};
|
|
141
|
-
return {
|
|
142
|
-
touchpoint: envelope(projectKey, anonymousId, "touchpoint.capture", touchpointData, capturedAt),
|
|
143
|
-
identity: envelope(projectKey, anonymousId, "profile.identify", identityData, capturedAt)
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
exports.createAttributionTestEnvelopes = createAttributionTestEnvelopes;
|
|
148
|
-
//# sourceMappingURL=testing.cjs.map
|
|
149
|
-
//# sourceMappingURL=testing.cjs.map
|
package/dist/testing.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts","../src/testing.ts"],"names":["key"],"mappings":";;;AAUA,IAAM,gBAAA,GAAmB,IAAA;AACzB,IAAM,gBAAA,GAAmB,GAAA;AACzB,IAAM,cAAA,GAAiB,IAAA;AACvB,IAAM,YAAA,GAAe,EAAA;AACrB,IAAM,oBAAA,GAAuB,EAAA;AAC7B,IAAM,YAAA,GAAe,4EAAA;AAKrB,IAAM,qBAAA,GAAwB,GAAA;AAC9B,IAAM,0BAAA,GAA6B,iBAAA;AA2B5B,SAAS,MAAA,GAAiB;AAC/B,EAAA,OAAA,iBAAO,IAAI,IAAA,EAAK,EAAE,WAAA,EAAY;AAChC;AAEO,SAAS,SAAS,MAAA,EAAwB;AAC/C,EAAA,MAAM,IAAA,GAAO,UAAA,CAAW,MAAA,EAAQ,UAAA,IAAa;AAC7C,EAAA,IAAI,IAAA,EAAM,OAAO,CAAA,EAAG,MAAM,IAAI,IAAI,CAAA,CAAA;AAElC,EAAA,MAAM,MAAA,GAAS,KAAK,MAAA,EAAO,CAAE,SAAS,EAAE,CAAA,CAAE,MAAM,CAAC,CAAA;AACjD,EAAA,OAAO,CAAA,EAAG,MAAM,CAAA,CAAA,EAAI,IAAA,CAAK,GAAA,GAAM,QAAA,CAAS,EAAE,CAAC,CAAA,EAAG,MAAM,CAAA,CAAA;AACtD;AAEA,SAAS,SAAS,KAAA,EAAuB;AACvC,EAAA,OAAO,KAA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AAA;AAAA,MACA,UAAA,EAAY,EAAE,MAAA,EAAQ,wBAAA;AAAyB,KACjD;AAAA,IACA,YAAA,EAAc,UAAA;AAAA,IACd,OAAA,EAAS,WAAA,CAAY,KAAA,CAAM,OAAO;AAAA,GACpC;AAEA,EAAA,OAAO;AAAA,IACL,YAAY,QAAA,CAAS,UAAA,EAAY,WAAA,EAAa,oBAAA,EAAsB,gBAAgB,UAAU,CAAA;AAAA,IAC9F,UAAU,QAAA,CAAS,UAAA,EAAY,WAAA,EAAa,kBAAA,EAAoB,cAAc,UAAU;AAAA,GAC1F;AACF","file":"testing.cjs","sourcesContent":["import type {\n BrowserBehaviorEventInput,\n BrowserPageviewInput,\n CapturedTouchpoint,\n ClickIdentifiers,\n IdentifyProfile,\n JsonObject,\n JsonValue,\n} from \"./types\";\n\nconst MAX_PARAM_LENGTH = 2_048;\nconst MAX_ROUTE_LENGTH = 255;\nconst MAX_URL_LENGTH = 8_192;\nconst MAX_UTM_KEYS = 50;\nconst MAX_VALUES_PER_PARAM = 10;\nconst UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\nconst EVENT_TYPE_PATTERN = /^[a-z][a-z0-9_.-]{0,127}$/;\nconst TIMEZONE_SUFFIX_PATTERN = /(?:z|[+-]\\d{2}:\\d{2})$/i;\nconst MAX_EVENT_ID_LENGTH = 255;\nconst MAX_EVENT_PROPERTIES_LENGTH = 32_768;\nconst MAX_PROFILE_ID_LENGTH = 255;\nconst PROFILE_ID_CONTROL_PATTERN = /[\\p{Cc}\\p{Cf}]/u;\n\nexport interface NormalizedEventFields {\n eventType: string;\n occurredAt: string;\n eventId?: string;\n value?: number;\n currency?: string;\n properties?: JsonObject;\n}\n\nexport interface NormalizedBehaviorEventFields {\n eventName: string;\n occurredAt: string;\n eventId?: string;\n pageUrl?: string;\n path?: string;\n title?: string;\n referrer?: string | null;\n properties?: JsonObject;\n}\n\nexport interface NormalizedPageviewFields\n extends Omit<NormalizedBehaviorEventFields, \"eventName\"> {\n eventName: \"pageview\";\n}\n\nexport function nowIso(): string {\n return new Date().toISOString();\n}\n\nexport function randomId(prefix: string): string {\n const uuid = globalThis.crypto?.randomUUID?.();\n if (uuid) return `${prefix}_${uuid}`;\n\n const random = Math.random().toString(36).slice(2);\n return `${prefix}_${Date.now().toString(36)}${random}`;\n}\n\nfunction truncate(value: string): string {\n return value.slice(0, MAX_PARAM_LENGTH);\n}\n\nfunction unique(values: string[]): string[] {\n return [...new Set(values.filter(Boolean).map(truncate))];\n}\n\nfunction valuesFor(searchParams: URLSearchParams, wantedKey: string): string[] {\n const values: string[] = [];\n for (const [key, value] of searchParams.entries()) {\n if (key.toLowerCase() === wantedKey) values.push(value);\n }\n return unique(values).slice(0, MAX_VALUES_PER_PARAM);\n}\n\nexport function isUuid(value: string): boolean {\n return UUID_PATTERN.test(value);\n}\n\nexport function normalizeProfileId(value: string): string | null {\n const normalized = value.trim();\n if (\n !normalized ||\n [...normalized].length > MAX_PROFILE_ID_LENGTH ||\n PROFILE_ID_CONTROL_PATTERN.test(normalized)\n ) {\n return null;\n }\n return normalized;\n}\n\nexport function safePageUrl(input: string): string {\n try {\n const url = new URL(input);\n return `${url.origin}${url.pathname}`.slice(0, MAX_URL_LENGTH);\n } catch {\n return \"\";\n }\n}\n\nexport function safeReferrer(input: string): string | null {\n if (!input) return null;\n const safe = safePageUrl(input);\n return safe || null;\n}\n\nexport function captureTouchpoint(\n location: Pick<Location, \"href\">,\n referrer: string,\n capturedAt = nowIso(),\n): CapturedTouchpoint {\n const url = new URL(location.href);\n const clickIds: ClickIdentifiers = {\n gclid: valuesFor(url.searchParams, \"gclid\"),\n gbraid: valuesFor(url.searchParams, \"gbraid\"),\n wbraid: valuesFor(url.searchParams, \"wbraid\"),\n };\n const utm: Record<string, string[]> = {};\n\n for (const key of [...new Set([...url.searchParams.keys()].map((key) => key.toLowerCase()))]\n .filter((candidate) => candidate.startsWith(\"utm_\"))\n .slice(0, MAX_UTM_KEYS)) {\n utm[key] = valuesFor(url.searchParams, key);\n }\n\n return {\n ahRoute: valuesFor(url.searchParams, \"ah_route\")[0]?.slice(0, MAX_ROUTE_LENGTH) ?? null,\n clickIds,\n utm,\n landingUrl: safePageUrl(url.href),\n referrer: safeReferrer(referrer),\n // Browsers do not know Google's server-side click timestamp. This is the\n // first-party observation time and is the upper bound available to the SDK.\n clickTime: capturedAt,\n };\n}\n\nexport function toJsonValue(value: unknown): JsonValue | undefined {\n if (value === undefined) return undefined;\n try {\n return JSON.parse(JSON.stringify(value)) as JsonValue;\n } catch {\n return undefined;\n }\n}\n\ninterface EventFieldsInput {\n eventType: string;\n eventId?: string;\n occurredAt?: string;\n value?: number;\n currency?: string;\n properties?: Record<string, unknown>;\n}\n\nexport function normalizeEventFields(input: EventFieldsInput): NormalizedEventFields {\n const eventType = input.eventType?.trim().toLowerCase();\n if (!eventType || (!EVENT_TYPE_PATTERN.test(eventType) && !isUuid(eventType))) {\n throw new Error(\n \"AdHub eventType must be an Event Definition UUID or event key.\",\n );\n }\n\n const occurredAtInput = input.occurredAt?.trim() || nowIso();\n if (!TIMEZONE_SUFFIX_PATTERN.test(occurredAtInput)) {\n throw new Error(\"AdHub occurredAt must include a timezone offset.\");\n }\n const occurredAtDate = new Date(occurredAtInput);\n if (Number.isNaN(occurredAtDate.getTime())) {\n throw new Error(\"AdHub occurredAt must be a valid ISO-8601 timestamp.\");\n }\n\n const result: NormalizedEventFields = {\n eventType,\n occurredAt: occurredAtDate.toISOString(),\n };\n\n if (input.eventId !== undefined) {\n const eventId = input.eventId.trim();\n if (\n !eventId ||\n eventId.length > MAX_EVENT_ID_LENGTH ||\n [...eventId].some((character) => character.charCodeAt(0) < 32 || character === \"\\u007f\")\n ) {\n throw new Error(\"AdHub eventId must be 1-255 characters without control characters.\");\n }\n result.eventId = eventId;\n }\n\n if (input.value !== undefined) {\n if (!Number.isFinite(input.value) || input.value < 0) {\n throw new Error(\"AdHub event value must be a finite non-negative number.\");\n }\n result.value = input.value;\n }\n\n if (input.currency !== undefined) {\n const currency = input.currency.trim().toUpperCase();\n if (!/^[A-Z]{3}$/.test(currency)) {\n throw new Error(\"AdHub event currency must be a three-letter ISO 4217 code.\");\n }\n result.currency = currency;\n }\n\n if (input.properties !== undefined) {\n const properties = toJsonValue(input.properties);\n if (\n !properties ||\n typeof properties !== \"object\" ||\n Array.isArray(properties) ||\n JSON.stringify(properties).length > MAX_EVENT_PROPERTIES_LENGTH\n ) {\n throw new Error(\"AdHub event properties must be a JSON object no larger than 32 KiB.\");\n }\n result.properties = properties;\n }\n\n return result;\n}\n\nexport function normalizeBehaviorEventFields(\n input: BrowserBehaviorEventInput,\n): NormalizedBehaviorEventFields {\n const eventName = input.eventName?.trim().toLowerCase();\n if (!eventName || !EVENT_TYPE_PATTERN.test(eventName)) {\n throw new Error(\n \"AdHub behavior eventName must start with a letter and use letters, digits, _, . or -.\",\n );\n }\n\n const occurredAtInput = input.occurredAt?.trim() || nowIso();\n if (!TIMEZONE_SUFFIX_PATTERN.test(occurredAtInput)) {\n throw new Error(\"AdHub behavior occurredAt must include a timezone offset.\");\n }\n const occurredAtDate = new Date(occurredAtInput);\n if (Number.isNaN(occurredAtDate.getTime())) {\n throw new Error(\"AdHub behavior occurredAt must be a valid ISO-8601 timestamp.\");\n }\n\n const result: NormalizedBehaviorEventFields = {\n eventName,\n occurredAt: occurredAtDate.toISOString(),\n };\n\n if (input.eventId !== undefined) {\n const eventId = input.eventId.trim();\n if (\n !eventId ||\n eventId.length > MAX_EVENT_ID_LENGTH ||\n [...eventId].some((character) => character.charCodeAt(0) < 32 || character === \"\\u007f\")\n ) {\n throw new Error(\"AdHub behavior eventId must be 1-255 characters without control characters.\");\n }\n result.eventId = eventId;\n }\n\n if (input.pageUrl !== undefined) {\n const pageUrl = safePageUrl(input.pageUrl);\n if (pageUrl) result.pageUrl = pageUrl;\n }\n if (input.path !== undefined) {\n const path = input.path.trim().slice(0, 2_048);\n if (path) result.path = path;\n }\n if (input.title !== undefined) {\n result.title = input.title.trim().slice(0, 512);\n }\n if (input.referrer !== undefined) {\n result.referrer = input.referrer === null ? null : safeReferrer(input.referrer);\n }\n\n if (input.properties !== undefined) {\n const properties = toJsonValue(input.properties);\n if (\n !properties ||\n typeof properties !== \"object\" ||\n Array.isArray(properties) ||\n JSON.stringify(properties).length > MAX_EVENT_PROPERTIES_LENGTH\n ) {\n throw new Error(\"AdHub behavior properties must be a JSON object no larger than 32 KiB.\");\n }\n result.properties = properties;\n }\n\n return result;\n}\n\nexport function normalizePageviewFields(input: BrowserPageviewInput = {}): NormalizedPageviewFields {\n return {\n ...normalizeBehaviorEventFields({ eventName: \"pageview\", ...input }),\n eventName: \"pageview\",\n };\n}\n\nexport function sanitizeProfile(profile: IdentifyProfile): IdentifyProfile | null {\n const profileId = normalizeProfileId(profile.profileId);\n if (!profileId) return null;\n\n const result: IdentifyProfile = { profileId };\n if (profile.email?.trim()) result.email = profile.email.trim().slice(0, 320);\n if (profile.firstName?.trim()) result.firstName = profile.firstName.trim().slice(0, 200);\n if (profile.lastName?.trim()) result.lastName = profile.lastName.trim().slice(0, 200);\n if (profile.properties) {\n const properties = toJsonValue(profile.properties);\n if (\n properties &&\n typeof properties === \"object\" &&\n !Array.isArray(properties) &&\n JSON.stringify(properties).length <= 32_768\n ) {\n result.properties = properties;\n }\n }\n return result;\n}\n","import type {\n AttributionTestEnvelopeInput,\n AttributionTestEnvelopes,\n IdentifyData,\n SdkEnvelope,\n TouchpointData,\n} from \"./types\";\n\nexport type {\n AttributionTestClickIdType,\n AttributionTestEnvelopeInput,\n AttributionTestEnvelopes,\n} from \"./types\";\nimport {\n captureTouchpoint,\n isUuid,\n normalizeProfileId,\n nowIso,\n randomId,\n safePageUrl,\n} from \"./utils\";\n\nconst CLICK_ID_TYPES = new Set([\"gclid\", \"gbraid\", \"wbraid\"]);\n\nfunction envelope<TType extends string, TData>(\n projectKey: string,\n anonymousId: string,\n type: TType,\n data: TData,\n sentAt: string,\n): SdkEnvelope<TType, TData> {\n return {\n schemaVersion: \"1.0\",\n type,\n requestId: randomId(\"req_test\"),\n sentAt,\n projectKey,\n anonymousId,\n data,\n };\n}\n\nexport function createAttributionTestEnvelopes(\n input: AttributionTestEnvelopeInput,\n): AttributionTestEnvelopes {\n const projectKey = input.projectKey.trim();\n const attributionRouteKey = input.attributionRouteKey.trim();\n const clickId = input.clickId.trim();\n const profileId = normalizeProfileId(input.profileId);\n if (!isUuid(projectKey)) {\n throw new Error(\"Attribution test requires a UUID project identifier.\");\n }\n if (!isUuid(attributionRouteKey)) {\n throw new Error(\"Attribution test requires a UUID attribution route identifier.\");\n }\n if (!profileId) {\n throw new Error(\n \"Attribution test profileId must be 1-255 characters without control characters.\",\n );\n }\n if (!CLICK_ID_TYPES.has(input.clickIdType) || !clickId || clickId.length > 2_048 || /\\s/.test(clickId)) {\n throw new Error(\"Attribution test requires one valid Google click ID.\");\n }\n\n const capturedAt = input.capturedAt ?? nowIso();\n const testUrl = new URL(input.pageUrl);\n const trackingKeys = new Set([\"ah_route\", \"gclid\", \"gbraid\", \"wbraid\"]);\n for (const key of [...testUrl.searchParams.keys()]) {\n if (trackingKeys.has(key.toLowerCase())) testUrl.searchParams.delete(key);\n }\n testUrl.searchParams.set(\"ah_route\", attributionRouteKey);\n testUrl.searchParams.set(input.clickIdType, clickId);\n const captured = captureTouchpoint(\n { href: testUrl.toString() } as Location,\n input.referrer ?? \"\",\n capturedAt,\n );\n const anonymousId = randomId(\"anon_test\");\n const sessionId = randomId(\"sess_test\");\n const touchpointData: TouchpointData = {\n ...captured,\n sessionId,\n locale: input.locale ?? null,\n timezone: input.timezone ?? null,\n };\n const identityData: IdentifyData = {\n profile: {\n profileId,\n properties: { source: \"adhub_attribution_test\" },\n },\n identifiedAt: capturedAt,\n pageUrl: safePageUrl(input.pageUrl),\n };\n\n return {\n touchpoint: envelope(projectKey, anonymousId, \"touchpoint.capture\", touchpointData, capturedAt),\n identity: envelope(projectKey, anonymousId, \"profile.identify\", identityData, capturedAt),\n };\n}\n"]}
|
package/dist/testing.d.cts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { A as AttributionTestEnvelopeInput, a as AttributionTestEnvelopes } from './types-DMUJO8Kj.cjs';
|
|
2
|
-
export { b as AttributionTestClickIdType } from './types-DMUJO8Kj.cjs';
|
|
3
|
-
|
|
4
|
-
declare function createAttributionTestEnvelopes(input: AttributionTestEnvelopeInput): AttributionTestEnvelopes;
|
|
5
|
-
|
|
6
|
-
export { AttributionTestEnvelopeInput, AttributionTestEnvelopes, createAttributionTestEnvelopes };
|
package/dist/testing.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { A as AttributionTestEnvelopeInput, a as AttributionTestEnvelopes } from './types-DMUJO8Kj.js';
|
|
2
|
-
export { b as AttributionTestClickIdType } from './types-DMUJO8Kj.js';
|
|
3
|
-
|
|
4
|
-
declare function createAttributionTestEnvelopes(input: AttributionTestEnvelopeInput): AttributionTestEnvelopes;
|
|
5
|
-
|
|
6
|
-
export { AttributionTestEnvelopeInput, AttributionTestEnvelopes, createAttributionTestEnvelopes };
|
package/dist/testing.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { normalizeProfileId, isUuid, nowIso, captureTouchpoint, randomId, safePageUrl } from './chunk-NF56G46G.js';
|
|
2
|
-
|
|
3
|
-
// src/testing.ts
|
|
4
|
-
var CLICK_ID_TYPES = /* @__PURE__ */ new Set(["gclid", "gbraid", "wbraid"]);
|
|
5
|
-
function envelope(projectKey, anonymousId, type, data, sentAt) {
|
|
6
|
-
return {
|
|
7
|
-
schemaVersion: "1.0",
|
|
8
|
-
type,
|
|
9
|
-
requestId: randomId("req_test"),
|
|
10
|
-
sentAt,
|
|
11
|
-
projectKey,
|
|
12
|
-
anonymousId,
|
|
13
|
-
data
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
function createAttributionTestEnvelopes(input) {
|
|
17
|
-
const projectKey = input.projectKey.trim();
|
|
18
|
-
const attributionRouteKey = input.attributionRouteKey.trim();
|
|
19
|
-
const clickId = input.clickId.trim();
|
|
20
|
-
const profileId = normalizeProfileId(input.profileId);
|
|
21
|
-
if (!isUuid(projectKey)) {
|
|
22
|
-
throw new Error("Attribution test requires a UUID project identifier.");
|
|
23
|
-
}
|
|
24
|
-
if (!isUuid(attributionRouteKey)) {
|
|
25
|
-
throw new Error("Attribution test requires a UUID attribution route identifier.");
|
|
26
|
-
}
|
|
27
|
-
if (!profileId) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
"Attribution test profileId must be 1-255 characters without control characters."
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
if (!CLICK_ID_TYPES.has(input.clickIdType) || !clickId || clickId.length > 2048 || /\s/.test(clickId)) {
|
|
33
|
-
throw new Error("Attribution test requires one valid Google click ID.");
|
|
34
|
-
}
|
|
35
|
-
const capturedAt = input.capturedAt ?? nowIso();
|
|
36
|
-
const testUrl = new URL(input.pageUrl);
|
|
37
|
-
const trackingKeys = /* @__PURE__ */ new Set(["ah_route", "gclid", "gbraid", "wbraid"]);
|
|
38
|
-
for (const key of [...testUrl.searchParams.keys()]) {
|
|
39
|
-
if (trackingKeys.has(key.toLowerCase())) testUrl.searchParams.delete(key);
|
|
40
|
-
}
|
|
41
|
-
testUrl.searchParams.set("ah_route", attributionRouteKey);
|
|
42
|
-
testUrl.searchParams.set(input.clickIdType, clickId);
|
|
43
|
-
const captured = captureTouchpoint(
|
|
44
|
-
{ href: testUrl.toString() },
|
|
45
|
-
input.referrer ?? "",
|
|
46
|
-
capturedAt
|
|
47
|
-
);
|
|
48
|
-
const anonymousId = randomId("anon_test");
|
|
49
|
-
const sessionId = randomId("sess_test");
|
|
50
|
-
const touchpointData = {
|
|
51
|
-
...captured,
|
|
52
|
-
sessionId,
|
|
53
|
-
locale: input.locale ?? null,
|
|
54
|
-
timezone: input.timezone ?? null
|
|
55
|
-
};
|
|
56
|
-
const identityData = {
|
|
57
|
-
profile: {
|
|
58
|
-
profileId,
|
|
59
|
-
properties: { source: "adhub_attribution_test" }
|
|
60
|
-
},
|
|
61
|
-
identifiedAt: capturedAt,
|
|
62
|
-
pageUrl: safePageUrl(input.pageUrl)
|
|
63
|
-
};
|
|
64
|
-
return {
|
|
65
|
-
touchpoint: envelope(projectKey, anonymousId, "touchpoint.capture", touchpointData, capturedAt),
|
|
66
|
-
identity: envelope(projectKey, anonymousId, "profile.identify", identityData, capturedAt)
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export { createAttributionTestEnvelopes };
|
|
71
|
-
//# sourceMappingURL=testing.js.map
|
|
72
|
-
//# sourceMappingURL=testing.js.map
|
package/dist/testing.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/testing.ts"],"names":[],"mappings":";;;AAsBA,IAAM,iCAAiB,IAAI,GAAA,CAAI,CAAC,OAAA,EAAS,QAAA,EAAU,QAAQ,CAAC,CAAA;AAE5D,SAAS,QAAA,CACP,UAAA,EACA,WAAA,EACA,IAAA,EACA,MACA,MAAA,EAC2B;AAC3B,EAAA,OAAO;AAAA,IACL,aAAA,EAAe,KAAA;AAAA,IACf,IAAA;AAAA,IACA,SAAA,EAAW,SAAS,UAAU,CAAA;AAAA,IAC9B,MAAA;AAAA,IACA,UAAA;AAAA,IACA,WAAA;AAAA,IACA;AAAA,GACF;AACF;AAEO,SAAS,+BACd,KAAA,EAC0B;AAC1B,EAAA,MAAM,UAAA,GAAa,KAAA,CAAM,UAAA,CAAW,IAAA,EAAK;AACzC,EAAA,MAAM,mBAAA,GAAsB,KAAA,CAAM,mBAAA,CAAoB,IAAA,EAAK;AAC3D,EAAA,MAAM,OAAA,GAAU,KAAA,CAAM,OAAA,CAAQ,IAAA,EAAK;AACnC,EAAA,MAAM,SAAA,GAAY,kBAAA,CAAmB,KAAA,CAAM,SAAS,CAAA;AACpD,EAAA,IAAI,C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AA,EAAS,EAAE;AAAA,IAC3B,MAAM,QAAA,IAAY,EAAA;AAAA,IAClB;AAAA,GACF;AACA,EAAA,MAAM,WAAA,GAAc,SAAS,WAAW,CAAA;AACxC,EAAA,MAAM,SAAA,GAAY,SAAS,WAAW,CAAA;AACtC,EAAA,MAAM,cAAA,GAAiC;AAAA,IACrC,GAAG,QAAA;AAAA,IACH,SAAA;AAAA,IACA,MAAA,EAAQ,MAAM,MAAA,IAAU,IAAA;AAAA,IACxB,QAAA,EAAU,MAAM,QAAA,IAAY;AAAA,GAC9B;AACA,EAAA,MAAM,YAAA,GAA6B;AAAA,IACjC,OAAA,EAAS;AAAA,MACP,SAAA;AAAA,MACA,UAAA,EAAY,EAAE,MAAA,EAAQ,wBAAA;AAAyB,KACjD;AAAA,IACA,YAAA,EAAc,UAAA;AAAA,IACd,OAAA,EAAS,WAAA,CAAY,KAAA,CAAM,OAAO;AAAA,GACpC;AAEA,EAAA,OAAO;AAAA,IACL,YAAY,QAAA,CAAS,UAAA,EAAY,WAAA,EAAa,oBAAA,EAAsB,gBAAgB,UAAU,CAAA;AAAA,IAC9F,UAAU,QAAA,CAAS,UAAA,EAAY,WAAA,EAAa,kBAAA,EAAoB,cAAc,UAAU;AAAA,GAC1F;AACF","file":"testing.js","sourcesContent":["import type {\n AttributionTestEnvelopeInput,\n AttributionTestEnvelopes,\n IdentifyData,\n SdkEnvelope,\n TouchpointData,\n} from \"./types\";\n\nexport type {\n AttributionTestClickIdType,\n AttributionTestEnvelopeInput,\n AttributionTestEnvelopes,\n} from \"./types\";\nimport {\n captureTouchpoint,\n isUuid,\n normalizeProfileId,\n nowIso,\n randomId,\n safePageUrl,\n} from \"./utils\";\n\nconst CLICK_ID_TYPES = new Set([\"gclid\", \"gbraid\", \"wbraid\"]);\n\nfunction envelope<TType extends string, TData>(\n projectKey: string,\n anonymousId: string,\n type: TType,\n data: TData,\n sentAt: string,\n): SdkEnvelope<TType, TData> {\n return {\n schemaVersion: \"1.0\",\n type,\n requestId: randomId(\"req_test\"),\n sentAt,\n projectKey,\n anonymousId,\n data,\n };\n}\n\nexport function createAttributionTestEnvelopes(\n input: AttributionTestEnvelopeInput,\n): AttributionTestEnvelopes {\n const projectKey = input.projectKey.trim();\n const attributionRouteKey = input.attributionRouteKey.trim();\n const clickId = input.clickId.trim();\n const profileId = normalizeProfileId(input.profileId);\n if (!isUuid(projectKey)) {\n throw new Error(\"Attribution test requires a UUID project identifier.\");\n }\n if (!isUuid(attributionRouteKey)) {\n throw new Error(\"Attribution test requires a UUID attribution route identifier.\");\n }\n if (!profileId) {\n throw new Error(\n \"Attribution test profileId must be 1-255 characters without control characters.\",\n );\n }\n if (!CLICK_ID_TYPES.has(input.clickIdType) || !clickId || clickId.length > 2_048 || /\\s/.test(clickId)) {\n throw new Error(\"Attribution test requires one valid Google click ID.\");\n }\n\n const capturedAt = input.capturedAt ?? nowIso();\n const testUrl = new URL(input.pageUrl);\n const trackingKeys = new Set([\"ah_route\", \"gclid\", \"gbraid\", \"wbraid\"]);\n for (const key of [...testUrl.searchParams.keys()]) {\n if (trackingKeys.has(key.toLowerCase())) testUrl.searchParams.delete(key);\n }\n testUrl.searchParams.set(\"ah_route\", attributionRouteKey);\n testUrl.searchParams.set(input.clickIdType, clickId);\n const captured = captureTouchpoint(\n { href: testUrl.toString() } as Location,\n input.referrer ?? \"\",\n capturedAt,\n );\n const anonymousId = randomId(\"anon_test\");\n const sessionId = randomId(\"sess_test\");\n const touchpointData: TouchpointData = {\n ...captured,\n sessionId,\n locale: input.locale ?? null,\n timezone: input.timezone ?? null,\n };\n const identityData: IdentifyData = {\n profile: {\n profileId,\n properties: { source: \"adhub_attribution_test\" },\n },\n identifiedAt: capturedAt,\n pageUrl: safePageUrl(input.pageUrl),\n };\n\n return {\n touchpoint: envelope(projectKey, anonymousId, \"touchpoint.capture\", touchpointData, capturedAt),\n identity: envelope(projectKey, anonymousId, \"profile.identify\", identityData, capturedAt),\n };\n}\n"]}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
type JsonPrimitive = string | number | boolean | null;
|
|
2
|
-
type JsonObject = {
|
|
3
|
-
[key: string]: JsonValue;
|
|
4
|
-
};
|
|
5
|
-
type JsonValue = JsonPrimitive | JsonValue[] | JsonObject;
|
|
6
|
-
interface ClickIdentifiers {
|
|
7
|
-
gclid: string[];
|
|
8
|
-
gbraid: string[];
|
|
9
|
-
wbraid: string[];
|
|
10
|
-
}
|
|
11
|
-
interface CapturedTouchpoint {
|
|
12
|
-
ahRoute: string | null;
|
|
13
|
-
clickIds: ClickIdentifiers;
|
|
14
|
-
utm: Record<string, string[]>;
|
|
15
|
-
landingUrl: string;
|
|
16
|
-
referrer: string | null;
|
|
17
|
-
clickTime: string;
|
|
18
|
-
}
|
|
19
|
-
interface SdkEnvelope<TType extends string, TData> {
|
|
20
|
-
schemaVersion: "1.0";
|
|
21
|
-
type: TType;
|
|
22
|
-
requestId: string;
|
|
23
|
-
sentAt: string;
|
|
24
|
-
projectKey: string;
|
|
25
|
-
anonymousId: string;
|
|
26
|
-
data: TData;
|
|
27
|
-
}
|
|
28
|
-
interface TouchpointData extends CapturedTouchpoint {
|
|
29
|
-
sessionId: string;
|
|
30
|
-
locale: string | null;
|
|
31
|
-
timezone: string | null;
|
|
32
|
-
}
|
|
33
|
-
interface IdentifyData {
|
|
34
|
-
profile: {
|
|
35
|
-
profileId: string;
|
|
36
|
-
email?: string;
|
|
37
|
-
firstName?: string;
|
|
38
|
-
lastName?: string;
|
|
39
|
-
properties?: JsonValue;
|
|
40
|
-
};
|
|
41
|
-
identifiedAt: string;
|
|
42
|
-
pageUrl: string;
|
|
43
|
-
}
|
|
44
|
-
type AttributionTestClickIdType = keyof ClickIdentifiers;
|
|
45
|
-
interface AttributionTestEnvelopeInput {
|
|
46
|
-
projectKey: string;
|
|
47
|
-
attributionRouteKey: string;
|
|
48
|
-
clickIdType: AttributionTestClickIdType;
|
|
49
|
-
clickId: string;
|
|
50
|
-
profileId: string;
|
|
51
|
-
pageUrl: string;
|
|
52
|
-
referrer?: string | null;
|
|
53
|
-
capturedAt?: string;
|
|
54
|
-
locale?: string | null;
|
|
55
|
-
timezone?: string | null;
|
|
56
|
-
}
|
|
57
|
-
interface AttributionTestEnvelopes {
|
|
58
|
-
touchpoint: SdkEnvelope<"touchpoint.capture", TouchpointData>;
|
|
59
|
-
identity: SdkEnvelope<"profile.identify", IdentifyData>;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type { AttributionTestEnvelopeInput as A, JsonObject as J, AttributionTestEnvelopes as a, AttributionTestClickIdType as b };
|
package/dist/types-DMUJO8Kj.d.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
type JsonPrimitive = string | number | boolean | null;
|
|
2
|
-
type JsonObject = {
|
|
3
|
-
[key: string]: JsonValue;
|
|
4
|
-
};
|
|
5
|
-
type JsonValue = JsonPrimitive | JsonValue[] | JsonObject;
|
|
6
|
-
interface ClickIdentifiers {
|
|
7
|
-
gclid: string[];
|
|
8
|
-
gbraid: string[];
|
|
9
|
-
wbraid: string[];
|
|
10
|
-
}
|
|
11
|
-
interface CapturedTouchpoint {
|
|
12
|
-
ahRoute: string | null;
|
|
13
|
-
clickIds: ClickIdentifiers;
|
|
14
|
-
utm: Record<string, string[]>;
|
|
15
|
-
landingUrl: string;
|
|
16
|
-
referrer: string | null;
|
|
17
|
-
clickTime: string;
|
|
18
|
-
}
|
|
19
|
-
interface SdkEnvelope<TType extends string, TData> {
|
|
20
|
-
schemaVersion: "1.0";
|
|
21
|
-
type: TType;
|
|
22
|
-
requestId: string;
|
|
23
|
-
sentAt: string;
|
|
24
|
-
projectKey: string;
|
|
25
|
-
anonymousId: string;
|
|
26
|
-
data: TData;
|
|
27
|
-
}
|
|
28
|
-
interface TouchpointData extends CapturedTouchpoint {
|
|
29
|
-
sessionId: string;
|
|
30
|
-
locale: string | null;
|
|
31
|
-
timezone: string | null;
|
|
32
|
-
}
|
|
33
|
-
interface IdentifyData {
|
|
34
|
-
profile: {
|
|
35
|
-
profileId: string;
|
|
36
|
-
email?: string;
|
|
37
|
-
firstName?: string;
|
|
38
|
-
lastName?: string;
|
|
39
|
-
properties?: JsonValue;
|
|
40
|
-
};
|
|
41
|
-
identifiedAt: string;
|
|
42
|
-
pageUrl: string;
|
|
43
|
-
}
|
|
44
|
-
type AttributionTestClickIdType = keyof ClickIdentifiers;
|
|
45
|
-
interface AttributionTestEnvelopeInput {
|
|
46
|
-
projectKey: string;
|
|
47
|
-
attributionRouteKey: string;
|
|
48
|
-
clickIdType: AttributionTestClickIdType;
|
|
49
|
-
clickId: string;
|
|
50
|
-
profileId: string;
|
|
51
|
-
pageUrl: string;
|
|
52
|
-
referrer?: string | null;
|
|
53
|
-
capturedAt?: string;
|
|
54
|
-
locale?: string | null;
|
|
55
|
-
timezone?: string | null;
|
|
56
|
-
}
|
|
57
|
-
interface AttributionTestEnvelopes {
|
|
58
|
-
touchpoint: SdkEnvelope<"touchpoint.capture", TouchpointData>;
|
|
59
|
-
identity: SdkEnvelope<"profile.identify", IdentifyData>;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type { AttributionTestEnvelopeInput as A, JsonObject as J, AttributionTestEnvelopes as a, AttributionTestClickIdType as b };
|