@amplitude/plugin-page-url-enrichment-browser 0.7.9 → 0.7.10

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.
@@ -1 +1 @@
1
- {"version":3,"file":"page-url-enrichment.d.ts","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,gBAAgB,EAMrB,KAAK,wBAAwB,EAG9B,MAAM,2BAA2B,CAAC;AAEnC,eAAO,MAAM,wBAAwB,qBAAqB,CAAC;AAC3D,eAAO,MAAM,yBAAyB,sBAAsB,CAAC;AAC7D,eAAO,MAAM,oBAAoB,iBAAiB,CAAC;AAEnD,MAAM,MAAM,OAAO,GAAG;IACpB,CAAC,wBAAwB,CAAC,CAAC,EAAE,MAAM,CAAC;IACpC,CAAC,yBAAyB,CAAC,CAAC,EAAE,MAAM,CAAC;CACtC,CAAC;AAQF,eAAO,MAAM,4BAA4B,aAIvC,CAAC;AAEH,eAAO,MAAM,0BAA0B,WAAY,OAAO,KAAG,OAQ5D,CAAC;AAEF,eAAO,MAAM,uBAAuB,yBAA8B,wBAAwB,KAAQ,gBAoLjG,CAAC"}
1
+ {"version":3,"file":"page-url-enrichment.d.ts","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,gBAAgB,EAMrB,KAAK,wBAAwB,EAG9B,MAAM,2BAA2B,CAAC;AAEnC,eAAO,MAAM,wBAAwB,qBAAqB,CAAC;AAC3D,eAAO,MAAM,yBAAyB,sBAAsB,CAAC;AAC7D,eAAO,MAAM,oBAAoB,iBAAiB,CAAC;AAEnD,MAAM,MAAM,OAAO,GAAG;IACpB,CAAC,wBAAwB,CAAC,CAAC,EAAE,MAAM,CAAC;IACpC,CAAC,yBAAyB,CAAC,CAAC,EAAE,MAAM,CAAC;CACtC,CAAC;AAQF,eAAO,MAAM,4BAA4B,aAIvC,CAAC;AAEH,eAAO,MAAM,0BAA0B,WAAY,OAAO,KAAG,OAQ5D,CAAC;AAEF,eAAO,MAAM,uBAAuB,yBAA8B,wBAAwB,KAAQ,gBAyMjG,CAAC"}
@@ -100,14 +100,16 @@ var pageUrlEnrichmentPlugin = function (_a) {
100
100
  name: '@amplitude/plugin-page-url-enrichment-browser',
101
101
  type: 'enrichment',
102
102
  setup: function (config, _) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
103
+ var referrer, referrerHostname, currentHostname, currentURL, existingURLInfo, storedCurrentURL, arrivedFromDifferentOrigin;
103
104
  var _a;
104
- return tslib_1.__generator(this, function (_b) {
105
- switch (_b.label) {
105
+ var _b;
106
+ return tslib_1.__generator(this, function (_c) {
107
+ switch (_c.label) {
106
108
  case 0:
107
109
  loggerProvider = config.loggerProvider;
108
110
  loggerProvider.log('Installing @amplitude/plugin-page-url-enrichment-browser');
109
111
  isTracking = true;
110
- if (!globalScope) return [3 /*break*/, 2];
112
+ if (!globalScope) return [3 /*break*/, 7];
111
113
  try {
112
114
  sessionStorage = new analytics_core_1.BrowserStorage(globalScope.sessionStorage);
113
115
  }
@@ -117,7 +119,36 @@ var pageUrlEnrichmentPlugin = function (_a) {
117
119
  }
118
120
  return [4 /*yield*/, (sessionStorage === null || sessionStorage === void 0 ? void 0 : sessionStorage.isEnabled())];
119
121
  case 1:
120
- isStorageEnabled = (_a = (_b.sent())) !== null && _a !== void 0 ? _a : false;
122
+ isStorageEnabled = (_b = (_c.sent())) !== null && _b !== void 0 ? _b : false;
123
+ if (!(sessionStorage && isStorageEnabled)) return [3 /*break*/, 6];
124
+ referrer = (typeof document !== 'undefined' && document.referrer) || '';
125
+ referrerHostname = referrer ? getHostname(referrer) : undefined;
126
+ currentHostname = (typeof location !== 'undefined' && location.hostname) || '';
127
+ currentURL = (0, analytics_core_1.getDecodeURI)((typeof location !== 'undefined' && location.href) || '');
128
+ return [4 /*yield*/, sessionStorage.get(exports.URL_INFO_STORAGE_KEY)];
129
+ case 2:
130
+ existingURLInfo = _c.sent();
131
+ storedCurrentURL = (existingURLInfo === null || existingURLInfo === void 0 ? void 0 : existingURLInfo[exports.CURRENT_PAGE_STORAGE_KEY]) || '';
132
+ arrivedFromDifferentOrigin = !!referrerHostname && referrerHostname !== currentHostname && storedCurrentURL !== currentURL;
133
+ if (!arrivedFromDifferentOrigin) return [3 /*break*/, 4];
134
+ // sessionStorage survives same-tab cross-origin trips (siteA -> external -> siteA),
135
+ // leaving stale "current" that would wrongly become "previous". document.referrer
136
+ // is the truthful previous, so overwrite — at the cost of dropping the prior siteA page.
137
+ return [4 /*yield*/, sessionStorage.set(exports.URL_INFO_STORAGE_KEY, (_a = {},
138
+ _a[exports.CURRENT_PAGE_STORAGE_KEY] = currentURL,
139
+ _a[exports.PREVIOUS_PAGE_STORAGE_KEY] = referrer,
140
+ _a))];
141
+ case 3:
142
+ // sessionStorage survives same-tab cross-origin trips (siteA -> external -> siteA),
143
+ // leaving stale "current" that would wrongly become "previous". document.referrer
144
+ // is the truthful previous, so overwrite — at the cost of dropping the prior siteA page.
145
+ _c.sent();
146
+ return [3 /*break*/, 6];
147
+ case 4: return [4 /*yield*/, saveURLInfo()];
148
+ case 5:
149
+ _c.sent();
150
+ _c.label = 6;
151
+ case 6:
121
152
  globalScope.addEventListener('popstate', saveUrlInfoWrapper);
122
153
  if (!isProxied) {
123
154
  /* istanbul ignore next */
@@ -146,44 +177,29 @@ var pageUrlEnrichmentPlugin = function (_a) {
146
177
  });
147
178
  isProxied = true;
148
179
  }
149
- _b.label = 2;
150
- case 2: return [2 /*return*/];
180
+ _c.label = 7;
181
+ case 7: return [2 /*return*/];
151
182
  }
152
183
  });
153
184
  }); },
154
185
  execute: function (event) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
155
- var locationHREF, URLInfo, updatedURLInfo, previousPage;
156
- var _a;
157
- return tslib_1.__generator(this, function (_b) {
158
- switch (_b.label) {
186
+ var locationHREF, URLInfo, previousPage;
187
+ return tslib_1.__generator(this, function (_a) {
188
+ switch (_a.label) {
159
189
  case 0:
160
190
  locationHREF = (0, analytics_core_1.getDecodeURI)((typeof location !== 'undefined' && location.href) || '');
161
- if (!(sessionStorage && isStorageEnabled)) return [3 /*break*/, 5];
191
+ if (!(sessionStorage && isStorageEnabled)) return [3 /*break*/, 2];
162
192
  return [4 /*yield*/, sessionStorage.get(exports.URL_INFO_STORAGE_KEY)];
163
193
  case 1:
164
- URLInfo = _b.sent();
165
- if (!!(URLInfo === null || URLInfo === void 0 ? void 0 : URLInfo[exports.CURRENT_PAGE_STORAGE_KEY])) return [3 /*break*/, 3];
166
- return [4 /*yield*/, sessionStorage.set(exports.URL_INFO_STORAGE_KEY, (_a = {},
167
- _a[exports.CURRENT_PAGE_STORAGE_KEY] = locationHREF,
168
- _a[exports.PREVIOUS_PAGE_STORAGE_KEY] = document.referrer || '',
169
- _a))];
170
- case 2:
171
- _b.sent();
172
- _b.label = 3;
173
- case 3: return [4 /*yield*/, sessionStorage.get(exports.URL_INFO_STORAGE_KEY)];
174
- case 4:
175
- updatedURLInfo = _b.sent();
176
- previousPage = '';
177
- if (updatedURLInfo) {
178
- previousPage = updatedURLInfo[exports.PREVIOUS_PAGE_STORAGE_KEY] || '';
179
- }
194
+ URLInfo = _a.sent();
195
+ previousPage = (URLInfo === null || URLInfo === void 0 ? void 0 : URLInfo[exports.PREVIOUS_PAGE_STORAGE_KEY]) || '';
180
196
  // no need to proceed to add additional properties if the event is one of the default event types to be excluded
181
197
  if (exports.EXCLUDED_DEFAULT_EVENT_TYPES.has(event.event_type)) {
182
198
  return [2 /*return*/, event];
183
199
  }
184
200
  event.event_properties = tslib_1.__assign(tslib_1.__assign({}, (event.event_properties || {})), { '[Amplitude] Page Domain': addIfNotExist(event, '[Amplitude] Page Domain', (typeof location !== 'undefined' && location.hostname) || ''), '[Amplitude] Page Location': addIfNotExist(event, '[Amplitude] Page Location', locationHREF), '[Amplitude] Page Path': addIfNotExist(event, '[Amplitude] Page Path', (typeof location !== 'undefined' && (0, analytics_core_1.getDecodeURI)(location.pathname)) || ''), '[Amplitude] Page Title': addIfNotExist(event, '[Amplitude] Page Title', (0, analytics_core_1.getPageTitle)(analytics_core_1.replaceSensitiveString)), '[Amplitude] Page URL': addIfNotExist(event, '[Amplitude] Page URL', locationHREF.split('?')[0]), '[Amplitude] Previous Page Location': previousPage, '[Amplitude] Previous Page Type': getPrevPageType(previousPage) });
185
- _b.label = 5;
186
- case 5: return [2 /*return*/, event];
201
+ _a.label = 2;
202
+ case 2: return [2 /*return*/, event];
187
203
  }
188
204
  });
189
205
  }); },
