@atsignal/browser-plugin-enrich-context 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 NETHRU Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # `@atsignal/browser-plugin-enrich-context`
2
+
3
+ Browser plugin that enriches Signal events with browser environment and attribution context.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @atsignal/browser @atsignal/browser-plugin-enrich-context
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import signal from "@atsignal/browser";
15
+ import "@atsignal/browser-plugin-enrich-context";
16
+
17
+ await signal.init("your-api-key", {
18
+ persistQueue: false,
19
+ plugins: [signal.plugins.enrichContext()],
20
+ });
21
+ ```
22
+
23
+ Load the browser global base bundle first, then the plugin global bundle if you are using script tags.
24
+
25
+ More SDK details live in the monorepo root README.
package/dist/index.cjs ADDED
@@ -0,0 +1,301 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ enrichContext: () => enrichContext
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+ var import_browser = require("@atsignal/browser");
27
+
28
+ // src/factory.ts
29
+ var ENV_NAMESPACE = "@env";
30
+ var ATTR_NAMESPACE = "@attr";
31
+ var EventContext = class {
32
+ document() {
33
+ var _a;
34
+ if (this._document !== void 0) {
35
+ return (_a = this._document) != null ? _a : void 0;
36
+ }
37
+ const documentValue = typeof document === "undefined" ? null : document;
38
+ this._document = documentValue;
39
+ return documentValue != null ? documentValue : void 0;
40
+ }
41
+ location() {
42
+ var _a;
43
+ if (this._location !== void 0) {
44
+ return (_a = this._location) != null ? _a : void 0;
45
+ }
46
+ const locationValue = typeof location === "undefined" ? null : location;
47
+ this._location = locationValue;
48
+ return locationValue != null ? locationValue : void 0;
49
+ }
50
+ searchParams() {
51
+ var _a;
52
+ if (this._searchParams !== void 0) {
53
+ return this._searchParams;
54
+ }
55
+ const search = normalizeOptionalString((_a = this.location()) == null ? void 0 : _a.search);
56
+ this._searchParams = search ? new URLSearchParams(search) : null;
57
+ return this._searchParams;
58
+ }
59
+ };
60
+ var ENV_STATIC_COLLECTORS = Object.freeze({
61
+ language: getLanguage,
62
+ platform: getPlatform
63
+ });
64
+ var ENV_DYNAMIC_COLLECTORS = Object.freeze({
65
+ current_url: (context) => {
66
+ var _a;
67
+ return normalizeOptionalString((_a = context.location()) == null ? void 0 : _a.href);
68
+ },
69
+ current_path: (context) => {
70
+ var _a;
71
+ return normalizeOptionalString((_a = context.location()) == null ? void 0 : _a.pathname);
72
+ },
73
+ current_title: (context) => {
74
+ var _a;
75
+ return normalizeOptionalString((_a = context.document()) == null ? void 0 : _a.title);
76
+ },
77
+ current_hostname: (context) => {
78
+ var _a;
79
+ return normalizeOptionalString((_a = context.location()) == null ? void 0 : _a.hostname);
80
+ }
81
+ });
82
+ var ATTR_QUERY_PARAMETER_KEYS = Object.freeze([
83
+ "utm_source",
84
+ "utm_medium",
85
+ "utm_campaign",
86
+ "utm_term",
87
+ "utm_content",
88
+ "utm_id",
89
+ "utm_source_platform",
90
+ "utm_campaign_id",
91
+ "utm_creative_format",
92
+ "utm_marketing_tactic",
93
+ "gclid",
94
+ "dclid",
95
+ "fbclid",
96
+ "ko_click_id",
97
+ "li_fat_id",
98
+ "msclkid",
99
+ "ttclid",
100
+ "twclid",
101
+ "wbraid",
102
+ "gbraid",
103
+ "rdt_cid",
104
+ "sccid"
105
+ ]);
106
+ function createAttrDynamicCollectors() {
107
+ const collectors = /* @__PURE__ */ Object.create(null);
108
+ for (const key of ATTR_QUERY_PARAMETER_KEYS) {
109
+ collectors[key] = (context) => {
110
+ var _a;
111
+ return normalizeOptionalString((_a = context.searchParams()) == null ? void 0 : _a.get(key));
112
+ };
113
+ }
114
+ return Object.freeze(collectors);
115
+ }
116
+ var ATTR_DYNAMIC_COLLECTORS = createAttrDynamicCollectors();
117
+ function normalizeOptionalString(value) {
118
+ if (typeof value !== "string" || value.length === 0) {
119
+ return void 0;
120
+ }
121
+ return value;
122
+ }
123
+ function getNavigator() {
124
+ return typeof navigator === "undefined" ? void 0 : navigator;
125
+ }
126
+ function getLanguage() {
127
+ var _a;
128
+ return normalizeOptionalString((_a = getNavigator()) == null ? void 0 : _a.language);
129
+ }
130
+ function getPlatform() {
131
+ var _a, _b;
132
+ const currentNavigator = getNavigator();
133
+ return (_b = normalizeOptionalString((_a = currentNavigator == null ? void 0 : currentNavigator.userAgentData) == null ? void 0 : _a.platform)) != null ? _b : normalizeOptionalString(currentNavigator == null ? void 0 : currentNavigator.platform);
134
+ }
135
+ function getReferrer() {
136
+ return normalizeOptionalString(
137
+ typeof document === "undefined" ? void 0 : document.referrer
138
+ );
139
+ }
140
+ function getReferringHostFromReferrer(referrer) {
141
+ if (!referrer) {
142
+ return void 0;
143
+ }
144
+ try {
145
+ return normalizeOptionalString(new URL(referrer).hostname);
146
+ } catch (e) {
147
+ return void 0;
148
+ }
149
+ }
150
+ function buildAttrStaticEntries(blockedKeys) {
151
+ if (blockedKeys.has("referrer") && blockedKeys.has("referring_host")) {
152
+ return [];
153
+ }
154
+ const values = [];
155
+ const referrer = getReferrer();
156
+ if (!blockedKeys.has("referrer") && referrer !== void 0) {
157
+ values.push(["referrer", referrer]);
158
+ }
159
+ if (!blockedKeys.has("referring_host")) {
160
+ const referringHost = getReferringHostFromReferrer(referrer);
161
+ if (referringHost !== void 0) {
162
+ values.push(["referring_host", referringHost]);
163
+ }
164
+ }
165
+ return values;
166
+ }
167
+ function parseBlockedNamespace(blockedProperties, namespace) {
168
+ const blockedKeys = /* @__PURE__ */ new Set();
169
+ const prefix = `${namespace}/`;
170
+ let namespaceBlocked = false;
171
+ for (const blockedProperty of blockedProperties) {
172
+ if (blockedProperty === namespace) {
173
+ namespaceBlocked = true;
174
+ continue;
175
+ }
176
+ if (!blockedProperty.startsWith(prefix)) {
177
+ continue;
178
+ }
179
+ const blockedKey = blockedProperty.slice(prefix.length);
180
+ if (blockedKey.length > 0) {
181
+ blockedKeys.add(blockedKey);
182
+ }
183
+ }
184
+ return {
185
+ namespaceBlocked,
186
+ blockedKeys
187
+ };
188
+ }
189
+ function buildStaticEntries(collectors, blockedKeys) {
190
+ const values = [];
191
+ for (const [key, collect] of Object.entries(collectors)) {
192
+ if (blockedKeys.has(key)) {
193
+ continue;
194
+ }
195
+ const value = collect();
196
+ if (value === void 0) {
197
+ continue;
198
+ }
199
+ values.push([key, value]);
200
+ }
201
+ return values;
202
+ }
203
+ function buildDynamicEntries(collectors, blockedKeys) {
204
+ const values = [];
205
+ for (const [key, collect] of Object.entries(collectors)) {
206
+ if (blockedKeys.has(key)) {
207
+ continue;
208
+ }
209
+ values.push([key, collect]);
210
+ }
211
+ return values;
212
+ }
213
+ function buildNamespaceConfig(namespace, blockedProperties, staticCollectors, dynamicCollectors) {
214
+ const blockedNamespace = parseBlockedNamespace(blockedProperties, namespace);
215
+ if (blockedNamespace.namespaceBlocked) {
216
+ return null;
217
+ }
218
+ const staticValues = namespace === ATTR_NAMESPACE ? buildAttrStaticEntries(blockedNamespace.blockedKeys) : buildStaticEntries(staticCollectors, blockedNamespace.blockedKeys);
219
+ const dynamicValues = buildDynamicEntries(dynamicCollectors, blockedNamespace.blockedKeys);
220
+ if (staticValues.length === 0 && dynamicValues.length === 0) {
221
+ return null;
222
+ }
223
+ return {
224
+ namespace,
225
+ staticValues,
226
+ dynamicValues
227
+ };
228
+ }
229
+ function isPlainObject(value) {
230
+ if (!value || typeof value !== "object") {
231
+ return false;
232
+ }
233
+ const prototype = Object.getPrototypeOf(value);
234
+ return prototype === Object.prototype || prototype === null;
235
+ }
236
+ function mergeNamespaceEntries(event, config, context) {
237
+ if (!config) {
238
+ return;
239
+ }
240
+ const existingValue = event[config.namespace];
241
+ if (existingValue !== void 0 && !isPlainObject(existingValue)) {
242
+ return;
243
+ }
244
+ const target = existingValue != null ? existingValue : {};
245
+ if (!isPlainObject(target)) {
246
+ return;
247
+ }
248
+ let changed = false;
249
+ for (const [key, value] of config.staticValues) {
250
+ if (key in target) {
251
+ continue;
252
+ }
253
+ target[key] = value;
254
+ changed = true;
255
+ }
256
+ for (const [key, collect] of config.dynamicValues) {
257
+ if (key in target) {
258
+ continue;
259
+ }
260
+ const value = collect(context);
261
+ if (value === void 0) {
262
+ continue;
263
+ }
264
+ target[key] = value;
265
+ changed = true;
266
+ }
267
+ if (existingValue === void 0 && changed) {
268
+ event[config.namespace] = target;
269
+ }
270
+ }
271
+ function enrichContext() {
272
+ return {
273
+ name: "enrichContext",
274
+ setup: ({ hooks, config }) => {
275
+ const envConfig = buildNamespaceConfig(
276
+ ENV_NAMESPACE,
277
+ config.blockedProperties,
278
+ ENV_STATIC_COLLECTORS,
279
+ ENV_DYNAMIC_COLLECTORS
280
+ );
281
+ const attrConfig = buildNamespaceConfig(
282
+ ATTR_NAMESPACE,
283
+ config.blockedProperties,
284
+ {},
285
+ ATTR_DYNAMIC_COLLECTORS
286
+ );
287
+ hooks.onEventCreated(({ event }) => {
288
+ const context = new EventContext();
289
+ mergeNamespaceEntries(event, envConfig, context);
290
+ mergeNamespaceEntries(event, attrConfig, context);
291
+ });
292
+ }
293
+ };
294
+ }
295
+
296
+ // src/index.ts
297
+ (0, import_browser.registerBrowserPlugin)("enrichContext", enrichContext);
298
+ // Annotate the CommonJS export names for ESM import in node:
299
+ 0 && (module.exports = {
300
+ enrichContext
301
+ });
@@ -0,0 +1,5 @@
1
+ import { ClientPlugin } from '@atsignal/browser';
2
+
3
+ declare function enrichContext(): ClientPlugin;
4
+
5
+ export { enrichContext };
@@ -0,0 +1,5 @@
1
+ import { ClientPlugin } from '@atsignal/browser';
2
+
3
+ declare function enrichContext(): ClientPlugin;
4
+
5
+ export { enrichContext };
package/dist/index.js ADDED
@@ -0,0 +1,276 @@
1
+ // src/index.ts
2
+ import { registerBrowserPlugin } from "@atsignal/browser";
3
+
4
+ // src/factory.ts
5
+ var ENV_NAMESPACE = "@env";
6
+ var ATTR_NAMESPACE = "@attr";
7
+ var EventContext = class {
8
+ document() {
9
+ var _a;
10
+ if (this._document !== void 0) {
11
+ return (_a = this._document) != null ? _a : void 0;
12
+ }
13
+ const documentValue = typeof document === "undefined" ? null : document;
14
+ this._document = documentValue;
15
+ return documentValue != null ? documentValue : void 0;
16
+ }
17
+ location() {
18
+ var _a;
19
+ if (this._location !== void 0) {
20
+ return (_a = this._location) != null ? _a : void 0;
21
+ }
22
+ const locationValue = typeof location === "undefined" ? null : location;
23
+ this._location = locationValue;
24
+ return locationValue != null ? locationValue : void 0;
25
+ }
26
+ searchParams() {
27
+ var _a;
28
+ if (this._searchParams !== void 0) {
29
+ return this._searchParams;
30
+ }
31
+ const search = normalizeOptionalString((_a = this.location()) == null ? void 0 : _a.search);
32
+ this._searchParams = search ? new URLSearchParams(search) : null;
33
+ return this._searchParams;
34
+ }
35
+ };
36
+ var ENV_STATIC_COLLECTORS = Object.freeze({
37
+ language: getLanguage,
38
+ platform: getPlatform
39
+ });
40
+ var ENV_DYNAMIC_COLLECTORS = Object.freeze({
41
+ current_url: (context) => {
42
+ var _a;
43
+ return normalizeOptionalString((_a = context.location()) == null ? void 0 : _a.href);
44
+ },
45
+ current_path: (context) => {
46
+ var _a;
47
+ return normalizeOptionalString((_a = context.location()) == null ? void 0 : _a.pathname);
48
+ },
49
+ current_title: (context) => {
50
+ var _a;
51
+ return normalizeOptionalString((_a = context.document()) == null ? void 0 : _a.title);
52
+ },
53
+ current_hostname: (context) => {
54
+ var _a;
55
+ return normalizeOptionalString((_a = context.location()) == null ? void 0 : _a.hostname);
56
+ }
57
+ });
58
+ var ATTR_QUERY_PARAMETER_KEYS = Object.freeze([
59
+ "utm_source",
60
+ "utm_medium",
61
+ "utm_campaign",
62
+ "utm_term",
63
+ "utm_content",
64
+ "utm_id",
65
+ "utm_source_platform",
66
+ "utm_campaign_id",
67
+ "utm_creative_format",
68
+ "utm_marketing_tactic",
69
+ "gclid",
70
+ "dclid",
71
+ "fbclid",
72
+ "ko_click_id",
73
+ "li_fat_id",
74
+ "msclkid",
75
+ "ttclid",
76
+ "twclid",
77
+ "wbraid",
78
+ "gbraid",
79
+ "rdt_cid",
80
+ "sccid"
81
+ ]);
82
+ function createAttrDynamicCollectors() {
83
+ const collectors = /* @__PURE__ */ Object.create(null);
84
+ for (const key of ATTR_QUERY_PARAMETER_KEYS) {
85
+ collectors[key] = (context) => {
86
+ var _a;
87
+ return normalizeOptionalString((_a = context.searchParams()) == null ? void 0 : _a.get(key));
88
+ };
89
+ }
90
+ return Object.freeze(collectors);
91
+ }
92
+ var ATTR_DYNAMIC_COLLECTORS = createAttrDynamicCollectors();
93
+ function normalizeOptionalString(value) {
94
+ if (typeof value !== "string" || value.length === 0) {
95
+ return void 0;
96
+ }
97
+ return value;
98
+ }
99
+ function getNavigator() {
100
+ return typeof navigator === "undefined" ? void 0 : navigator;
101
+ }
102
+ function getLanguage() {
103
+ var _a;
104
+ return normalizeOptionalString((_a = getNavigator()) == null ? void 0 : _a.language);
105
+ }
106
+ function getPlatform() {
107
+ var _a, _b;
108
+ const currentNavigator = getNavigator();
109
+ return (_b = normalizeOptionalString((_a = currentNavigator == null ? void 0 : currentNavigator.userAgentData) == null ? void 0 : _a.platform)) != null ? _b : normalizeOptionalString(currentNavigator == null ? void 0 : currentNavigator.platform);
110
+ }
111
+ function getReferrer() {
112
+ return normalizeOptionalString(
113
+ typeof document === "undefined" ? void 0 : document.referrer
114
+ );
115
+ }
116
+ function getReferringHostFromReferrer(referrer) {
117
+ if (!referrer) {
118
+ return void 0;
119
+ }
120
+ try {
121
+ return normalizeOptionalString(new URL(referrer).hostname);
122
+ } catch (e) {
123
+ return void 0;
124
+ }
125
+ }
126
+ function buildAttrStaticEntries(blockedKeys) {
127
+ if (blockedKeys.has("referrer") && blockedKeys.has("referring_host")) {
128
+ return [];
129
+ }
130
+ const values = [];
131
+ const referrer = getReferrer();
132
+ if (!blockedKeys.has("referrer") && referrer !== void 0) {
133
+ values.push(["referrer", referrer]);
134
+ }
135
+ if (!blockedKeys.has("referring_host")) {
136
+ const referringHost = getReferringHostFromReferrer(referrer);
137
+ if (referringHost !== void 0) {
138
+ values.push(["referring_host", referringHost]);
139
+ }
140
+ }
141
+ return values;
142
+ }
143
+ function parseBlockedNamespace(blockedProperties, namespace) {
144
+ const blockedKeys = /* @__PURE__ */ new Set();
145
+ const prefix = `${namespace}/`;
146
+ let namespaceBlocked = false;
147
+ for (const blockedProperty of blockedProperties) {
148
+ if (blockedProperty === namespace) {
149
+ namespaceBlocked = true;
150
+ continue;
151
+ }
152
+ if (!blockedProperty.startsWith(prefix)) {
153
+ continue;
154
+ }
155
+ const blockedKey = blockedProperty.slice(prefix.length);
156
+ if (blockedKey.length > 0) {
157
+ blockedKeys.add(blockedKey);
158
+ }
159
+ }
160
+ return {
161
+ namespaceBlocked,
162
+ blockedKeys
163
+ };
164
+ }
165
+ function buildStaticEntries(collectors, blockedKeys) {
166
+ const values = [];
167
+ for (const [key, collect] of Object.entries(collectors)) {
168
+ if (blockedKeys.has(key)) {
169
+ continue;
170
+ }
171
+ const value = collect();
172
+ if (value === void 0) {
173
+ continue;
174
+ }
175
+ values.push([key, value]);
176
+ }
177
+ return values;
178
+ }
179
+ function buildDynamicEntries(collectors, blockedKeys) {
180
+ const values = [];
181
+ for (const [key, collect] of Object.entries(collectors)) {
182
+ if (blockedKeys.has(key)) {
183
+ continue;
184
+ }
185
+ values.push([key, collect]);
186
+ }
187
+ return values;
188
+ }
189
+ function buildNamespaceConfig(namespace, blockedProperties, staticCollectors, dynamicCollectors) {
190
+ const blockedNamespace = parseBlockedNamespace(blockedProperties, namespace);
191
+ if (blockedNamespace.namespaceBlocked) {
192
+ return null;
193
+ }
194
+ const staticValues = namespace === ATTR_NAMESPACE ? buildAttrStaticEntries(blockedNamespace.blockedKeys) : buildStaticEntries(staticCollectors, blockedNamespace.blockedKeys);
195
+ const dynamicValues = buildDynamicEntries(dynamicCollectors, blockedNamespace.blockedKeys);
196
+ if (staticValues.length === 0 && dynamicValues.length === 0) {
197
+ return null;
198
+ }
199
+ return {
200
+ namespace,
201
+ staticValues,
202
+ dynamicValues
203
+ };
204
+ }
205
+ function isPlainObject(value) {
206
+ if (!value || typeof value !== "object") {
207
+ return false;
208
+ }
209
+ const prototype = Object.getPrototypeOf(value);
210
+ return prototype === Object.prototype || prototype === null;
211
+ }
212
+ function mergeNamespaceEntries(event, config, context) {
213
+ if (!config) {
214
+ return;
215
+ }
216
+ const existingValue = event[config.namespace];
217
+ if (existingValue !== void 0 && !isPlainObject(existingValue)) {
218
+ return;
219
+ }
220
+ const target = existingValue != null ? existingValue : {};
221
+ if (!isPlainObject(target)) {
222
+ return;
223
+ }
224
+ let changed = false;
225
+ for (const [key, value] of config.staticValues) {
226
+ if (key in target) {
227
+ continue;
228
+ }
229
+ target[key] = value;
230
+ changed = true;
231
+ }
232
+ for (const [key, collect] of config.dynamicValues) {
233
+ if (key in target) {
234
+ continue;
235
+ }
236
+ const value = collect(context);
237
+ if (value === void 0) {
238
+ continue;
239
+ }
240
+ target[key] = value;
241
+ changed = true;
242
+ }
243
+ if (existingValue === void 0 && changed) {
244
+ event[config.namespace] = target;
245
+ }
246
+ }
247
+ function enrichContext() {
248
+ return {
249
+ name: "enrichContext",
250
+ setup: ({ hooks, config }) => {
251
+ const envConfig = buildNamespaceConfig(
252
+ ENV_NAMESPACE,
253
+ config.blockedProperties,
254
+ ENV_STATIC_COLLECTORS,
255
+ ENV_DYNAMIC_COLLECTORS
256
+ );
257
+ const attrConfig = buildNamespaceConfig(
258
+ ATTR_NAMESPACE,
259
+ config.blockedProperties,
260
+ {},
261
+ ATTR_DYNAMIC_COLLECTORS
262
+ );
263
+ hooks.onEventCreated(({ event }) => {
264
+ const context = new EventContext();
265
+ mergeNamespaceEntries(event, envConfig, context);
266
+ mergeNamespaceEntries(event, attrConfig, context);
267
+ });
268
+ }
269
+ };
270
+ }
271
+
272
+ // src/index.ts
273
+ registerBrowserPlugin("enrichContext", enrichContext);
274
+ export {
275
+ enrichContext
276
+ };
@@ -0,0 +1,280 @@
1
+ "use strict";
2
+ var __signalEnrichContextBundle = (() => {
3
+ // src/factory.ts
4
+ var ENV_NAMESPACE = "@env";
5
+ var ATTR_NAMESPACE = "@attr";
6
+ var EventContext = class {
7
+ document() {
8
+ var _a2;
9
+ if (this._document !== void 0) {
10
+ return (_a2 = this._document) != null ? _a2 : void 0;
11
+ }
12
+ const documentValue = typeof document === "undefined" ? null : document;
13
+ this._document = documentValue;
14
+ return documentValue != null ? documentValue : void 0;
15
+ }
16
+ location() {
17
+ var _a2;
18
+ if (this._location !== void 0) {
19
+ return (_a2 = this._location) != null ? _a2 : void 0;
20
+ }
21
+ const locationValue = typeof location === "undefined" ? null : location;
22
+ this._location = locationValue;
23
+ return locationValue != null ? locationValue : void 0;
24
+ }
25
+ searchParams() {
26
+ var _a2;
27
+ if (this._searchParams !== void 0) {
28
+ return this._searchParams;
29
+ }
30
+ const search = normalizeOptionalString((_a2 = this.location()) == null ? void 0 : _a2.search);
31
+ this._searchParams = search ? new URLSearchParams(search) : null;
32
+ return this._searchParams;
33
+ }
34
+ };
35
+ var ENV_STATIC_COLLECTORS = Object.freeze({
36
+ language: getLanguage,
37
+ platform: getPlatform
38
+ });
39
+ var ENV_DYNAMIC_COLLECTORS = Object.freeze({
40
+ current_url: (context) => {
41
+ var _a2;
42
+ return normalizeOptionalString((_a2 = context.location()) == null ? void 0 : _a2.href);
43
+ },
44
+ current_path: (context) => {
45
+ var _a2;
46
+ return normalizeOptionalString((_a2 = context.location()) == null ? void 0 : _a2.pathname);
47
+ },
48
+ current_title: (context) => {
49
+ var _a2;
50
+ return normalizeOptionalString((_a2 = context.document()) == null ? void 0 : _a2.title);
51
+ },
52
+ current_hostname: (context) => {
53
+ var _a2;
54
+ return normalizeOptionalString((_a2 = context.location()) == null ? void 0 : _a2.hostname);
55
+ }
56
+ });
57
+ var ATTR_QUERY_PARAMETER_KEYS = Object.freeze([
58
+ "utm_source",
59
+ "utm_medium",
60
+ "utm_campaign",
61
+ "utm_term",
62
+ "utm_content",
63
+ "utm_id",
64
+ "utm_source_platform",
65
+ "utm_campaign_id",
66
+ "utm_creative_format",
67
+ "utm_marketing_tactic",
68
+ "gclid",
69
+ "dclid",
70
+ "fbclid",
71
+ "ko_click_id",
72
+ "li_fat_id",
73
+ "msclkid",
74
+ "ttclid",
75
+ "twclid",
76
+ "wbraid",
77
+ "gbraid",
78
+ "rdt_cid",
79
+ "sccid"
80
+ ]);
81
+ function createAttrDynamicCollectors() {
82
+ const collectors = /* @__PURE__ */ Object.create(null);
83
+ for (const key of ATTR_QUERY_PARAMETER_KEYS) {
84
+ collectors[key] = (context) => {
85
+ var _a2;
86
+ return normalizeOptionalString((_a2 = context.searchParams()) == null ? void 0 : _a2.get(key));
87
+ };
88
+ }
89
+ return Object.freeze(collectors);
90
+ }
91
+ var ATTR_DYNAMIC_COLLECTORS = createAttrDynamicCollectors();
92
+ function normalizeOptionalString(value) {
93
+ if (typeof value !== "string" || value.length === 0) {
94
+ return void 0;
95
+ }
96
+ return value;
97
+ }
98
+ function getNavigator() {
99
+ return typeof navigator === "undefined" ? void 0 : navigator;
100
+ }
101
+ function getLanguage() {
102
+ var _a2;
103
+ return normalizeOptionalString((_a2 = getNavigator()) == null ? void 0 : _a2.language);
104
+ }
105
+ function getPlatform() {
106
+ var _a2, _b;
107
+ const currentNavigator = getNavigator();
108
+ return (_b = normalizeOptionalString((_a2 = currentNavigator == null ? void 0 : currentNavigator.userAgentData) == null ? void 0 : _a2.platform)) != null ? _b : normalizeOptionalString(currentNavigator == null ? void 0 : currentNavigator.platform);
109
+ }
110
+ function getReferrer() {
111
+ return normalizeOptionalString(
112
+ typeof document === "undefined" ? void 0 : document.referrer
113
+ );
114
+ }
115
+ function getReferringHostFromReferrer(referrer) {
116
+ if (!referrer) {
117
+ return void 0;
118
+ }
119
+ try {
120
+ return normalizeOptionalString(new URL(referrer).hostname);
121
+ } catch (e) {
122
+ return void 0;
123
+ }
124
+ }
125
+ function buildAttrStaticEntries(blockedKeys) {
126
+ if (blockedKeys.has("referrer") && blockedKeys.has("referring_host")) {
127
+ return [];
128
+ }
129
+ const values = [];
130
+ const referrer = getReferrer();
131
+ if (!blockedKeys.has("referrer") && referrer !== void 0) {
132
+ values.push(["referrer", referrer]);
133
+ }
134
+ if (!blockedKeys.has("referring_host")) {
135
+ const referringHost = getReferringHostFromReferrer(referrer);
136
+ if (referringHost !== void 0) {
137
+ values.push(["referring_host", referringHost]);
138
+ }
139
+ }
140
+ return values;
141
+ }
142
+ function parseBlockedNamespace(blockedProperties, namespace) {
143
+ const blockedKeys = /* @__PURE__ */ new Set();
144
+ const prefix = `${namespace}/`;
145
+ let namespaceBlocked = false;
146
+ for (const blockedProperty of blockedProperties) {
147
+ if (blockedProperty === namespace) {
148
+ namespaceBlocked = true;
149
+ continue;
150
+ }
151
+ if (!blockedProperty.startsWith(prefix)) {
152
+ continue;
153
+ }
154
+ const blockedKey = blockedProperty.slice(prefix.length);
155
+ if (blockedKey.length > 0) {
156
+ blockedKeys.add(blockedKey);
157
+ }
158
+ }
159
+ return {
160
+ namespaceBlocked,
161
+ blockedKeys
162
+ };
163
+ }
164
+ function buildStaticEntries(collectors, blockedKeys) {
165
+ const values = [];
166
+ for (const [key, collect] of Object.entries(collectors)) {
167
+ if (blockedKeys.has(key)) {
168
+ continue;
169
+ }
170
+ const value = collect();
171
+ if (value === void 0) {
172
+ continue;
173
+ }
174
+ values.push([key, value]);
175
+ }
176
+ return values;
177
+ }
178
+ function buildDynamicEntries(collectors, blockedKeys) {
179
+ const values = [];
180
+ for (const [key, collect] of Object.entries(collectors)) {
181
+ if (blockedKeys.has(key)) {
182
+ continue;
183
+ }
184
+ values.push([key, collect]);
185
+ }
186
+ return values;
187
+ }
188
+ function buildNamespaceConfig(namespace, blockedProperties, staticCollectors, dynamicCollectors) {
189
+ const blockedNamespace = parseBlockedNamespace(blockedProperties, namespace);
190
+ if (blockedNamespace.namespaceBlocked) {
191
+ return null;
192
+ }
193
+ const staticValues = namespace === ATTR_NAMESPACE ? buildAttrStaticEntries(blockedNamespace.blockedKeys) : buildStaticEntries(staticCollectors, blockedNamespace.blockedKeys);
194
+ const dynamicValues = buildDynamicEntries(dynamicCollectors, blockedNamespace.blockedKeys);
195
+ if (staticValues.length === 0 && dynamicValues.length === 0) {
196
+ return null;
197
+ }
198
+ return {
199
+ namespace,
200
+ staticValues,
201
+ dynamicValues
202
+ };
203
+ }
204
+ function isPlainObject(value) {
205
+ if (!value || typeof value !== "object") {
206
+ return false;
207
+ }
208
+ const prototype = Object.getPrototypeOf(value);
209
+ return prototype === Object.prototype || prototype === null;
210
+ }
211
+ function mergeNamespaceEntries(event, config, context) {
212
+ if (!config) {
213
+ return;
214
+ }
215
+ const existingValue = event[config.namespace];
216
+ if (existingValue !== void 0 && !isPlainObject(existingValue)) {
217
+ return;
218
+ }
219
+ const target = existingValue != null ? existingValue : {};
220
+ if (!isPlainObject(target)) {
221
+ return;
222
+ }
223
+ let changed = false;
224
+ for (const [key, value] of config.staticValues) {
225
+ if (key in target) {
226
+ continue;
227
+ }
228
+ target[key] = value;
229
+ changed = true;
230
+ }
231
+ for (const [key, collect] of config.dynamicValues) {
232
+ if (key in target) {
233
+ continue;
234
+ }
235
+ const value = collect(context);
236
+ if (value === void 0) {
237
+ continue;
238
+ }
239
+ target[key] = value;
240
+ changed = true;
241
+ }
242
+ if (existingValue === void 0 && changed) {
243
+ event[config.namespace] = target;
244
+ }
245
+ }
246
+ function enrichContext() {
247
+ return {
248
+ name: "enrichContext",
249
+ setup: ({ hooks, config }) => {
250
+ const envConfig = buildNamespaceConfig(
251
+ ENV_NAMESPACE,
252
+ config.blockedProperties,
253
+ ENV_STATIC_COLLECTORS,
254
+ ENV_DYNAMIC_COLLECTORS
255
+ );
256
+ const attrConfig = buildNamespaceConfig(
257
+ ATTR_NAMESPACE,
258
+ config.blockedProperties,
259
+ {},
260
+ ATTR_DYNAMIC_COLLECTORS
261
+ );
262
+ hooks.onEventCreated(({ event }) => {
263
+ const context = new EventContext();
264
+ mergeNamespaceEntries(event, envConfig, context);
265
+ mergeNamespaceEntries(event, attrConfig, context);
266
+ });
267
+ }
268
+ };
269
+ }
270
+
271
+ // src/global.ts
272
+ var globalObject = globalThis;
273
+ var _a;
274
+ if (!((_a = globalObject.signal) == null ? void 0 : _a.plugins) || typeof globalObject.signal.plugins !== "object") {
275
+ throw new Error(
276
+ "Signal browser global bundle must be loaded before the enrichContext plugin bundle"
277
+ );
278
+ }
279
+ globalObject.signal.plugins.enrichContext = enrichContext;
280
+ })();
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@atsignal/browser-plugin-enrich-context",
3
+ "version": "0.1.0",
4
+ "description": "Browser enrich-context plugin for Signal.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "dist/index.cjs",
8
+ "module": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/at-signal/signal-js.git"
13
+ },
14
+ "homepage": "https://github.com/at-signal/signal-js#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/at-signal/signal-js/issues"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js",
22
+ "require": "./dist/index.cjs"
23
+ }
24
+ },
25
+ "sideEffects": [
26
+ "./dist/index.js",
27
+ "./dist/index.cjs"
28
+ ],
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "peerDependencies": {
33
+ "@atsignal/browser": "0.1.0"
34
+ },
35
+ "devDependencies": {
36
+ "@atsignal/browser": "0.1.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "registry": "https://registry.npmjs.org/"
41
+ },
42
+ "scripts": {
43
+ "build": "tsup --config tsup.config.ts",
44
+ "dev": "tsup --config tsup.config.ts --watch",
45
+ "clean": "rimraf dist",
46
+ "test": "pnpm --filter @atsignal/js-core run build && pnpm --filter @atsignal/browser run build && pnpm run build && node --test test/*.test.mjs"
47
+ }
48
+ }