@@ -1 +1 @@
1
- {"version":3,"file":"page-url-enrichment.js","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":";;;;AAAA,4DAamC;AAEtB,QAAA,wBAAwB,GAAG,kBAAkB,CAAC;AAC9C,QAAA,yBAAyB,GAAG,mBAAmB,CAAC;AAChD,QAAA,oBAAoB,GAAG,cAAc,CAAC;AAOnD,IAAK,gBAIJ;AAJD,WAAK,gBAAgB;IACnB,qCAAiB,CAAA;IACjB,yCAAqB,CAAA;IACrB,yCAAqB,CAAA;AACvB,CAAC,EAJI,gBAAgB,KAAhB,gBAAgB,QAIpB;AAEY,QAAA,4BAA4B,GAAG,IAAI,GAAG,CAAS;IAC1D,iCAAgB,CAAC,QAAQ;IACzB,iCAAgB,CAAC,cAAc;IAC/B,iCAAgB,CAAC,OAAO;CACzB,CAAC,CAAC;AAEI,IAAM,0BAA0B,GAAG,UAAC,MAAe;IACxD,IAAI,OAAO,MAAM,KAAK,SAAS,EAAE;QAC/B,OAAO,MAAM,CAAC;KACf;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,mBAAmB,IAAI,MAAM,EAAE;QAClF,OAAO,OAAO,CAAE,MAA0C,CAAC,iBAAiB,CAAC,CAAC;KAC/E;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AARW,QAAA,0BAA0B,8BAQrC;AAEK,IAAM,uBAA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AAC,4BAAoB,EAAE,EAAE,CAAC,EAAA;;wBAAlD,SAAkD,CAAC;;;;;aAEtD;KACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AApLW,QAAA,uBAAuB,2BAoLlC;AAEF,SAAS,aAAa,CAAC,KAAY,EAAE,GAAW,EAAE,KAAa;IAC7D,IAAI,CAAC,KAAK,CAAC,gBAAgB,EAAE;QAC3B,KAAK,CAAC,gBAAgB,GAAG,EAAE,CAAC;KAC7B;IAED,IAAK,KAAK,CAAC,gBAA2C,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;QACzE,OAAO,KAAK,CAAC;KACd;IAED,OAAQ,KAAK,CAAC,gBAA2C,CAAC,GAAG,CAAW,CAAC;AAC3E,CAAC","sourcesContent":["import {\n type BrowserClient,\n type BrowserConfig,\n BrowserStorage,\n type EnrichmentPlugin,\n type Event,\n getDecodeURI,\n getGlobalScope,\n getPageTitle,\n type ILogger,\n type PageUrlEnrichmentOptions,\n replaceSensitiveString,\n SpecialEventType,\n} from '@amplitude/analytics-core';\n\nexport const CURRENT_PAGE_STORAGE_KEY = 'AMP_CURRENT_PAGE';\nexport const PREVIOUS_PAGE_STORAGE_KEY = 'AMP_PREVIOUS_PAGE';\nexport const URL_INFO_STORAGE_KEY = 'AMP_URL_INFO';\n\nexport type URLInfo = {\n [CURRENT_PAGE_STORAGE_KEY]?: string;\n [PREVIOUS_PAGE_STORAGE_KEY]?: string;\n};\n\nenum PreviousPageType {\n Direct = 'direct', // for no prev page or referrer\n Internal = 'internal', // for internal domains - exact domain matches or when current and previous page are both internal domains\n External = 'external', // for different domains\n}\n\nexport const EXCLUDED_DEFAULT_EVENT_TYPES = new Set<string>([\n SpecialEventType.IDENTIFY,\n SpecialEventType.GROUP_IDENTIFY,\n SpecialEventType.REVENUE,\n]);\n\nexport const isPageUrlEnrichmentEnabled = (option: unknown): boolean => {\n if (typeof option === 'boolean') {\n return option;\n }\n if (typeof option === 'object' && option !== null && 'pageUrlEnrichment' in option) {\n return Boolean((option as { pageUrlEnrichment?: boolean }).pageUrlEnrichment);\n }\n return false;\n};\n\nexport const pageUrlEnrichmentPlugin = ({ internalDomains = [] }: PageUrlEnrichmentOptions = {}): EnrichmentPlugin => {\n const globalScope = getGlobalScope();\n let sessionStorage: BrowserStorage<URLInfo> | undefined = undefined;\n let isStorageEnabled = false;\n let loggerProvider: ILogger | undefined = undefined;\n\n let isProxied = false;\n let isTracking = false;\n\n const getHostname = (url: string): string | undefined => {\n let hostname: string | undefined;\n\n try {\n const decodedUrl = getDecodeURI(url, loggerProvider);\n hostname = new URL(decodedUrl).hostname;\n } catch (e) {\n /* istanbul ignore next */\n loggerProvider?.error('Could not parse URL: ', e);\n }\n\n return hostname;\n };\n\n const getPrevPageType = (previousPage: string) => {\n const currentDomain = (typeof location !== 'undefined' && location.hostname) || '';\n const previousPageDomain = previousPage ? getHostname(previousPage) : undefined;\n\n if (!previousPageDomain) {\n return PreviousPageType.Direct;\n }\n\n const isCurrentInternal = internalDomains.some((domain) => currentDomain.indexOf(domain) !== -1);\n const isPrevInternal = internalDomains.some((domain) => previousPageDomain.indexOf(domain) !== -1);\n\n if (currentDomain === previousPageDomain || (isPrevInternal && isCurrentInternal)) {\n return PreviousPageType.Internal;\n }\n\n return PreviousPageType.External;\n };\n\n const saveURLInfo = async () => {\n if (sessionStorage && isStorageEnabled) {\n const URLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n const currentURL = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n const storedCurrentURL = URLInfo?.[CURRENT_PAGE_STORAGE_KEY] || '';\n\n let previousURL: string | undefined;\n if (currentURL === storedCurrentURL) {\n previousURL = URLInfo?.[PREVIOUS_PAGE_STORAGE_KEY] || '';\n } else if (storedCurrentURL) {\n previousURL = storedCurrentURL;\n } else {\n previousURL = document.referrer || '';\n }\n\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {\n [CURRENT_PAGE_STORAGE_KEY]: currentURL,\n [PREVIOUS_PAGE_STORAGE_KEY]: previousURL,\n });\n }\n };\n\n const saveUrlInfoWrapper = () => {\n void saveURLInfo();\n };\n\n const plugin: EnrichmentPlugin = {\n name: '@amplitude/plugin-page-url-enrichment-browser',\n type: 'enrichment',\n\n setup: async (config: BrowserConfig, _: BrowserClient) => {\n loggerProvider = config.loggerProvider;\n loggerProvider.log('Installing @amplitude/plugin-page-url-enrichment-browser');\n\n isTracking = true;\n\n if (globalScope) {\n try {\n sessionStorage = new BrowserStorage<URLInfo>(globalScope.sessionStorage);\n } catch (error) {\n /* istanbul ignore next */\n loggerProvider?.debug('sessionStorage is not available in this environment.');\n }\n isStorageEnabled = (await sessionStorage?.isEnabled()) ?? false;\n\n globalScope.addEventListener('popstate', saveUrlInfoWrapper);\n\n if (!isProxied) {\n /* istanbul ignore next */\n // There is no global browser listener for changes to history, so we have\n // to modify pushState and replaceState directly.\n // https://stackoverflow.com/a/64927639\n // eslint-disable-next-line @typescript-eslint/unbound-method\n globalScope.history.pushState = new Proxy(globalScope.history.pushState, {\n apply: (target, thisArg, [state, unused, url]) => {\n target.apply(thisArg, [state, unused, url]);\n if (isTracking) {\n saveUrlInfoWrapper();\n }\n },\n });\n\n // eslint-disable-next-line @typescript-eslint/unbound-method\n globalScope.history.replaceState = new Proxy(globalScope.history.replaceState, {\n apply: (target, thisArg, [state, unused, url]) => {\n target.apply(thisArg, [state, unused, url]);\n if (isTracking) {\n saveUrlInfoWrapper();\n }\n },\n });\n\n isProxied = true;\n }\n }\n },\n execute: async (event: Event) => {\n const locationHREF = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n\n if (sessionStorage && isStorageEnabled) {\n const URLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n if (!URLInfo?.[CURRENT_PAGE_STORAGE_KEY]) {\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {\n [CURRENT_PAGE_STORAGE_KEY]: locationHREF,\n [PREVIOUS_PAGE_STORAGE_KEY]: document.referrer || '',\n });\n }\n\n const updatedURLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n let previousPage = '';\n if (updatedURLInfo) {\n previousPage = updatedURLInfo[PREVIOUS_PAGE_STORAGE_KEY] || '';\n }\n\n // no need to proceed to add additional properties if the event is one of the default event types to be excluded\n if (EXCLUDED_DEFAULT_EVENT_TYPES.has(event.event_type)) {\n return event;\n }\n\n event.event_properties = {\n ...(event.event_properties || {}),\n '[Amplitude] Page Domain': addIfNotExist(\n event,\n '[Amplitude] Page Domain',\n (typeof location !== 'undefined' && location.hostname) || '',\n ),\n '[Amplitude] Page Location': addIfNotExist(event, '[Amplitude] Page Location', locationHREF),\n '[Amplitude] Page Path': addIfNotExist(\n event,\n '[Amplitude] Page Path',\n (typeof location !== 'undefined' && getDecodeURI(location.pathname)) || '',\n ),\n '[Amplitude] Page Title': addIfNotExist(\n event,\n '[Amplitude] Page Title',\n getPageTitle(replaceSensitiveString),\n ),\n '[Amplitude] Page URL': addIfNotExist(event, '[Amplitude] Page URL', locationHREF.split('?')[0]),\n '[Amplitude] Previous Page Location': previousPage,\n '[Amplitude] Previous Page Type': getPrevPageType(previousPage),\n };\n }\n\n return event;\n },\n teardown: async () => {\n if (globalScope) {\n globalScope.removeEventListener('popstate', saveUrlInfoWrapper);\n\n isTracking = false;\n }\n\n if (sessionStorage && isStorageEnabled) {\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {});\n }\n },\n };\n\n return plugin;\n};\n\nfunction addIfNotExist(event: Event, key: string, value: string): string {\n if (!event.event_properties) {\n event.event_properties = {};\n }\n\n if ((event.event_properties as { [key: string]: any })[key] === undefined) {\n return value;\n }\n\n return (event.event_properties as { [key: string]: any })[key] as string;\n}\n"]}
1
+ {"version":3,"file":"page-url-enrichment.js","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":";;;;AAAA,4DAamC;AAEtB,QAAA,wBAAwB,GAAG,kBAAkB,CAAC;AAC9C,QAAA,yBAAyB,GAAG,mBAAmB,CAAC;A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W,CAAC,mBAAmB,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;4BAEhE,UAAU,GAAG,KAAK,CAAC;yBACpB;6BAEG,CAAA,cAAc,IAAI,gBAAgB,CAAA,EAAlC,wBAAkC;wBACpC,qBAAM,cAAc,CAAC,GAAG,CAAC,4BAAoB,EAAE,EAAE,CAAC,EAAA;;wBAAlD,SAAkD,CAAC;;;;;aAEtD;KACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAzMW,QAAA,uBAAuB,2BAyMlC;AAEF,SAAS,aAAa,CAAC,KAAY,EAAE,GAAW,EAAE,KAAa;IAC7D,IAAI,CAAC,KAAK,CAAC,gBAAgB,EAAE;QAC3B,KAAK,CAAC,gBAAgB,GAAG,EAAE,CAAC;KAC7B;IAED,IAAK,KAAK,CAAC,gBAA2C,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;QACzE,OAAO,KAAK,CAAC;KACd;IAED,OAAQ,KAAK,CAAC,gBAA2C,CAAC,GAAG,CAAW,CAAC;AAC3E,CAAC","sourcesContent":["import {\n type BrowserClient,\n type BrowserConfig,\n BrowserStorage,\n type EnrichmentPlugin,\n type Event,\n getDecodeURI,\n getGlobalScope,\n getPageTitle,\n type ILogger,\n type PageUrlEnrichmentOptions,\n replaceSensitiveString,\n SpecialEventType,\n} from '@amplitude/analytics-core';\n\nexport const CURRENT_PAGE_STORAGE_KEY = 'AMP_CURRENT_PAGE';\nexport const PREVIOUS_PAGE_STORAGE_KEY = 'AMP_PREVIOUS_PAGE';\nexport const URL_INFO_STORAGE_KEY = 'AMP_URL_INFO';\n\nexport type URLInfo = {\n [CURRENT_PAGE_STORAGE_KEY]?: string;\n [PREVIOUS_PAGE_STORAGE_KEY]?: string;\n};\n\nenum PreviousPageType {\n Direct = 'direct', // for no prev page or referrer\n Internal = 'internal', // for internal domains - exact domain matches or when current and previous page are both internal domains\n External = 'external', // for different domains\n}\n\nexport const EXCLUDED_DEFAULT_EVENT_TYPES = new Set<string>([\n SpecialEventType.IDENTIFY,\n SpecialEventType.GROUP_IDENTIFY,\n SpecialEventType.REVENUE,\n]);\n\nexport const isPageUrlEnrichmentEnabled = (option: unknown): boolean => {\n if (typeof option === 'boolean') {\n return option;\n }\n if (typeof option === 'object' && option !== null && 'pageUrlEnrichment' in option) {\n return Boolean((option as { pageUrlEnrichment?: boolean }).pageUrlEnrichment);\n }\n return false;\n};\n\nexport const pageUrlEnrichmentPlugin = ({ internalDomains = [] }: PageUrlEnrichmentOptions = {}): EnrichmentPlugin => {\n const globalScope = getGlobalScope();\n let sessionStorage: BrowserStorage<URLInfo> | undefined = undefined;\n let isStorageEnabled = false;\n let loggerProvider: ILogger | undefined = undefined;\n\n let isProxied = false;\n let isTracking = false;\n\n const getHostname = (url: string): string | undefined => {\n let hostname: string | undefined;\n\n try {\n const decodedUrl = getDecodeURI(url, loggerProvider);\n hostname = new URL(decodedUrl).hostname;\n } catch (e) {\n /* istanbul ignore next */\n loggerProvider?.error('Could not parse URL: ', e);\n }\n\n return hostname;\n };\n\n const getPrevPageType = (previousPage: string) => {\n const currentDomain = (typeof location !== 'undefined' && location.hostname) || '';\n const previousPageDomain = previousPage ? getHostname(previousPage) : undefined;\n\n if (!previousPageDomain) {\n return PreviousPageType.Direct;\n }\n\n const isCurrentInternal = internalDomains.some((domain) => currentDomain.indexOf(domain) !== -1);\n const isPrevInternal = internalDomains.some((domain) => previousPageDomain.indexOf(domain) !== -1);\n\n if (currentDomain === previousPageDomain || (isPrevInternal && isCurrentInternal)) {\n return PreviousPageType.Internal;\n }\n\n return PreviousPageType.External;\n };\n\n const saveURLInfo = async () => {\n if (sessionStorage && isStorageEnabled) {\n const URLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n const currentURL = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n const storedCurrentURL = URLInfo?.[CURRENT_PAGE_STORAGE_KEY] || '';\n\n let previousURL: string | undefined;\n if (currentURL === storedCurrentURL) {\n previousURL = URLInfo?.[PREVIOUS_PAGE_STORAGE_KEY] || '';\n } else if (storedCurrentURL) {\n previousURL = storedCurrentURL;\n } else {\n previousURL = document.referrer || '';\n }\n\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {\n [CURRENT_PAGE_STORAGE_KEY]: currentURL,\n [PREVIOUS_PAGE_STORAGE_KEY]: previousURL,\n });\n }\n };\n\n const saveUrlInfoWrapper = () => {\n void saveURLInfo();\n };\n\n const plugin: EnrichmentPlugin = {\n name: '@amplitude/plugin-page-url-enrichment-browser',\n type: 'enrichment',\n\n setup: async (config: BrowserConfig, _: BrowserClient) => {\n loggerProvider = config.loggerProvider;\n loggerProvider.log('Installing @amplitude/plugin-page-url-enrichment-browser');\n\n isTracking = true;\n\n if (globalScope) {\n try {\n sessionStorage = new BrowserStorage<URLInfo>(globalScope.sessionStorage);\n } catch (error) {\n /* istanbul ignore next */\n loggerProvider?.debug('sessionStorage is not available in this environment.');\n }\n isStorageEnabled = (await sessionStorage?.isEnabled()) ?? false;\n\n if (sessionStorage && isStorageEnabled) {\n const referrer = (typeof document !== 'undefined' && document.referrer) || '';\n const referrerHostname = referrer ? getHostname(referrer) : undefined;\n const currentHostname = (typeof location !== 'undefined' && location.hostname) || '';\n const currentURL = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n\n const existingURLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n const storedCurrentURL = existingURLInfo?.[CURRENT_PAGE_STORAGE_KEY] || '';\n\n // document.referrer is preserved across pushState navigations and full-page\n // reloads, so a different referrer hostname alone does not prove an external\n // arrival. Also require the stored \"current\" to differ from location.href —\n // that's what distinguishes a genuine new landing in this tab from a refresh\n // of an already-tracked page (where storage holds the truthful previous).\n const arrivedFromDifferentOrigin =\n !!referrerHostname && referrerHostname !== currentHostname && storedCurrentURL !== currentURL;\n\n if (arrivedFromDifferentOrigin) {\n // sessionStorage survives same-tab cross-origin trips (siteA -> external -> siteA),\n // leaving stale \"current\" that would wrongly become \"previous\". document.referrer\n // is the truthful previous, so overwrite — at the cost of dropping the prior siteA page.\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {\n [CURRENT_PAGE_STORAGE_KEY]: currentURL,\n [PREVIOUS_PAGE_STORAGE_KEY]: referrer,\n });\n } else {\n await saveURLInfo();\n }\n }\n\n globalScope.addEventListener('popstate', saveUrlInfoWrapper);\n\n if (!isProxied) {\n /* istanbul ignore next */\n // There is no global browser listener for changes to history, so we have\n // to modify pushState and replaceState directly.\n // https://stackoverflow.com/a/64927639\n // eslint-disable-next-line @typescript-eslint/unbound-method\n globalScope.history.pushState = new Proxy(globalScope.history.pushState, {\n apply: (target, thisArg, [state, unused, url]) => {\n target.apply(thisArg, [state, unused, url]);\n if (isTracking) {\n saveUrlInfoWrapper();\n }\n },\n });\n\n // eslint-disable-next-line @typescript-eslint/unbound-method\n globalScope.history.replaceState = new Proxy(globalScope.history.replaceState, {\n apply: (target, thisArg, [state, unused, url]) => {\n target.apply(thisArg, [state, unused, url]);\n if (isTracking) {\n saveUrlInfoWrapper();\n }\n },\n });\n\n isProxied = true;\n }\n }\n },\n execute: async (event: Event) => {\n const locationHREF = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n\n if (sessionStorage && isStorageEnabled) {\n // setup() seeds AMP_URL_INFO via saveURLInfo(), and pushState/replaceState/popstate\n // keep it in sync, so we just read the previous page here.\n const URLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n const previousPage = URLInfo?.[PREVIOUS_PAGE_STORAGE_KEY] || '';\n\n // no need to proceed to add additional properties if the event is one of the default event types to be excluded\n if (EXCLUDED_DEFAULT_EVENT_TYPES.has(event.event_type)) {\n return event;\n }\n\n event.event_properties = {\n ...(event.event_properties || {}),\n '[Amplitude] Page Domain': addIfNotExist(\n event,\n '[Amplitude] Page Domain',\n (typeof location !== 'undefined' && location.hostname) || '',\n ),\n '[Amplitude] Page Location': addIfNotExist(event, '[Amplitude] Page Location', locationHREF),\n '[Amplitude] Page Path': addIfNotExist(\n event,\n '[Amplitude] Page Path',\n (typeof location !== 'undefined' && getDecodeURI(location.pathname)) || '',\n ),\n '[Amplitude] Page Title': addIfNotExist(\n event,\n '[Amplitude] Page Title',\n getPageTitle(replaceSensitiveString),\n ),\n '[Amplitude] Page URL': addIfNotExist(event, '[Amplitude] Page URL', locationHREF.split('?')[0]),\n '[Amplitude] Previous Page Location': previousPage,\n '[Amplitude] Previous Page Type': getPrevPageType(previousPage),\n };\n }\n\n return event;\n },\n teardown: async () => {\n if (globalScope) {\n globalScope.removeEventListener('popstate', saveUrlInfoWrapper);\n\n isTracking = false;\n }\n\n if (sessionStorage && isStorageEnabled) {\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {});\n }\n },\n };\n\n return plugin;\n};\n\nfunction addIfNotExist(event: Event, key: string, value: string): string {\n if (!event.event_properties) {\n event.event_properties = {};\n }\n\n if ((event.event_properties as { [key: string]: any })[key] === undefined) {\n return value;\n }\n\n return (event.event_properties as { [key: string]: any })[key] as string;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"page-url-enrichment.d.ts","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,gBAAgB,EAMrB,KAAK,wBAAwB,EAG9B,MAAM,2BAA2B,CAAC;AAEnC,eAAO,MAAM,wBAAwB,qBAAqB,CAAC;AAC3D,eAAO,MAAM,yBAAyB,sBAAsB,CAAC;AAC7D,eAAO,MAAM,oBAAoB,iBAAiB,CAAC;AAEnD,MAAM,MAAM,OAAO,GAAG;IACpB,CAAC,wBAAwB,CAAC,CAAC,EAAE,MAAM,CAAC;IACpC,CAAC,yBAAyB,CAAC,CAAC,EAAE,MAAM,CAAC;CACtC,CAAC;AAQF,eAAO,MAAM,4BAA4B,aAIvC,CAAC;AAEH,eAAO,MAAM,0BAA0B,WAAY,OAAO,KAAG,OAQ5D,CAAC;AAEF,eAAO,MAAM,uBAAuB,yBAA8B,wBAAwB,KAAQ,gBAoLjG,CAAC"}
1
+ {"version":3,"file":"page-url-enrichment.d.ts","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,gBAAgB,EAMrB,KAAK,wBAAwB,EAG9B,MAAM,2BAA2B,CAAC;AAEnC,eAAO,MAAM,wBAAwB,qBAAqB,CAAC;AAC3D,eAAO,MAAM,yBAAyB,sBAAsB,CAAC;AAC7D,eAAO,MAAM,oBAAoB,iBAAiB,CAAC;AAEnD,MAAM,MAAM,OAAO,GAAG;IACpB,CAAC,wBAAwB,CAAC,CAAC,EAAE,MAAM,CAAC;IACpC,CAAC,yBAAyB,CAAC,CAAC,EAAE,MAAM,CAAC;CACtC,CAAC;AAQF,eAAO,MAAM,4BAA4B,aAIvC,CAAC;AAEH,eAAO,MAAM,0BAA0B,WAAY,OAAO,KAAG,OAQ5D,CAAC;AAEF,eAAO,MAAM,uBAAuB,yBAA8B,wBAAwB,KAAQ,gBAyMjG,CAAC"}
@@ -96,14 +96,16 @@ export var pageUrlEnrichmentPlugin = function (_a) {
96
96
  name: '@amplitude/plugin-page-url-enrichment-browser',
97
97
  type: 'enrichment',
98
98
  setup: function (config, _) { return __awaiter(void 0, void 0, void 0, function () {
99
+ var referrer, referrerHostname, currentHostname, currentURL, existingURLInfo, storedCurrentURL, arrivedFromDifferentOrigin;
99
100
  var _a;
100
- return __generator(this, function (_b) {
101
- switch (_b.label) {
101
+ var _b;
102
+ return __generator(this, function (_c) {
103
+ switch (_c.label) {
102
104
  case 0:
103
105
  loggerProvider = config.loggerProvider;
104
106
  loggerProvider.log('Installing @amplitude/plugin-page-url-enrichment-browser');
105
107
  isTracking = true;
106
- if (!globalScope) return [3 /*break*/, 2];
108
+ if (!globalScope) return [3 /*break*/, 7];
107
109
  try {
108
110
  sessionStorage = new BrowserStorage(globalScope.sessionStorage);
109
111
  }
@@ -113,7 +115,36 @@ export var pageUrlEnrichmentPlugin = function (_a) {
113
115
  }
114
116
  return [4 /*yield*/, (sessionStorage === null || sessionStorage === void 0 ? void 0 : sessionStorage.isEnabled())];
115
117
  case 1:
116
- isStorageEnabled = (_a = (_b.sent())) !== null && _a !== void 0 ? _a : false;
118
+ isStorageEnabled = (_b = (_c.sent())) !== null && _b !== void 0 ? _b : false;
119
+ if (!(sessionStorage && isStorageEnabled)) return [3 /*break*/, 6];
120
+ referrer = (typeof document !== 'undefined' && document.referrer) || '';
121
+ referrerHostname = referrer ? getHostname(referrer) : undefined;
122
+ currentHostname = (typeof location !== 'undefined' && location.hostname) || '';
123
+ currentURL = getDecodeURI((typeof location !== 'undefined' && location.href) || '');
124
+ return [4 /*yield*/, sessionStorage.get(URL_INFO_STORAGE_KEY)];
125
+ case 2:
126
+ existingURLInfo = _c.sent();
127
+ storedCurrentURL = (existingURLInfo === null || existingURLInfo === void 0 ? void 0 : existingURLInfo[CURRENT_PAGE_STORAGE_KEY]) || '';
128
+ arrivedFromDifferentOrigin = !!referrerHostname && referrerHostname !== currentHostname && storedCurrentURL !== currentURL;
129
+ if (!arrivedFromDifferentOrigin) return [3 /*break*/, 4];
130
+ // sessionStorage survives same-tab cross-origin trips (siteA -> external -> siteA),
131
+ // leaving stale "current" that would wrongly become "previous". document.referrer
132
+ // is the truthful previous, so overwrite — at the cost of dropping the prior siteA page.
133
+ return [4 /*yield*/, sessionStorage.set(URL_INFO_STORAGE_KEY, (_a = {},
134
+ _a[CURRENT_PAGE_STORAGE_KEY] = currentURL,
135
+ _a[PREVIOUS_PAGE_STORAGE_KEY] = referrer,
136
+ _a))];
137
+ case 3:
138
+ // sessionStorage survives same-tab cross-origin trips (siteA -> external -> siteA),
139
+ // leaving stale "current" that would wrongly become "previous". document.referrer
140
+ // is the truthful previous, so overwrite — at the cost of dropping the prior siteA page.
141
+ _c.sent();
142
+ return [3 /*break*/, 6];
143
+ case 4: return [4 /*yield*/, saveURLInfo()];
144
+ case 5:
145
+ _c.sent();
146
+ _c.label = 6;
147
+ case 6:
117
148
  globalScope.addEventListener('popstate', saveUrlInfoWrapper);
118
149
  if (!isProxied) {
119
150
  /* istanbul ignore next */
@@ -142,44 +173,29 @@ export var pageUrlEnrichmentPlugin = function (_a) {
142
173
  });
143
174
  isProxied = true;
144
175
  }
145
- _b.label = 2;
146
- case 2: return [2 /*return*/];
176
+ _c.label = 7;
177
+ case 7: return [2 /*return*/];
147
178
  }
148
179
  });
149
180
  }); },
150
181
  execute: function (event) { return __awaiter(void 0, void 0, void 0, function () {
151
- var locationHREF, URLInfo, updatedURLInfo, previousPage;
152
- var _a;
153
- return __generator(this, function (_b) {
154
- switch (_b.label) {
182
+ var locationHREF, URLInfo, previousPage;
183
+ return __generator(this, function (_a) {
184
+ switch (_a.label) {
155
185
  case 0:
156
186
  locationHREF = getDecodeURI((typeof location !== 'undefined' && location.href) || '');
157
- if (!(sessionStorage && isStorageEnabled)) return [3 /*break*/, 5];
187
+ if (!(sessionStorage && isStorageEnabled)) return [3 /*break*/, 2];
158
188
  return [4 /*yield*/, sessionStorage.get(URL_INFO_STORAGE_KEY)];
159
189
  case 1:
160
- URLInfo = _b.sent();
161
- if (!!(URLInfo === null || URLInfo === void 0 ? void 0 : URLInfo[CURRENT_PAGE_STORAGE_KEY])) return [3 /*break*/, 3];
162
- return [4 /*yield*/, sessionStorage.set(URL_INFO_STORAGE_KEY, (_a = {},
163
- _a[CURRENT_PAGE_STORAGE_KEY] = locationHREF,
164
- _a[PREVIOUS_PAGE_STORAGE_KEY] = document.referrer || '',
165
- _a))];
166
- case 2:
167
- _b.sent();
168
- _b.label = 3;
169
- case 3: return [4 /*yield*/, sessionStorage.get(URL_INFO_STORAGE_KEY)];
170
- case 4:
171
- updatedURLInfo = _b.sent();
172
- previousPage = '';
173
- if (updatedURLInfo) {
174
- previousPage = updatedURLInfo[PREVIOUS_PAGE_STORAGE_KEY] || '';
175
- }
190
+ URLInfo = _a.sent();
191
+ previousPage = (URLInfo === null || URLInfo === void 0 ? void 0 : URLInfo[PREVIOUS_PAGE_STORAGE_KEY]) || '';
176
192
  // no need to proceed to add additional properties if the event is one of the default event types to be excluded
177
193
  if (EXCLUDED_DEFAULT_EVENT_TYPES.has(event.event_type)) {
178
194
  return [2 /*return*/, event];
179
195
  }
180
196
  event.event_properties = __assign(__assign({}, (event.event_properties || {})), { '[Amplitude] Page Domain': addIfNotExist(event, '[Amplitude] Page Domain', (typeof location !== 'undefined' && location.hostname) || ''), '[Amplitude] Page Location': addIfNotExist(event, '[Amplitude] Page Location', locationHREF), '[Amplitude] Page Path': addIfNotExist(event, '[Amplitude] Page Path', (typeof location !== 'undefined' && getDecodeURI(location.pathname)) || ''), '[Amplitude] Page Title': addIfNotExist(event, '[Amplitude] Page Title', getPageTitle(replaceSensitiveString)), '[Amplitude] Page URL': addIfNotExist(event, '[Amplitude] Page URL', locationHREF.split('?')[0]), '[Amplitude] Previous Page Location': previousPage, '[Amplitude] Previous Page Type': getPrevPageType(previousPage) });
181
- _b.label = 5;
182
- case 5: return [2 /*return*/, event];
197
+ _a.label = 2;
198
+ case 2: return [2 /*return*/, event];
183
199
  }
184
200
  });
185
201
  }); },
@@ -1 +1 @@
1
- {"version":3,"file":"page-url-enrichment.js","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":";AAAA,OAAO,EAGL,cAAc,EAGd,YAAY,EACZ,cAAc,EACd,YAAY,EAGZ,sBAAsB,EACtB,gBAAgB,GACjB,MAAM,2BAA2B,CAAC;AAEnC,MAAM,CAAC,IAAM,wBAAwB,GAAG,kBAAkB,CAAC;AAC3D,MAAM,CAAC,IAAM,yBAAyB,GAAG,mBAAmB,CAAC;AAC7D,MAAM,CAAC,IAAM,oBAAoB,GAAG,cAAc,CAAC;AAOnD,IAAK,gBAIJ;AAJD,WAAK,gBAAgB;IACnB,qCAAiB,CAAA;IACjB,yCAAqB,CAAA;IACrB,yCAAqB,CAAA;AACvB,CAAC,EAJI,gBAAgB,KAAhB,gBAAgB,QAIpB;AAED,MAAM,CAAC,IAAM,4BAA4B,GAAG,IAAI,GAAG,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C,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAChG,oCAAoC,EAAE,YAAY,EAClD,gCAAgC,EAAE,eAAe,CAAC,YAAY,CAAC,GAChE,CAAC;;4BAGJ,sBAAO,KAAK,EAAC;;;aACd;QACD,QAAQ,EAAE;;;;wBACR,IAAI,WAAW,EAAE;4BACf,WAAW,CAAC,mBAAmB,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;4BAEhE,UAAU,GAAG,KAAK,CAAC;yBACpB;6BAEG,CAAA,cAAc,IAAI,gBAAgB,CAAA,EAAlC,wBAAkC;wBACpC,qBAAM,cAAc,CAAC,GAAG,CAAC,oBAAoB,EAAE,EAAE,CAAC,EAAA;;wBAAlD,SAAkD,CAAC;;;;;aAEtD;KACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF,SAAS,aAAa,CAAC,KAAY,EAAE,GAAW,EAAE,KAAa;IAC7D,IAAI,CAAC,KAAK,CAAC,gBAAgB,EAAE;QAC3B,KAAK,CAAC,gBAAgB,GAAG,EAAE,CAAC;KAC7B;IAED,IAAK,KAAK,CAAC,gBAA2C,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;QACzE,OAAO,KAAK,CAAC;KACd;IAED,OAAQ,KAAK,CAAC,gBAA2C,CAAC,GAAG,CAAW,CAAC;AAC3E,CAAC","sourcesContent":["import {\n type BrowserClient,\n type BrowserConfig,\n BrowserStorage,\n type EnrichmentPlugin,\n type Event,\n getDecodeURI,\n getGlobalScope,\n getPageTitle,\n type ILogger,\n type PageUrlEnrichmentOptions,\n replaceSensitiveString,\n SpecialEventType,\n} from '@amplitude/analytics-core';\n\nexport const CURRENT_PAGE_STORAGE_KEY = 'AMP_CURRENT_PAGE';\nexport const PREVIOUS_PAGE_STORAGE_KEY = 'AMP_PREVIOUS_PAGE';\nexport const URL_INFO_STORAGE_KEY = 'AMP_URL_INFO';\n\nexport type URLInfo = {\n [CURRENT_PAGE_STORAGE_KEY]?: string;\n [PREVIOUS_PAGE_STORAGE_KEY]?: string;\n};\n\nenum PreviousPageType {\n Direct = 'direct', // for no prev page or referrer\n Internal = 'internal', // for internal domains - exact domain matches or when current and previous page are both internal domains\n External = 'external', // for different domains\n}\n\nexport const EXCLUDED_DEFAULT_EVENT_TYPES = new Set<string>([\n SpecialEventType.IDENTIFY,\n SpecialEventType.GROUP_IDENTIFY,\n SpecialEventType.REVENUE,\n]);\n\nexport const isPageUrlEnrichmentEnabled = (option: unknown): boolean => {\n if (typeof option === 'boolean') {\n return option;\n }\n if (typeof option === 'object' && option !== null && 'pageUrlEnrichment' in option) {\n return Boolean((option as { pageUrlEnrichment?: boolean }).pageUrlEnrichment);\n }\n return false;\n};\n\nexport const pageUrlEnrichmentPlugin = ({ internalDomains = [] }: PageUrlEnrichmentOptions = {}): EnrichmentPlugin => {\n const globalScope = getGlobalScope();\n let sessionStorage: BrowserStorage<URLInfo> | undefined = undefined;\n let isStorageEnabled = false;\n let loggerProvider: ILogger | undefined = undefined;\n\n let isProxied = false;\n let isTracking = false;\n\n const getHostname = (url: string): string | undefined => {\n let hostname: string | undefined;\n\n try {\n const decodedUrl = getDecodeURI(url, loggerProvider);\n hostname = new URL(decodedUrl).hostname;\n } catch (e) {\n /* istanbul ignore next */\n loggerProvider?.error('Could not parse URL: ', e);\n }\n\n return hostname;\n };\n\n const getPrevPageType = (previousPage: string) => {\n const currentDomain = (typeof location !== 'undefined' && location.hostname) || '';\n const previousPageDomain = previousPage ? getHostname(previousPage) : undefined;\n\n if (!previousPageDomain) {\n return PreviousPageType.Direct;\n }\n\n const isCurrentInternal = internalDomains.some((domain) => currentDomain.indexOf(domain) !== -1);\n const isPrevInternal = internalDomains.some((domain) => previousPageDomain.indexOf(domain) !== -1);\n\n if (currentDomain === previousPageDomain || (isPrevInternal && isCurrentInternal)) {\n return PreviousPageType.Internal;\n }\n\n return PreviousPageType.External;\n };\n\n const saveURLInfo = async () => {\n if (sessionStorage && isStorageEnabled) {\n const URLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n const currentURL = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n const storedCurrentURL = URLInfo?.[CURRENT_PAGE_STORAGE_KEY] || '';\n\n let previousURL: string | undefined;\n if (currentURL === storedCurrentURL) {\n previousURL = URLInfo?.[PREVIOUS_PAGE_STORAGE_KEY] || '';\n } else if (storedCurrentURL) {\n previousURL = storedCurrentURL;\n } else {\n previousURL = document.referrer || '';\n }\n\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {\n [CURRENT_PAGE_STORAGE_KEY]: currentURL,\n [PREVIOUS_PAGE_STORAGE_KEY]: previousURL,\n });\n }\n };\n\n const saveUrlInfoWrapper = () => {\n void saveURLInfo();\n };\n\n const plugin: EnrichmentPlugin = {\n name: '@amplitude/plugin-page-url-enrichment-browser',\n type: 'enrichment',\n\n setup: async (config: BrowserConfig, _: BrowserClient) => {\n loggerProvider = config.loggerProvider;\n loggerProvider.log('Installing @amplitude/plugin-page-url-enrichment-browser');\n\n isTracking = true;\n\n if (globalScope) {\n try {\n sessionStorage = new BrowserStorage<URLInfo>(globalScope.sessionStorage);\n } catch (error) {\n /* istanbul ignore next */\n loggerProvider?.debug('sessionStorage is not available in this environment.');\n }\n isStorageEnabled = (await sessionStorage?.isEnabled()) ?? false;\n\n globalScope.addEventListener('popstate', saveUrlInfoWrapper);\n\n if (!isProxied) {\n /* istanbul ignore next */\n // There is no global browser listener for changes to history, so we have\n // to modify pushState and replaceState directly.\n // https://stackoverflow.com/a/64927639\n // eslint-disable-next-line @typescript-eslint/unbound-method\n globalScope.history.pushState = new Proxy(globalScope.history.pushState, {\n apply: (target, thisArg, [state, unused, url]) => {\n target.apply(thisArg, [state, unused, url]);\n if (isTracking) {\n saveUrlInfoWrapper();\n }\n },\n });\n\n // eslint-disable-next-line @typescript-eslint/unbound-method\n globalScope.history.replaceState = new Proxy(globalScope.history.replaceState, {\n apply: (target, thisArg, [state, unused, url]) => {\n target.apply(thisArg, [state, unused, url]);\n if (isTracking) {\n saveUrlInfoWrapper();\n }\n },\n });\n\n isProxied = true;\n }\n }\n },\n execute: async (event: Event) => {\n const locationHREF = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n\n if (sessionStorage && isStorageEnabled) {\n const URLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n if (!URLInfo?.[CURRENT_PAGE_STORAGE_KEY]) {\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {\n [CURRENT_PAGE_STORAGE_KEY]: locationHREF,\n [PREVIOUS_PAGE_STORAGE_KEY]: document.referrer || '',\n });\n }\n\n const updatedURLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n let previousPage = '';\n if (updatedURLInfo) {\n previousPage = updatedURLInfo[PREVIOUS_PAGE_STORAGE_KEY] || '';\n }\n\n // no need to proceed to add additional properties if the event is one of the default event types to be excluded\n if (EXCLUDED_DEFAULT_EVENT_TYPES.has(event.event_type)) {\n return event;\n }\n\n event.event_properties = {\n ...(event.event_properties || {}),\n '[Amplitude] Page Domain': addIfNotExist(\n event,\n '[Amplitude] Page Domain',\n (typeof location !== 'undefined' && location.hostname) || '',\n ),\n '[Amplitude] Page Location': addIfNotExist(event, '[Amplitude] Page Location', locationHREF),\n '[Amplitude] Page Path': addIfNotExist(\n event,\n '[Amplitude] Page Path',\n (typeof location !== 'undefined' && getDecodeURI(location.pathname)) || '',\n ),\n '[Amplitude] Page Title': addIfNotExist(\n event,\n '[Amplitude] Page Title',\n getPageTitle(replaceSensitiveString),\n ),\n '[Amplitude] Page URL': addIfNotExist(event, '[Amplitude] Page URL', locationHREF.split('?')[0]),\n '[Amplitude] Previous Page Location': previousPage,\n '[Amplitude] Previous Page Type': getPrevPageType(previousPage),\n };\n }\n\n return event;\n },\n teardown: async () => {\n if (globalScope) {\n globalScope.removeEventListener('popstate', saveUrlInfoWrapper);\n\n isTracking = false;\n }\n\n if (sessionStorage && isStorageEnabled) {\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {});\n }\n },\n };\n\n return plugin;\n};\n\nfunction addIfNotExist(event: Event, key: string, value: string): string {\n if (!event.event_properties) {\n event.event_properties = {};\n }\n\n if ((event.event_properties as { [key: string]: any })[key] === undefined) {\n return value;\n }\n\n return (event.event_properties as { [key: string]: any })[key] as string;\n}\n"]}
1
+ {"version":3,"file":"page-url-enrichment.js","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":";AAAA,OAAO,EAGL,cAAc,EAGd,YAAY,EACZ,cAAc,EACd,YAAY,EAGZ,sBAAsB,EACtB,gBAAgB,GACjB,MAAM,2BAA2B,CAAC;AAEnC,MAAM,CAAC,IAAM,wBAAwB,GAAG,kBAAkB,CAAC;AAC3D,MAAM,CAAC,IAAM,yBAAyB,GAAG,mBAAmB,CAAC;AAC7D,MAAM,CAAC,IAAM,oBAAoB,GAAG,cAAc,CAAC;AAOnD,IAAK,gBAIJ;AAJD,WAAK,gBAAgB;IACnB,qCAAiB,CAAA;IACjB,yCAAqB,CAAA;IACrB,yCAAqB,CAAA;AACvB,CAAC,EAJI,gBAAgB,KAAhB,gBAAgB,QAIpB;AAED,MAAM,CAAC,IAAM,4BAA4B,GAAG,IAAI,GAAG,CAAS;IAC1D,gBAAgB,CAAC,QAAQ;IACzB,gBAAgB,CAAC,cAAc;IAC/B,gBAAgB,CAAC,OAAO;CACzB,CAAC,CAAC;AAEH,MAAM,CAAC,IAAM,0BAA0B,GAAG,UAAC,MAAe;IACxD,IAAI,OAAO,MAAM,KAAK,SAAS,EAAE;QAC/B,OAAO,MAAM,CAAC;KACf;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,mBAAmB,IAAI,MAAM,EAAE;QAClF,OAAO,OAAO,CAAE,MAA0C,CAAC,iBAAiB,CAAC,CAAC;KAC/E;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF,MAAM,CAAC,IAAM,uBAAuB,GAAG,UAAC,EAAuD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AQ,KAAK,WAAW,IAAI,YAAY,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,IAAI,EAAE,CAC3E,EACD,wBAAwB,EAAE,aAAa,CACrC,KAAK,EACL,wBAAwB,EACxB,YAAY,CAAC,sBAAsB,CAAC,CACrC,EACD,sBAAsB,EAAE,aAAa,CAAC,KAAK,EAAE,sBAAsB,EAAE,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAChG,oCAAoC,EAAE,YAAY,EAClD,gCAAgC,EAAE,eAAe,CAAC,YAAY,CAAC,GAChE,CAAC;;4BAGJ,sBAAO,KAAK,EAAC;;;aACd;QACD,QAAQ,EAAE;;;;wBACR,IAAI,WAAW,EAAE;4BACf,WAAW,CAAC,mBAAmB,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;4BAEhE,UAAU,GAAG,KAAK,CAAC;yBACpB;6BAEG,CAAA,cAAc,IAAI,gBAAgB,CAAA,EAAlC,wBAAkC;wBACpC,qBAAM,cAAc,CAAC,GAAG,CAAC,oBAAoB,EAAE,EAAE,CAAC,EAAA;;wBAAlD,SAAkD,CAAC;;;;;aAEtD;KACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF,SAAS,aAAa,CAAC,KAAY,EAAE,GAAW,EAAE,KAAa;IAC7D,IAAI,CAAC,KAAK,CAAC,gBAAgB,EAAE;QAC3B,KAAK,CAAC,gBAAgB,GAAG,EAAE,CAAC;KAC7B;IAED,IAAK,KAAK,CAAC,gBAA2C,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;QACzE,OAAO,KAAK,CAAC;KACd;IAED,OAAQ,KAAK,CAAC,gBAA2C,CAAC,GAAG,CAAW,CAAC;AAC3E,CAAC","sourcesContent":["import {\n type BrowserClient,\n type BrowserConfig,\n BrowserStorage,\n type EnrichmentPlugin,\n type Event,\n getDecodeURI,\n getGlobalScope,\n getPageTitle,\n type ILogger,\n type PageUrlEnrichmentOptions,\n replaceSensitiveString,\n SpecialEventType,\n} from '@amplitude/analytics-core';\n\nexport const CURRENT_PAGE_STORAGE_KEY = 'AMP_CURRENT_PAGE';\nexport const PREVIOUS_PAGE_STORAGE_KEY = 'AMP_PREVIOUS_PAGE';\nexport const URL_INFO_STORAGE_KEY = 'AMP_URL_INFO';\n\nexport type URLInfo = {\n [CURRENT_PAGE_STORAGE_KEY]?: string;\n [PREVIOUS_PAGE_STORAGE_KEY]?: string;\n};\n\nenum PreviousPageType {\n Direct = 'direct', // for no prev page or referrer\n Internal = 'internal', // for internal domains - exact domain matches or when current and previous page are both internal domains\n External = 'external', // for different domains\n}\n\nexport const EXCLUDED_DEFAULT_EVENT_TYPES = new Set<string>([\n SpecialEventType.IDENTIFY,\n SpecialEventType.GROUP_IDENTIFY,\n SpecialEventType.REVENUE,\n]);\n\nexport const isPageUrlEnrichmentEnabled = (option: unknown): boolean => {\n if (typeof option === 'boolean') {\n return option;\n }\n if (typeof option === 'object' && option !== null && 'pageUrlEnrichment' in option) {\n return Boolean((option as { pageUrlEnrichment?: boolean }).pageUrlEnrichment);\n }\n return false;\n};\n\nexport const pageUrlEnrichmentPlugin = ({ internalDomains = [] }: PageUrlEnrichmentOptions = {}): EnrichmentPlugin => {\n const globalScope = getGlobalScope();\n let sessionStorage: BrowserStorage<URLInfo> | undefined = undefined;\n let isStorageEnabled = false;\n let loggerProvider: ILogger | undefined = undefined;\n\n let isProxied = false;\n let isTracking = false;\n\n const getHostname = (url: string): string | undefined => {\n let hostname: string | undefined;\n\n try {\n const decodedUrl = getDecodeURI(url, loggerProvider);\n hostname = new URL(decodedUrl).hostname;\n } catch (e) {\n /* istanbul ignore next */\n loggerProvider?.error('Could not parse URL: ', e);\n }\n\n return hostname;\n };\n\n const getPrevPageType = (previousPage: string) => {\n const currentDomain = (typeof location !== 'undefined' && location.hostname) || '';\n const previousPageDomain = previousPage ? getHostname(previousPage) : undefined;\n\n if (!previousPageDomain) {\n return PreviousPageType.Direct;\n }\n\n const isCurrentInternal = internalDomains.some((domain) => currentDomain.indexOf(domain) !== -1);\n const isPrevInternal = internalDomains.some((domain) => previousPageDomain.indexOf(domain) !== -1);\n\n if (currentDomain === previousPageDomain || (isPrevInternal && isCurrentInternal)) {\n return PreviousPageType.Internal;\n }\n\n return PreviousPageType.External;\n };\n\n const saveURLInfo = async () => {\n if (sessionStorage && isStorageEnabled) {\n const URLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n const currentURL = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n const storedCurrentURL = URLInfo?.[CURRENT_PAGE_STORAGE_KEY] || '';\n\n let previousURL: string | undefined;\n if (currentURL === storedCurrentURL) {\n previousURL = URLInfo?.[PREVIOUS_PAGE_STORAGE_KEY] || '';\n } else if (storedCurrentURL) {\n previousURL = storedCurrentURL;\n } else {\n previousURL = document.referrer || '';\n }\n\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {\n [CURRENT_PAGE_STORAGE_KEY]: currentURL,\n [PREVIOUS_PAGE_STORAGE_KEY]: previousURL,\n });\n }\n };\n\n const saveUrlInfoWrapper = () => {\n void saveURLInfo();\n };\n\n const plugin: EnrichmentPlugin = {\n name: '@amplitude/plugin-page-url-enrichment-browser',\n type: 'enrichment',\n\n setup: async (config: BrowserConfig, _: BrowserClient) => {\n loggerProvider = config.loggerProvider;\n loggerProvider.log('Installing @amplitude/plugin-page-url-enrichment-browser');\n\n isTracking = true;\n\n if (globalScope) {\n try {\n sessionStorage = new BrowserStorage<URLInfo>(globalScope.sessionStorage);\n } catch (error) {\n /* istanbul ignore next */\n loggerProvider?.debug('sessionStorage is not available in this environment.');\n }\n isStorageEnabled = (await sessionStorage?.isEnabled()) ?? false;\n\n if (sessionStorage && isStorageEnabled) {\n const referrer = (typeof document !== 'undefined' && document.referrer) || '';\n const referrerHostname = referrer ? getHostname(referrer) : undefined;\n const currentHostname = (typeof location !== 'undefined' && location.hostname) || '';\n const currentURL = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n\n const existingURLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n const storedCurrentURL = existingURLInfo?.[CURRENT_PAGE_STORAGE_KEY] || '';\n\n // document.referrer is preserved across pushState navigations and full-page\n // reloads, so a different referrer hostname alone does not prove an external\n // arrival. Also require the stored \"current\" to differ from location.href —\n // that's what distinguishes a genuine new landing in this tab from a refresh\n // of an already-tracked page (where storage holds the truthful previous).\n const arrivedFromDifferentOrigin =\n !!referrerHostname && referrerHostname !== currentHostname && storedCurrentURL !== currentURL;\n\n if (arrivedFromDifferentOrigin) {\n // sessionStorage survives same-tab cross-origin trips (siteA -> external -> siteA),\n // leaving stale \"current\" that would wrongly become \"previous\". document.referrer\n // is the truthful previous, so overwrite — at the cost of dropping the prior siteA page.\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {\n [CURRENT_PAGE_STORAGE_KEY]: currentURL,\n [PREVIOUS_PAGE_STORAGE_KEY]: referrer,\n });\n } else {\n await saveURLInfo();\n }\n }\n\n globalScope.addEventListener('popstate', saveUrlInfoWrapper);\n\n if (!isProxied) {\n /* istanbul ignore next */\n // There is no global browser listener for changes to history, so we have\n // to modify pushState and replaceState directly.\n // https://stackoverflow.com/a/64927639\n // eslint-disable-next-line @typescript-eslint/unbound-method\n globalScope.history.pushState = new Proxy(globalScope.history.pushState, {\n apply: (target, thisArg, [state, unused, url]) => {\n target.apply(thisArg, [state, unused, url]);\n if (isTracking) {\n saveUrlInfoWrapper();\n }\n },\n });\n\n // eslint-disable-next-line @typescript-eslint/unbound-method\n globalScope.history.replaceState = new Proxy(globalScope.history.replaceState, {\n apply: (target, thisArg, [state, unused, url]) => {\n target.apply(thisArg, [state, unused, url]);\n if (isTracking) {\n saveUrlInfoWrapper();\n }\n },\n });\n\n isProxied = true;\n }\n }\n },\n execute: async (event: Event) => {\n const locationHREF = getDecodeURI((typeof location !== 'undefined' && location.href) || '');\n\n if (sessionStorage && isStorageEnabled) {\n // setup() seeds AMP_URL_INFO via saveURLInfo(), and pushState/replaceState/popstate\n // keep it in sync, so we just read the previous page here.\n const URLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);\n const previousPage = URLInfo?.[PREVIOUS_PAGE_STORAGE_KEY] || '';\n\n // no need to proceed to add additional properties if the event is one of the default event types to be excluded\n if (EXCLUDED_DEFAULT_EVENT_TYPES.has(event.event_type)) {\n return event;\n }\n\n event.event_properties = {\n ...(event.event_properties || {}),\n '[Amplitude] Page Domain': addIfNotExist(\n event,\n '[Amplitude] Page Domain',\n (typeof location !== 'undefined' && location.hostname) || '',\n ),\n '[Amplitude] Page Location': addIfNotExist(event, '[Amplitude] Page Location', locationHREF),\n '[Amplitude] Page Path': addIfNotExist(\n event,\n '[Amplitude] Page Path',\n (typeof location !== 'undefined' && getDecodeURI(location.pathname)) || '',\n ),\n '[Amplitude] Page Title': addIfNotExist(\n event,\n '[Amplitude] Page Title',\n getPageTitle(replaceSensitiveString),\n ),\n '[Amplitude] Page URL': addIfNotExist(event, '[Amplitude] Page URL', locationHREF.split('?')[0]),\n '[Amplitude] Previous Page Location': previousPage,\n '[Amplitude] Previous Page Type': getPrevPageType(previousPage),\n };\n }\n\n return event;\n },\n teardown: async () => {\n if (globalScope) {\n globalScope.removeEventListener('popstate', saveUrlInfoWrapper);\n\n isTracking = false;\n }\n\n if (sessionStorage && isStorageEnabled) {\n await sessionStorage.set(URL_INFO_STORAGE_KEY, {});\n }\n },\n };\n\n return plugin;\n};\n\nfunction addIfNotExist(event: Event, key: string, value: string): string {\n if (!event.event_properties) {\n event.event_properties = {};\n }\n\n if ((event.event_properties as { [key: string]: any })[key] === undefined) {\n return value;\n }\n\n return (event.event_properties as { [key: string]: any })[key] as string;\n}\n"]}
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).amplitude={})}(this,function(e){"use strict";var t,n,r=function(){return r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},r.apply(this,arguments)};function o(e,t,n,r){return new(n||(n=Promise))(function(o,i){function a(e){try{l(r.next(e))}catch(e){i(e)}}function u(e){try{l(r.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(a,u)}l((r=r.apply(e,t||[])).next())})}function i(e,t){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=u(0),a.throw=u(1),a.return=u(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function u(u){return function(l){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,u[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&u[0]?r.return:u[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,u[1])).done)return o;switch(r=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return i.label++,{value:u[1],done:!1};case 5:i.label++,r=u[1],u=[0];continue;case 7:u=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){i=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){i.label=u[1];break}if(6===u[0]&&i.label<o[1]){i.label=o[1],o=u;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(u);break}o[2]&&i.ops.pop(),i.trys.pop();continue}u=t.call(e,i)}catch(e){u=[6,e],r=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,l])}}}function a(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)a.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return a}"function"==typeof SuppressedError&&SuppressedError,function(e){e.SET="$set",e.SET_ONCE="$setOnce",e.ADD="$add",e.APPEND="$append",e.PREPEND="$prepend",e.REMOVE="$remove",e.PREINSERT="$preInsert",e.POSTINSERT="$postInsert",e.UNSET="$unset",e.CLEAR_ALL="$clearAll"}(t||(t={})),function(e){e.IDENTIFY="$identify",e.GROUP_IDENTIFY="$groupidentify",e.REVENUE="revenue_amount"}(n||(n={}));var u,l=function(e,t){var n=e;try{n=decodeURI(e)}catch(e){null==t||t.error("Malformed URI sequence: ",e)}return n},c=function(){function e(e){this.storage=e}return e.prototype.isEnabled=function(){return o(this,void 0,void 0,function(){var t,n,r;return i(this,function(o){switch(o.label){case 0:if(!this.storage)return[2,!1];t=String(Date.now()),n=new e(this.storage),r="AMP_TEST",o.label=1;case 1:return o.trys.push([1,4,5,7]),[4,n.set(r,t)];case 2:return o.sent(),[4,n.get(r)];case 3:return[2,o.sent()===t];case 4:return o.sent(),[2,!1];case 5:return[4,n.remove(r)];case 6:return o.sent(),[7];case 7:return[2]}})})},e.prototype.get=function(e){return o(this,void 0,void 0,function(){var t;return i(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,this.getRaw(e)];case 1:return(t=n.sent())?[2,JSON.parse(t)]:[2,void 0];case 2:return n.sent(),console.error("[Amplitude] Error: Could not get value from storage"),[2,void 0];case 3:return[2]}})})},e.prototype.getRaw=function(e){var t;return o(this,void 0,void 0,function(){return i(this,function(n){return[2,(null===(t=this.storage)||void 0===t?void 0:t.getItem(e))||void 0]})})},e.prototype.set=function(e,t){var n;return o(this,void 0,void 0,function(){return i(this,function(r){try{null===(n=this.storage)||void 0===n||n.setItem(e,JSON.stringify(t))}catch(e){}return[2]})})},e.prototype.remove=function(e){var t;return o(this,void 0,void 0,function(){return i(this,function(n){try{null===(t=this.storage)||void 0===t||t.removeItem(e)}catch(e){}return[2]})})},e.prototype.reset=function(){var e;return o(this,void 0,void 0,function(){return i(this,function(t){try{null===(e=this.storage)||void 0===e||e.clear()}catch(e){}return[2]})})},e}(),s="*****",f=/\b(?:\d[ -]*?){13,16}\b/,d=/(\d{3}-?\d{2}-?\d{4})/g,p=/[^\s@]+@[^\s@.]+\.[^\s@]+/g,v=function(e,t){var n,r;if(void 0===t&&(t=[]),"string"!=typeof e)return"";var o=e;o=(o=(o=o.replace(f,s)).replace(d,s)).replace(p,s);try{for(var i=function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(t),a=i.next();!a.done;a=i.next()){var u=a.value;try{o=o.replace(u,s)}catch(e){}}}catch(e){n={error:e}}finally{try{a&&!a.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return o},h=function(e){if("undefined"==typeof document||!document.title)return"";var t=document.querySelector("title");return t&&t.hasAttribute("data-amp-mask")?s:e?e(document.title):document.title},y="AMP_CURRENT_PAGE",g="AMP_PREVIOUS_PAGE",m="AMP_URL_INFO";!function(e){e.Direct="direct",e.Internal="internal",e.External="external"}(u||(u={}));var b=new Set([n.IDENTIFY,n.GROUP_IDENTIFY,n.REVENUE]),E=function(e){var t,n=(void 0===e?{}:e).internalDomains,s=void 0===n?[]:n,f=(t="ampIntegrationContext","undefined"!=typeof globalThis&&void 0!==globalThis[t]?globalThis[t]:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:void 0),d=void 0,p=!1,E=void 0,w=!1,S=!1,A=function(e){var t="undefined"!=typeof location&&location.hostname||"",n=e?function(e){var t;try{var n=l(e,E);t=new URL(n).hostname}catch(e){null==E||E.error("Could not parse URL: ",e)}return t}(e):void 0;if(!n)return u.Direct;var r=s.some(function(e){return-1!==t.indexOf(e)}),o=s.some(function(e){return-1!==n.indexOf(e)});return t===n||o&&r?u.Internal:u.External},_=function(){o(void 0,void 0,void 0,function(){var e,t,n,r,o;return i(this,function(i){switch(i.label){case 0:return d&&p?[4,d.get(m)]:[3,3];case 1:return e=i.sent(),t=l("undefined"!=typeof location&&location.href||""),n=(null==e?void 0:e[y])||"",r=void 0,r=t===n?(null==e?void 0:e[g])||"":n||document.referrer||"",[4,d.set(m,(o={},o[y]=t,o[g]=r,o))];case 2:i.sent(),i.label=3;case 3:return[2]}})})},R={name:"@amplitude/plugin-page-url-enrichment-browser",type:"enrichment",setup:function(e,t){return o(void 0,void 0,void 0,function(){var t;return i(this,function(n){switch(n.label){case 0:if((E=e.loggerProvider).log("Installing @amplitude/plugin-page-url-enrichment-browser"),S=!0,!f)return[3,2];try{d=new c(f.sessionStorage)}catch(e){null==E||E.debug("sessionStorage is not available in this environment.")}return[4,null==d?void 0:d.isEnabled()];case 1:p=null!==(t=n.sent())&&void 0!==t&&t,f.addEventListener("popstate",_),w||(f.history.pushState=new Proxy(f.history.pushState,{apply:function(e,t,n){var r=a(n,3),o=r[0],i=r[1],u=r[2];e.apply(t,[o,i,u]),S&&_()}}),f.history.replaceState=new Proxy(f.history.replaceState,{apply:function(e,t,n){var r=a(n,3),o=r[0],i=r[1],u=r[2];e.apply(t,[o,i,u]),S&&_()}}),w=!0),n.label=2;case 2:return[2]}})})},execute:function(e){return o(void 0,void 0,void 0,function(){var t,n,o,a,u;return i(this,function(i){switch(i.label){case 0:return t=l("undefined"!=typeof location&&location.href||""),d&&p?[4,d.get(m)]:[3,5];case 1:return(null==(n=i.sent())?void 0:n[y])?[3,3]:[4,d.set(m,(u={},u[y]=t,u[g]=document.referrer||"",u))];case 2:i.sent(),i.label=3;case 3:return[4,d.get(m)];case 4:if(o=i.sent(),a="",o&&(a=o[g]||""),b.has(e.event_type))return[2,e];e.event_properties=r(r({},e.event_properties||{}),{"[Amplitude] Page Domain":P(e,"[Amplitude] Page Domain","undefined"!=typeof location&&location.hostname||""),"[Amplitude] Page Location":P(e,"[Amplitude] Page Location",t),"[Amplitude] Page Path":P(e,"[Amplitude] Page Path","undefined"!=typeof location&&l(location.pathname)||""),"[Amplitude] Page Title":P(e,"[Amplitude] Page Title",h(v)),"[Amplitude] Page URL":P(e,"[Amplitude] Page URL",t.split("?")[0]),"[Amplitude] Previous Page Location":a,"[Amplitude] Previous Page Type":A(a)}),i.label=5;case 5:return[2,e]}})})},teardown:function(){return o(void 0,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return f&&(f.removeEventListener("popstate",_),S=!1),d&&p?[4,d.set(m,{})]:[3,2];case 1:e.sent(),e.label=2;case 2:return[2]}})})}};return R};function P(e,t,n){return e.event_properties||(e.event_properties={}),void 0===e.event_properties[t]?n:e.event_properties[t]}e.CURRENT_PAGE_STORAGE_KEY=y,e.PREVIOUS_PAGE_STORAGE_KEY=g,e.URL_INFO_STORAGE_KEY=m,e.pageUrlEnrichmentPlugin=E,e.plugin=E,Object.defineProperty(e,"__esModule",{value:!0})});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).amplitude={})}(this,function(e){"use strict";var t,n,r=function(){return r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},r.apply(this,arguments)};function o(e,t,n,r){return new(n||(n=Promise))(function(o,i){function a(e){try{l(r.next(e))}catch(e){i(e)}}function u(e){try{l(r.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(a,u)}l((r=r.apply(e,t||[])).next())})}function i(e,t){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=u(0),a.throw=u(1),a.return=u(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function u(u){return function(l){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,u[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&u[0]?r.return:u[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,u[1])).done)return o;switch(r=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return i.label++,{value:u[1],done:!1};case 5:i.label++,r=u[1],u=[0];continue;case 7:u=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){i=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){i.label=u[1];break}if(6===u[0]&&i.label<o[1]){i.label=o[1],o=u;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(u);break}o[2]&&i.ops.pop(),i.trys.pop();continue}u=t.call(e,i)}catch(e){u=[6,e],r=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,l])}}}function a(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)a.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return a}"function"==typeof SuppressedError&&SuppressedError,function(e){e.SET="$set",e.SET_ONCE="$setOnce",e.ADD="$add",e.APPEND="$append",e.PREPEND="$prepend",e.REMOVE="$remove",e.PREINSERT="$preInsert",e.POSTINSERT="$postInsert",e.UNSET="$unset",e.CLEAR_ALL="$clearAll"}(t||(t={})),function(e){e.IDENTIFY="$identify",e.GROUP_IDENTIFY="$groupidentify",e.REVENUE="revenue_amount"}(n||(n={}));var u,l=function(e,t){var n=e;try{n=decodeURI(e)}catch(e){null==t||t.error("Malformed URI sequence: ",e)}return n},c=function(){function e(e){this.storage=e}return e.prototype.isEnabled=function(){return o(this,void 0,void 0,function(){var t,n,r;return i(this,function(o){switch(o.label){case 0:if(!this.storage)return[2,!1];t=String(Date.now()),n=new e(this.storage),r="AMP_TEST",o.label=1;case 1:return o.trys.push([1,4,5,7]),[4,n.set(r,t)];case 2:return o.sent(),[4,n.get(r)];case 3:return[2,o.sent()===t];case 4:return o.sent(),[2,!1];case 5:return[4,n.remove(r)];case 6:return o.sent(),[7];case 7:return[2]}})})},e.prototype.get=function(e){return o(this,void 0,void 0,function(){var t;return i(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,this.getRaw(e)];case 1:return(t=n.sent())?[2,JSON.parse(t)]:[2,void 0];case 2:return n.sent(),console.error("[Amplitude] Error: Could not get value from storage"),[2,void 0];case 3:return[2]}})})},e.prototype.getRaw=function(e){var t;return o(this,void 0,void 0,function(){return i(this,function(n){return[2,(null===(t=this.storage)||void 0===t?void 0:t.getItem(e))||void 0]})})},e.prototype.set=function(e,t){var n;return o(this,void 0,void 0,function(){return i(this,function(r){try{null===(n=this.storage)||void 0===n||n.setItem(e,JSON.stringify(t))}catch(e){}return[2]})})},e.prototype.remove=function(e){var t;return o(this,void 0,void 0,function(){return i(this,function(n){try{null===(t=this.storage)||void 0===t||t.removeItem(e)}catch(e){}return[2]})})},e.prototype.reset=function(){var e;return o(this,void 0,void 0,function(){return i(this,function(t){try{null===(e=this.storage)||void 0===e||e.clear()}catch(e){}return[2]})})},e}(),s="*****",f=/\b(?:\d[ -]*?){13,16}\b/,d=/(\d{3}-?\d{2}-?\d{4})/g,p=/[^\s@]+@[^\s@.]+\.[^\s@]+/g,v=function(e,t){var n,r;if(void 0===t&&(t=[]),"string"!=typeof e)return"";var o=e;o=(o=(o=o.replace(f,s)).replace(d,s)).replace(p,s);try{for(var i=function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(t),a=i.next();!a.done;a=i.next()){var u=a.value;try{o=o.replace(u,s)}catch(e){}}}catch(e){n={error:e}}finally{try{a&&!a.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return o},h=function(e){if("undefined"==typeof document||!document.title)return"";var t=document.querySelector("title");return t&&t.hasAttribute("data-amp-mask")?s:e?e(document.title):document.title},y="AMP_CURRENT_PAGE",g="AMP_PREVIOUS_PAGE",m="AMP_URL_INFO";!function(e){e.Direct="direct",e.Internal="internal",e.External="external"}(u||(u={}));var b=new Set([n.IDENTIFY,n.GROUP_IDENTIFY,n.REVENUE]),E=function(e){var t,n=(void 0===e?{}:e).internalDomains,s=void 0===n?[]:n,f=(t="ampIntegrationContext","undefined"!=typeof globalThis&&void 0!==globalThis[t]?globalThis[t]:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:void 0),d=void 0,p=!1,E=void 0,w=!1,S=!1,A=function(e){var t;try{var n=l(e,E);t=new URL(n).hostname}catch(e){null==E||E.error("Could not parse URL: ",e)}return t},_=function(e){var t="undefined"!=typeof location&&location.hostname||"",n=e?A(e):void 0;if(!n)return u.Direct;var r=s.some(function(e){return-1!==t.indexOf(e)}),o=s.some(function(e){return-1!==n.indexOf(e)});return t===n||o&&r?u.Internal:u.External},R=function(){return o(void 0,void 0,void 0,function(){var e,t,n,r,o;return i(this,function(i){switch(i.label){case 0:return d&&p?[4,d.get(m)]:[3,3];case 1:return e=i.sent(),t=l("undefined"!=typeof location&&location.href||""),n=(null==e?void 0:e[y])||"",r=void 0,r=t===n?(null==e?void 0:e[g])||"":n||(document.referrer||""),[4,d.set(m,(o={},o[y]=t,o[g]=r,o))];case 2:i.sent(),i.label=3;case 3:return[2]}})})},T=function(){R()},I={name:"@amplitude/plugin-page-url-enrichment-browser",type:"enrichment",setup:function(e,t){return o(void 0,void 0,void 0,function(){var t,n,r,o,u,s,v,h;return i(this,function(i){switch(i.label){case 0:if((E=e.loggerProvider).log("Installing @amplitude/plugin-page-url-enrichment-browser"),S=!0,!f)return[3,7];try{d=new c(f.sessionStorage)}catch(e){null==E||E.debug("sessionStorage is not available in this environment.")}return[4,null==d?void 0:d.isEnabled()];case 1:return p=null!==(h=i.sent())&&void 0!==h&&h,d&&p?(t="undefined"!=typeof document&&document.referrer||"",n=t?A(t):void 0,r="undefined"!=typeof location&&location.hostname||"",o=l("undefined"!=typeof location&&location.href||""),[4,d.get(m)]):[3,6];case 2:return u=i.sent(),s=(null==u?void 0:u[y])||"",!!n&&n!==r&&s!==o?[4,d.set(m,(v={},v[y]=o,v[g]=t,v))]:[3,4];case 3:return i.sent(),[3,6];case 4:return[4,R()];case 5:i.sent(),i.label=6;case 6:f.addEventListener("popstate",T),w||(f.history.pushState=new Proxy(f.history.pushState,{apply:function(e,t,n){var r=a(n,3),o=r[0],i=r[1],u=r[2];e.apply(t,[o,i,u]),S&&T()}}),f.history.replaceState=new Proxy(f.history.replaceState,{apply:function(e,t,n){var r=a(n,3),o=r[0],i=r[1],u=r[2];e.apply(t,[o,i,u]),S&&T()}}),w=!0),i.label=7;case 7:return[2]}})})},execute:function(e){return o(void 0,void 0,void 0,function(){var t,n,o;return i(this,function(i){switch(i.label){case 0:return t=l("undefined"!=typeof location&&location.href||""),d&&p?[4,d.get(m)]:[3,2];case 1:if(n=i.sent(),o=(null==n?void 0:n[g])||"",b.has(e.event_type))return[2,e];e.event_properties=r(r({},e.event_properties||{}),{"[Amplitude] Page Domain":P(e,"[Amplitude] Page Domain","undefined"!=typeof location&&location.hostname||""),"[Amplitude] Page Location":P(e,"[Amplitude] Page Location",t),"[Amplitude] Page Path":P(e,"[Amplitude] Page Path","undefined"!=typeof location&&l(location.pathname)||""),"[Amplitude] Page Title":P(e,"[Amplitude] Page Title",h(v)),"[Amplitude] Page URL":P(e,"[Amplitude] Page URL",t.split("?")[0]),"[Amplitude] Previous Page Location":o,"[Amplitude] Previous Page Type":_(o)}),i.label=2;case 2:return[2,e]}})})},teardown:function(){return o(void 0,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return f&&(f.removeEventListener("popstate",T),S=!1),d&&p?[4,d.set(m,{})]:[3,2];case 1:e.sent(),e.label=2;case 2:return[2]}})})}};return I};function P(e,t,n){return e.event_properties||(e.event_properties={}),void 0===e.event_properties[t]?n:e.event_properties[t]}e.CURRENT_PAGE_STORAGE_KEY=y,e.PREVIOUS_PAGE_STORAGE_KEY=g,e.URL_INFO_STORAGE_KEY=m,e.pageUrlEnrichmentPlugin=E,e.plugin=E,Object.defineProperty(e,"__esModule",{value:!0})});
Binary file
@@ -1 +1 @@
1
- {"version":3,"file":"page-url-enrichment.d.ts","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,gBAAgB,EAMrB,KAAK,wBAAwB,EAG9B,MAAM,2BAA2B,CAAC;AAEnC,eAAO,MAAM,wBAAwB,qBAAqB,CAAC;AAC3D,eAAO,MAAM,yBAAyB,sBAAsB,CAAC;AAC7D,eAAO,MAAM,oBAAoB,iBAAiB,CAAC;AAEnD,MAAM,MAAM,OAAO,GAAG;IACpB,CAAC,wBAAwB,CAAC,CAAC,EAAE,MAAM,CAAC;IACpC,CAAC,yBAAyB,CAAC,CAAC,EAAE,MAAM,CAAC;CACtC,CAAC;AAQF,eAAO,MAAM,4BAA4B,aAIvC,CAAC;AAEH,eAAO,MAAM,0BAA0B,GAAI,QAAQ,OAAO,KAAG,OAQ5D,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAI,sBAA0B,wBAA6B,KAAG,gBAoLjG,CAAC"}
1
+ {"version":3,"file":"page-url-enrichment.d.ts","sourceRoot":"","sources":["../../src/page-url-enrichment.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,gBAAgB,EAMrB,KAAK,wBAAwB,EAG9B,MAAM,2BAA2B,CAAC;AAEnC,eAAO,MAAM,wBAAwB,qBAAqB,CAAC;AAC3D,eAAO,MAAM,yBAAyB,sBAAsB,CAAC;AAC7D,eAAO,MAAM,oBAAoB,iBAAiB,CAAC;AAEnD,MAAM,MAAM,OAAO,GAAG;IACpB,CAAC,wBAAwB,CAAC,CAAC,EAAE,MAAM,CAAC;IACpC,CAAC,yBAAyB,CAAC,CAAC,EAAE,MAAM,CAAC;CACtC,CAAC;AAQF,eAAO,MAAM,4BAA4B,aAIvC,CAAC;AAEH,eAAO,MAAM,0BAA0B,WAAY,OAAO,KAAG,OAQ5D,CAAC;AAEF,eAAO,MAAM,uBAAuB,yBAA8B,wBAAwB,KAAQ,gBAyMjG,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amplitude/plugin-page-url-enrichment-browser",
3
- "version": "0.7.9",
3
+ "version": "0.7.10",
4
4
  "description": "",
5
5
  "author": "Amplitude Inc",
6
6
  "homepage": "https://github.com/amplitude/Amplitude-TypeScript",
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "tslib": "^2.4.1",
25
- "@amplitude/analytics-core": "2.48.0"
25
+ "@amplitude/analytics-core": "2.48.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@rollup/plugin-commonjs": "^23.0.4",