@amplitude/plugin-web-vitals-browser 1.1.49-video-analytics-5.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -0
- package/lib/cjs/constants.d.ts +34 -0
- package/lib/cjs/constants.d.ts.map +1 -1
- package/lib/cjs/constants.js +14 -1
- package/lib/cjs/constants.js.map +1 -1
- package/lib/cjs/index.d.ts +9 -1
- package/lib/cjs/index.d.ts.map +1 -1
- package/lib/cjs/index.js +21 -3
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/utils.d.ts +4 -0
- package/lib/cjs/utils.d.ts.map +1 -0
- package/lib/cjs/utils.js +29 -0
- package/lib/cjs/utils.js.map +1 -0
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.d.ts.map +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/cjs/version.js.map +1 -1
- package/lib/cjs/web-vitals-plugin.d.ts.map +1 -1
- package/lib/cjs/web-vitals-plugin.js +6 -22
- package/lib/cjs/web-vitals-plugin.js.map +1 -1
- package/lib/cjs/web-vitals-soft-nav-plugin.d.ts +4 -0
- package/lib/cjs/web-vitals-soft-nav-plugin.d.ts.map +1 -0
- package/lib/cjs/web-vitals-soft-nav-plugin.js +177 -0
- package/lib/cjs/web-vitals-soft-nav-plugin.js.map +1 -0
- package/lib/esm/constants.d.ts +34 -0
- package/lib/esm/constants.d.ts.map +1 -1
- package/lib/esm/constants.js +13 -0
- package/lib/esm/constants.js.map +1 -1
- package/lib/esm/index.d.ts +9 -1
- package/lib/esm/index.d.ts.map +1 -1
- package/lib/esm/index.js +20 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/utils.d.ts +4 -0
- package/lib/esm/utils.d.ts.map +1 -0
- package/lib/esm/utils.js +25 -0
- package/lib/esm/utils.js.map +1 -0
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.d.ts.map +1 -1
- package/lib/esm/version.js +1 -1
- package/lib/esm/version.js.map +1 -1
- package/lib/esm/web-vitals-plugin.d.ts.map +1 -1
- package/lib/esm/web-vitals-plugin.js +1 -17
- package/lib/esm/web-vitals-plugin.js.map +1 -1
- package/lib/esm/web-vitals-soft-nav-plugin.d.ts +4 -0
- package/lib/esm/web-vitals-soft-nav-plugin.d.ts.map +1 -0
- package/lib/esm/web-vitals-soft-nav-plugin.js +173 -0
- package/lib/esm/web-vitals-soft-nav-plugin.js.map +1 -0
- package/lib/scripts/amplitude-min.js +1 -1
- package/lib/scripts/amplitude-min.js.gz +0 -0
- package/lib/scripts/amplitude-min.js.map +1 -1
- package/lib/scripts/amplitude-min.umd.js +1 -1
- package/lib/scripts/amplitude-min.umd.js.gz +0 -0
- package/lib/scripts/constants.d.ts +34 -0
- package/lib/scripts/constants.d.ts.map +1 -1
- package/lib/scripts/index.d.ts +9 -1
- package/lib/scripts/index.d.ts.map +1 -1
- package/lib/scripts/utils.d.ts +4 -0
- package/lib/scripts/utils.d.ts.map +1 -0
- package/lib/scripts/version.d.ts +1 -1
- package/lib/scripts/version.d.ts.map +1 -1
- package/lib/scripts/web-vitals-plugin.d.ts.map +1 -1
- package/lib/scripts/web-vitals-soft-nav-plugin.d.ts +4 -0
- package/lib/scripts/web-vitals-soft-nav-plugin.d.ts.map +1 -0
- package/package.json +3 -3
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { WebVitalsOptions } from '@amplitude/analytics-core';
|
|
2
|
+
import { webVitalsSoftNavPlugin } from './web-vitals-soft-nav-plugin';
|
|
2
3
|
export { VERSION } from './version';
|
|
4
|
+
export { webVitalsSoftNavPlugin };
|
|
5
|
+
/**
|
|
6
|
+
* Creates the web vitals plugin. When `reportSoftNav` is enabled, uses the soft-navigation
|
|
7
|
+
* plugin so each navigation (including SPA route changes) is reported separately.
|
|
8
|
+
*/
|
|
9
|
+
export declare const webVitalsPlugin: (options?: WebVitalsOptions) => import("./web-vitals-plugin").BrowserEnrichmentPlugin;
|
|
10
|
+
export { webVitalsPlugin as plugin };
|
|
3
11
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAkB,MAAM,2BAA2B,CAAC;AAE7E,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AAEtE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,sBAAsB,EAAE,CAAC;AAOlC;;;GAGG;AACH,eAAO,MAAM,eAAe,aAAa,gBAAgB,0DAKxD,CAAC;AAEF,OAAO,EAAE,eAAe,IAAI,MAAM,EAAE,CAAC"}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
import { getGlobalScope } from '@amplitude/analytics-core';
|
|
2
|
+
import { webVitalsPlugin as createWebVitalsPlugin } from './web-vitals-plugin';
|
|
3
|
+
import { webVitalsSoftNavPlugin } from './web-vitals-soft-nav-plugin';
|
|
2
4
|
export { VERSION } from './version';
|
|
5
|
+
export { webVitalsSoftNavPlugin };
|
|
6
|
+
var isSoftNavSupported = function () {
|
|
7
|
+
var globalScope = getGlobalScope();
|
|
8
|
+
return globalScope && 'PerformanceSoftNavigation' in globalScope;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Creates the web vitals plugin. When `reportSoftNav` is enabled, uses the soft-navigation
|
|
12
|
+
* plugin so each navigation (including SPA route changes) is reported separately.
|
|
13
|
+
*/
|
|
14
|
+
export var webVitalsPlugin = function (options) {
|
|
15
|
+
if (options === void 0) { options = {}; }
|
|
16
|
+
if (options.reportSoftNav === true && isSoftNavSupported()) {
|
|
17
|
+
return webVitalsSoftNavPlugin();
|
|
18
|
+
}
|
|
19
|
+
return createWebVitalsPlugin();
|
|
20
|
+
};
|
|
21
|
+
export { webVitalsPlugin as plugin };
|
|
3
22
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,IAAI,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAAE,eAAe,IAAI,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAC/E,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AAEtE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,sBAAsB,EAAE,CAAC;AAElC,IAAM,kBAAkB,GAAG;IACzB,IAAM,WAAW,GAAG,cAAc,EAAE,CAAC;IACrC,OAAO,WAAW,IAAI,2BAA2B,IAAI,WAAW,CAAC;AACnE,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,IAAM,eAAe,GAAG,UAAC,OAA8B;IAA9B,wBAAA,EAAA,YAA8B;IAC5D,IAAI,OAAO,CAAC,aAAa,KAAK,IAAI,IAAI,kBAAkB,EAAE,EAAE;QAC1D,OAAO,sBAAsB,EAAE,CAAC;KACjC;IACD,OAAO,qBAAqB,EAAE,CAAC;AACjC,CAAC,CAAC;AAEF,OAAO,EAAE,eAAe,IAAI,MAAM,EAAE,CAAC","sourcesContent":["import { WebVitalsOptions, getGlobalScope } from '@amplitude/analytics-core';\nimport { webVitalsPlugin as createWebVitalsPlugin } from './web-vitals-plugin';\nimport { webVitalsSoftNavPlugin } from './web-vitals-soft-nav-plugin';\n\nexport { VERSION } from './version';\nexport { webVitalsSoftNavPlugin };\n\nconst isSoftNavSupported = () => {\n const globalScope = getGlobalScope();\n return globalScope && 'PerformanceSoftNavigation' in globalScope;\n};\n\n/**\n * Creates the web vitals plugin. When `reportSoftNav` is enabled, uses the soft-navigation\n * plugin so each navigation (including SPA route changes) is reported separately.\n */\nexport const webVitalsPlugin = (options: WebVitalsOptions = {}) => {\n if (options.reportSoftNav === true && isSoftNavSupported()) {\n return webVitalsSoftNavPlugin();\n }\n return createWebVitalsPlugin();\n};\n\nexport { webVitalsPlugin as plugin };\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAQpC,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,UAAQ,GAAG,sBAAsB,CAkBvF"}
|
package/lib/esm/utils.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
function getMetricStartTime(metric) {
|
|
2
|
+
var _a;
|
|
3
|
+
/* istanbul ignore next */
|
|
4
|
+
var startTime = ((_a = metric.entries[0]) === null || _a === void 0 ? void 0 : _a.startTime) || 0;
|
|
5
|
+
return performance.timeOrigin + startTime;
|
|
6
|
+
}
|
|
7
|
+
export function processMetric(metric, isSoftNav) {
|
|
8
|
+
if (isSoftNav === void 0) { isSoftNav = false; }
|
|
9
|
+
var payload = {
|
|
10
|
+
value: metric.value,
|
|
11
|
+
rating: metric.rating,
|
|
12
|
+
delta: metric.delta,
|
|
13
|
+
navigationType: metric.navigationType,
|
|
14
|
+
id: metric.id,
|
|
15
|
+
timestamp: Math.floor(getMetricStartTime(metric)),
|
|
16
|
+
navigationStart: !isSoftNav
|
|
17
|
+
? Math.floor(performance.timeOrigin)
|
|
18
|
+
: Math.floor(performance.timeOrigin + /* istanbul ignore next */ (metric.navigationStartTime || 0)),
|
|
19
|
+
};
|
|
20
|
+
if (isSoftNav) {
|
|
21
|
+
payload.navigationId = metric.navigationId;
|
|
22
|
+
}
|
|
23
|
+
return payload;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAGA,SAAS,kBAAkB,CAAC,MAAc;;IACxC,0BAA0B;IAC1B,IAAM,SAAS,GAAG,CAAA,MAAA,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,0CAAE,SAAS,KAAI,CAAC,CAAC;IACpD,OAAO,WAAW,CAAC,UAAU,GAAG,SAAS,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,SAAiB;IAAjB,0BAAA,EAAA,iBAAiB;IAC7D,IAAM,OAAO,GAA2B;QACtC,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,cAAc,EAAE,MAAM,CAAC,cAAc;QACrC,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACjD,eAAe,EAAE,CAAC,SAAS;YACzB,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,UAAU,CAAC;YACpC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,UAAU,GAAG,0BAA0B,CAAC,CAAC,MAAM,CAAC,mBAAmB,IAAI,CAAC,CAAC,CAAC;KACtG,CAAC;IAEF,IAAI,SAAS,EAAE;QACb,OAAO,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;KAC5C;IAED,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { WebVitalsMetricPayload } from './constants';\nimport { Metric } from 'web-vitals';\n\nfunction getMetricStartTime(metric: Metric) {\n /* istanbul ignore next */\n const startTime = metric.entries[0]?.startTime || 0;\n return performance.timeOrigin + startTime;\n}\n\nexport function processMetric(metric: Metric, isSoftNav = false): WebVitalsMetricPayload {\n const payload: WebVitalsMetricPayload = {\n value: metric.value,\n rating: metric.rating,\n delta: metric.delta,\n navigationType: metric.navigationType,\n id: metric.id,\n timestamp: Math.floor(getMetricStartTime(metric)),\n navigationStart: !isSoftNav\n ? Math.floor(performance.timeOrigin)\n : Math.floor(performance.timeOrigin + /* istanbul ignore next */ (metric.navigationStartTime || 0)),\n };\n\n if (isSoftNav) {\n payload.navigationId = metric.navigationId;\n }\n\n return payload;\n}\n"]}
|
package/lib/esm/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "1.1
|
|
1
|
+
export declare const VERSION = "1.2.1";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/lib/esm/version.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,UAAU,CAAC"}
|
package/lib/esm/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export var VERSION = '1.1
|
|
1
|
+
export var VERSION = '1.2.1';
|
|
2
2
|
//# sourceMappingURL=version.js.map
|
package/lib/esm/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,IAAM,OAAO,GAAG,
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,IAAM,OAAO,GAAG,OAAO,CAAC","sourcesContent":["export const VERSION = '1.2.1';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web-vitals-plugin.d.ts","sourceRoot":"","sources":["../../src/web-vitals-plugin.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,EACb,aAAa,EACb,gBAAgB,EAGjB,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"web-vitals-plugin.d.ts","sourceRoot":"","sources":["../../src/web-vitals-plugin.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,EACb,aAAa,EACb,gBAAgB,EAGjB,MAAM,2BAA2B,CAAC;AAKnC,MAAM,MAAM,uBAAuB,GAAG,gBAAgB,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;AAErF,eAAO,MAAM,eAAe,QAAO,uBAkElC,CAAC"}
|
|
@@ -3,23 +3,7 @@ import { __awaiter, __generator } from "tslib";
|
|
|
3
3
|
import { getGlobalScope, getDecodeURI, } from '@amplitude/analytics-core';
|
|
4
4
|
import { PLUGIN_NAME, WEB_VITALS_EVENT_NAME } from './constants';
|
|
5
5
|
import { onLCP, onINP, onCLS, onFCP, onTTFB } from 'web-vitals';
|
|
6
|
-
|
|
7
|
-
var _a;
|
|
8
|
-
/* istanbul ignore next */
|
|
9
|
-
var startTime = ((_a = metric.entries[0]) === null || _a === void 0 ? void 0 : _a.startTime) || 0;
|
|
10
|
-
return performance.timeOrigin + startTime;
|
|
11
|
-
}
|
|
12
|
-
function processMetric(metric) {
|
|
13
|
-
return {
|
|
14
|
-
value: metric.value,
|
|
15
|
-
rating: metric.rating,
|
|
16
|
-
delta: metric.delta,
|
|
17
|
-
navigationType: metric.navigationType,
|
|
18
|
-
id: metric.id,
|
|
19
|
-
timestamp: Math.floor(getMetricStartTime(metric)),
|
|
20
|
-
navigationStart: Math.floor(performance.timeOrigin),
|
|
21
|
-
};
|
|
22
|
-
}
|
|
6
|
+
import { processMetric } from './utils';
|
|
23
7
|
export var webVitalsPlugin = function () {
|
|
24
8
|
var visibilityListener = null;
|
|
25
9
|
var globalScope = getGlobalScope();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web-vitals-plugin.js","sourceRoot":"","sources":["../../src/web-vitals-plugin.ts"],"names":[],"mappings":";AAAA,0CAA0C;AAC1C,OAAO,EAIL,cAAc,EACd,YAAY,GACb,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,qBAAqB,
|
|
1
|
+
{"version":3,"file":"web-vitals-plugin.js","sourceRoot":"","sources":["../../src/web-vitals-plugin.ts"],"names":[],"mappings":";AAAA,0CAA0C;AAC1C,OAAO,EAIL,cAAc,EACd,YAAY,GACb,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,qBAAqB,EAAyB,MAAM,aAAa,CAAC;AACxF,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIxC,MAAM,CAAC,IAAM,eAAe,GAAG;IAC7B,IAAI,kBAAkB,GAAiD,IAAI,CAAC;IAC5E,IAAM,WAAW,GAAG,cAAc,EAAE,CAAC;IACrC,IAAM,GAAG,GAAG,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,QAAQ,CAAC;IAClC,IAAM,QAAQ,GAAG,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,QAAQ,CAAC;IACvC,IAAM,KAAK,GAAqC,UAAO,MAAM,EAAE,SAAS;;;YACtE,IAAI,GAAG,KAAK,SAAS,EAAE;gBACrB,sBAAO;aACR;YACK,YAAY,GAAG,YAAY,CAAC,0BAA0B,CAAC,CAAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,IAAI,KAAI,EAAE,EAAE,MAA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,GAAG,CAAC,eAAe,KAAK,QAAQ,IAAI,kBAAkB,EAAE;oBAC1D,SAAS,CAAC,KAAK,CAAC,qBAAqB,EAAE,gBAAgB,CAAC,CAAC;oBACzD,GAAG,CAAC,mBAAmB,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC;oBAChE,kBAAkB,GAAG,IAAI,CAAC;iBAC3B;YACH,CAAC,CAAC;YACF,GAAG,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC;;;SAC9D,CAAC;IAEF,IAAM,OAAO,GAAuC,UAAO,KAAK;;YAC9D,sBAAO,KAAK,EAAC;;SACd,CAAC;IAEF,IAAM,QAAQ,GAAG;;YACf,IAAI,kBAAkB,EAAE;gBACtB,0BAA0B;gBAC1B,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,mBAAmB,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC;aAClE;;;SACF,CAAC;IAEF,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,IAAI,EAAE,YAAY;QAClB,KAAK,OAAA;QACL,OAAO,SAAA;QACP,QAAQ,UAAA;KACT,CAAC;AACJ,CAAC,CAAC","sourcesContent":["/* eslint-disable no-restricted-globals */\nimport {\n BrowserClient,\n BrowserConfig,\n EnrichmentPlugin,\n getGlobalScope,\n getDecodeURI,\n} from '@amplitude/analytics-core';\nimport { PLUGIN_NAME, WEB_VITALS_EVENT_NAME, WebVitalsEventPayload } from './constants';\nimport { onLCP, onINP, onCLS, onFCP, onTTFB } from 'web-vitals';\nimport { processMetric } from './utils';\n\nexport type BrowserEnrichmentPlugin = EnrichmentPlugin<BrowserClient, BrowserConfig>;\n\nexport const webVitalsPlugin = (): BrowserEnrichmentPlugin => {\n let visibilityListener: ((this: Document, ev: Event) => void) | null = null;\n const globalScope = getGlobalScope();\n const doc = globalScope?.document;\n const location = globalScope?.location;\n const setup: BrowserEnrichmentPlugin['setup'] = async (config, amplitude) => {\n if (doc === undefined) {\n return;\n }\n const locationHref = getDecodeURI(/* istanbul ignore next */ location?.href || '', config.loggerProvider);\n const webVitalsPayload: WebVitalsEventPayload = {\n '[Amplitude] Page Domain': /* istanbul ignore next */ location?.hostname || '',\n '[Amplitude] Page Location': locationHref,\n '[Amplitude] Page Path': getDecodeURI(/* istanbul ignore next */ location?.pathname || '', config.loggerProvider),\n '[Amplitude] Page Title': /* istanbul ignore next */ (typeof document !== 'undefined' && document.title) || '',\n '[Amplitude] Page URL': getDecodeURI(locationHref.split('?')[0], config.loggerProvider),\n };\n\n onLCP((metric) => {\n webVitalsPayload['[Amplitude] LCP'] = processMetric(metric);\n });\n\n onFCP((metric) => {\n webVitalsPayload['[Amplitude] FCP'] = processMetric(metric);\n });\n\n onINP((metric) => {\n webVitalsPayload['[Amplitude] INP'] = processMetric(metric);\n });\n\n onCLS((metric) => {\n webVitalsPayload['[Amplitude] CLS'] = processMetric(metric);\n });\n\n onTTFB((metric) => {\n webVitalsPayload['[Amplitude] TTFB'] = processMetric(metric);\n });\n\n visibilityListener = () => {\n if (doc.visibilityState === 'hidden' && visibilityListener) {\n amplitude.track(WEB_VITALS_EVENT_NAME, webVitalsPayload);\n doc.removeEventListener('visibilitychange', visibilityListener);\n visibilityListener = null;\n }\n };\n doc.addEventListener('visibilitychange', visibilityListener);\n };\n\n const execute: BrowserEnrichmentPlugin['execute'] = async (event) => {\n return event;\n };\n\n const teardown = async () => {\n if (visibilityListener) {\n /* istanbul ignore next */\n doc?.removeEventListener('visibilitychange', visibilityListener);\n }\n };\n\n return {\n name: PLUGIN_NAME,\n type: 'enrichment',\n setup,\n execute,\n teardown,\n };\n};\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { BrowserClient, BrowserConfig, EnrichmentPlugin } from '@amplitude/analytics-core';
|
|
2
|
+
export type BrowserEnrichmentPlugin = EnrichmentPlugin<BrowserClient, BrowserConfig>;
|
|
3
|
+
export declare const webVitalsSoftNavPlugin: () => BrowserEnrichmentPlugin;
|
|
4
|
+
//# sourceMappingURL=web-vitals-soft-nav-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-vitals-soft-nav-plugin.d.ts","sourceRoot":"","sources":["../../src/web-vitals-soft-nav-plugin.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,aAAa,EACb,aAAa,EACb,gBAAgB,EAIjB,MAAM,2BAA2B,CAAC;AAWnC,MAAM,MAAM,uBAAuB,GAAG,gBAAgB,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;AAmCrF,eAAO,MAAM,sBAAsB,QAAO,uBA8HzC,CAAC"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { __awaiter, __generator, __values } from "tslib";
|
|
2
|
+
/* eslint-disable no-restricted-globals */
|
|
3
|
+
import { onLCP, onINP, onCLS, onFCP, onTTFB } from 'web-vitals';
|
|
4
|
+
import { getGlobalScope, getDecodeURI, } from '@amplitude/analytics-core';
|
|
5
|
+
import { PLUGIN_NAME, SOFT_NAV_FLUSH_DELAY_MS, WEB_VITALS_EVENT_NAME, WebVitalsMetricProperty, } from './constants';
|
|
6
|
+
import { processMetric } from './utils';
|
|
7
|
+
var METRIC_PROPERTIES = Object.values(WebVitalsMetricProperty);
|
|
8
|
+
/**
|
|
9
|
+
* Builds the page properties for the URL the metrics belong to. That is the URL of the navigation
|
|
10
|
+
* being reported on, which is not necessarily the current URL: a navigation's metrics can be
|
|
11
|
+
* reported after the next navigation has already started.
|
|
12
|
+
*/
|
|
13
|
+
function getPageProperties(url, title, loggerProvider) {
|
|
14
|
+
var hostname = '';
|
|
15
|
+
var pathname = '';
|
|
16
|
+
try {
|
|
17
|
+
var parsedUrl = new URL(url);
|
|
18
|
+
hostname = parsedUrl.hostname;
|
|
19
|
+
pathname = parsedUrl.pathname;
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
loggerProvider.debug('Web vitals plugin is unable to parse page URL: ', e);
|
|
23
|
+
}
|
|
24
|
+
var locationHref = getDecodeURI(url, loggerProvider);
|
|
25
|
+
return {
|
|
26
|
+
'[Amplitude] Page Domain': hostname,
|
|
27
|
+
'[Amplitude] Page Location': locationHref,
|
|
28
|
+
'[Amplitude] Page Path': getDecodeURI(pathname, loggerProvider),
|
|
29
|
+
'[Amplitude] Page Title': title,
|
|
30
|
+
'[Amplitude] Page URL': getDecodeURI(locationHref.split('?')[0], loggerProvider),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function hasMetrics(payload) {
|
|
34
|
+
return METRIC_PROPERTIES.some(function (property) { return property in payload; });
|
|
35
|
+
}
|
|
36
|
+
export var webVitalsSoftNavPlugin = function () {
|
|
37
|
+
var visibilityListener = null;
|
|
38
|
+
var flushTimeout;
|
|
39
|
+
var globalScope = getGlobalScope();
|
|
40
|
+
var doc = globalScope === null || globalScope === void 0 ? void 0 : globalScope.document;
|
|
41
|
+
var location = globalScope === null || globalScope === void 0 ? void 0 : globalScope.location;
|
|
42
|
+
var isActive = false;
|
|
43
|
+
var recordedNavigationIds = new Set();
|
|
44
|
+
var setup = function (config, amplitude) { return __awaiter(void 0, void 0, void 0, function () {
|
|
45
|
+
var payloads, latestNavigationId, getPayload, flush, flushAll, scheduleFlushOfPreviousNavigations, recordMetric, reportOpts;
|
|
46
|
+
return __generator(this, function (_a) {
|
|
47
|
+
if (doc === undefined) {
|
|
48
|
+
return [2 /*return*/];
|
|
49
|
+
}
|
|
50
|
+
isActive = true;
|
|
51
|
+
payloads = new Map();
|
|
52
|
+
latestNavigationId = -1;
|
|
53
|
+
getPayload = function (metric) {
|
|
54
|
+
var key = metric.navigationId;
|
|
55
|
+
var payload = payloads.get(key);
|
|
56
|
+
if (!payload) {
|
|
57
|
+
payload = getPageProperties(
|
|
58
|
+
/* istanbul ignore next */ metric.navigationURL || (location === null || location === void 0 ? void 0 : location.href) || '',
|
|
59
|
+
/* istanbul ignore next */ doc.title || '', config.loggerProvider);
|
|
60
|
+
payloads.set(key, payload);
|
|
61
|
+
}
|
|
62
|
+
return payload;
|
|
63
|
+
};
|
|
64
|
+
flush = function (key) {
|
|
65
|
+
recordedNavigationIds.add(key);
|
|
66
|
+
var payload = payloads.get(key);
|
|
67
|
+
/* istanbul ignore if */
|
|
68
|
+
if (!payload) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
payloads.delete(key);
|
|
72
|
+
// The page can be hidden repeatedly without a new navigation reporting anything.
|
|
73
|
+
if (hasMetrics(payload)) {
|
|
74
|
+
amplitude.track(WEB_VITALS_EVENT_NAME, payload);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
flushAll = function () {
|
|
78
|
+
var e_1, _a;
|
|
79
|
+
try {
|
|
80
|
+
for (var _b = __values(Array.from(payloads.keys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
81
|
+
var key = _c.value;
|
|
82
|
+
flush(key);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
86
|
+
finally {
|
|
87
|
+
try {
|
|
88
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
89
|
+
}
|
|
90
|
+
finally { if (e_1) throw e_1.error; }
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
scheduleFlushOfPreviousNavigations = function (currentNavigationId) {
|
|
94
|
+
if (flushTimeout !== undefined) {
|
|
95
|
+
clearTimeout(flushTimeout);
|
|
96
|
+
}
|
|
97
|
+
flushTimeout = setTimeout(function () {
|
|
98
|
+
var e_2, _a;
|
|
99
|
+
flushTimeout = undefined;
|
|
100
|
+
try {
|
|
101
|
+
for (var _b = __values(Array.from(payloads.keys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
102
|
+
var key = _c.value;
|
|
103
|
+
if (key < currentNavigationId) {
|
|
104
|
+
flush(key);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
109
|
+
finally {
|
|
110
|
+
try {
|
|
111
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
112
|
+
}
|
|
113
|
+
finally { if (e_2) throw e_2.error; }
|
|
114
|
+
}
|
|
115
|
+
}, SOFT_NAV_FLUSH_DELAY_MS);
|
|
116
|
+
};
|
|
117
|
+
recordMetric = function (property) { return function (metric) {
|
|
118
|
+
if (!isActive || recordedNavigationIds.has(metric.navigationId)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
getPayload(metric)[property] = processMetric(metric, true);
|
|
122
|
+
if (metric.navigationId > latestNavigationId) {
|
|
123
|
+
if (latestNavigationId !== -1) {
|
|
124
|
+
scheduleFlushOfPreviousNavigations(metric.navigationId);
|
|
125
|
+
}
|
|
126
|
+
latestNavigationId = metric.navigationId;
|
|
127
|
+
}
|
|
128
|
+
}; };
|
|
129
|
+
reportOpts = { reportSoftNavs: true };
|
|
130
|
+
onLCP(recordMetric(WebVitalsMetricProperty.LCP), reportOpts);
|
|
131
|
+
onFCP(recordMetric(WebVitalsMetricProperty.FCP), reportOpts);
|
|
132
|
+
onINP(recordMetric(WebVitalsMetricProperty.INP), reportOpts);
|
|
133
|
+
onCLS(recordMetric(WebVitalsMetricProperty.CLS), reportOpts);
|
|
134
|
+
onTTFB(recordMetric(WebVitalsMetricProperty.TTFB), reportOpts);
|
|
135
|
+
visibilityListener = function () {
|
|
136
|
+
if (doc.visibilityState === 'hidden' && visibilityListener) {
|
|
137
|
+
flushAll();
|
|
138
|
+
// Keep listening: the page can become visible again and report metrics for further navigations.
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
doc.addEventListener('visibilitychange', visibilityListener);
|
|
142
|
+
return [2 /*return*/];
|
|
143
|
+
});
|
|
144
|
+
}); };
|
|
145
|
+
var execute = function (event) { return __awaiter(void 0, void 0, void 0, function () {
|
|
146
|
+
return __generator(this, function (_a) {
|
|
147
|
+
return [2 /*return*/, event];
|
|
148
|
+
});
|
|
149
|
+
}); };
|
|
150
|
+
var teardown = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
151
|
+
return __generator(this, function (_a) {
|
|
152
|
+
isActive = false;
|
|
153
|
+
if (flushTimeout !== undefined) {
|
|
154
|
+
clearTimeout(flushTimeout);
|
|
155
|
+
flushTimeout = undefined;
|
|
156
|
+
}
|
|
157
|
+
if (visibilityListener) {
|
|
158
|
+
/* istanbul ignore next */
|
|
159
|
+
doc === null || doc === void 0 ? void 0 : doc.removeEventListener('visibilitychange', visibilityListener);
|
|
160
|
+
visibilityListener = null;
|
|
161
|
+
}
|
|
162
|
+
return [2 /*return*/];
|
|
163
|
+
});
|
|
164
|
+
}); };
|
|
165
|
+
return {
|
|
166
|
+
name: PLUGIN_NAME,
|
|
167
|
+
type: 'enrichment',
|
|
168
|
+
setup: setup,
|
|
169
|
+
execute: execute,
|
|
170
|
+
teardown: teardown,
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
//# sourceMappingURL=web-vitals-soft-nav-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-vitals-soft-nav-plugin.js","sourceRoot":"","sources":["../../src/web-vitals-soft-nav-plugin.ts"],"names":[],"mappings":";AAAA,0CAA0C;AAC1C,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAU,MAAM,YAAY,CAAC;AACxE,OAAO,EAKL,cAAc,EACd,YAAY,GACb,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,WAAW,EACX,uBAAuB,EACvB,qBAAqB,EAErB,uBAAuB,GAExB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIxC,IAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAAC,CAAC;AAEjE;;;;GAIG;AACH,SAAS,iBAAiB,CAAC,GAAW,EAAE,KAAa,EAAE,cAAuB;IAC5E,IAAI,QAAQ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nimport {\n BrowserClient,\n BrowserConfig,\n EnrichmentPlugin,\n ILogger,\n getGlobalScope,\n getDecodeURI,\n} from '@amplitude/analytics-core';\nimport {\n PLUGIN_NAME,\n SOFT_NAV_FLUSH_DELAY_MS,\n WEB_VITALS_EVENT_NAME,\n WebVitalsEventPayload,\n WebVitalsMetricProperty,\n WebVitalsMetricPropertyType,\n} from './constants';\nimport { processMetric } from './utils';\n\nexport type BrowserEnrichmentPlugin = EnrichmentPlugin<BrowserClient, BrowserConfig>;\n\nconst METRIC_PROPERTIES = Object.values(WebVitalsMetricProperty);\n\n/**\n * Builds the page properties for the URL the metrics belong to. That is the URL of the navigation\n * being reported on, which is not necessarily the current URL: a navigation's metrics can be\n * reported after the next navigation has already started.\n */\nfunction getPageProperties(url: string, title: string, loggerProvider: ILogger): WebVitalsEventPayload {\n let hostname = '';\n let pathname = '';\n try {\n const parsedUrl = new URL(url);\n hostname = parsedUrl.hostname;\n pathname = parsedUrl.pathname;\n } catch (e) {\n loggerProvider.debug('Web vitals plugin is unable to parse page URL: ', e);\n }\n\n const locationHref = getDecodeURI(url, loggerProvider);\n\n return {\n '[Amplitude] Page Domain': hostname,\n '[Amplitude] Page Location': locationHref,\n '[Amplitude] Page Path': getDecodeURI(pathname, loggerProvider),\n '[Amplitude] Page Title': title,\n '[Amplitude] Page URL': getDecodeURI(locationHref.split('?')[0], loggerProvider),\n };\n}\n\nfunction hasMetrics(payload: WebVitalsEventPayload): boolean {\n return METRIC_PROPERTIES.some((property) => property in payload);\n}\n\nexport const webVitalsSoftNavPlugin = (): BrowserEnrichmentPlugin => {\n let visibilityListener: ((this: Document, ev: Event) => void) | null = null;\n let flushTimeout: ReturnType<typeof setTimeout> | undefined;\n const globalScope = getGlobalScope();\n const doc = globalScope?.document;\n const location = globalScope?.location;\n let isActive = false;\n const recordedNavigationIds = new Set<number>();\n\n const setup: BrowserEnrichmentPlugin['setup'] = async (config, amplitude) => {\n if (doc === undefined) {\n return;\n }\n isActive = true;\n\n // One payload per navigation, keyed by the navigation its metrics belong to.\n const payloads = new Map<number, WebVitalsEventPayload>();\n let latestNavigationId = -1;\n\n const getPayload = (metric: Metric): WebVitalsEventPayload => {\n const key = metric.navigationId;\n let payload = payloads.get(key);\n if (!payload) {\n payload = getPageProperties(\n /* istanbul ignore next */ metric.navigationURL || location?.href || '',\n /* istanbul ignore next */ doc.title || '',\n config.loggerProvider,\n );\n payloads.set(key, payload);\n }\n return payload;\n };\n\n const flush = (key: number) => {\n recordedNavigationIds.add(key);\n\n const payload = payloads.get(key);\n /* istanbul ignore if */\n if (!payload) {\n return;\n }\n payloads.delete(key);\n\n // The page can be hidden repeatedly without a new navigation reporting anything.\n if (hasMetrics(payload)) {\n amplitude.track(WEB_VITALS_EVENT_NAME, payload);\n }\n };\n\n const flushAll = () => {\n for (const key of Array.from(payloads.keys())) {\n flush(key);\n }\n };\n\n // Once a newer navigation starts reporting metrics, the navigations before it are final. Sending\n // them is deferred briefly so metrics reported late still make it into their event.\n const scheduleFlushOfPreviousNavigations = (currentNavigationId: number) => {\n if (flushTimeout !== undefined) {\n clearTimeout(flushTimeout);\n }\n flushTimeout = setTimeout(() => {\n flushTimeout = undefined;\n for (const key of Array.from(payloads.keys())) {\n if (key < currentNavigationId) {\n flush(key);\n }\n }\n }, SOFT_NAV_FLUSH_DELAY_MS);\n };\n\n const recordMetric = (property: WebVitalsMetricPropertyType) => (metric: Metric) => {\n if (!isActive || recordedNavigationIds.has(metric.navigationId)) {\n return;\n }\n getPayload(metric)[property] = processMetric(metric, true);\n\n if (metric.navigationId > latestNavigationId) {\n if (latestNavigationId !== -1) {\n scheduleFlushOfPreviousNavigations(metric.navigationId);\n }\n latestNavigationId = metric.navigationId;\n }\n };\n\n const reportOpts = { reportSoftNavs: true };\n\n onLCP(recordMetric(WebVitalsMetricProperty.LCP), reportOpts);\n onFCP(recordMetric(WebVitalsMetricProperty.FCP), reportOpts);\n onINP(recordMetric(WebVitalsMetricProperty.INP), reportOpts);\n onCLS(recordMetric(WebVitalsMetricProperty.CLS), reportOpts);\n onTTFB(recordMetric(WebVitalsMetricProperty.TTFB), reportOpts);\n\n visibilityListener = () => {\n if (doc.visibilityState === 'hidden' && visibilityListener) {\n flushAll();\n // Keep listening: the page can become visible again and report metrics for further navigations.\n }\n };\n doc.addEventListener('visibilitychange', visibilityListener);\n };\n\n const execute: BrowserEnrichmentPlugin['execute'] = async (event) => {\n return event;\n };\n\n const teardown = async () => {\n isActive = false;\n if (flushTimeout !== undefined) {\n clearTimeout(flushTimeout);\n flushTimeout = undefined;\n }\n if (visibilityListener) {\n /* istanbul ignore next */\n doc?.removeEventListener('visibilitychange', visibilityListener);\n visibilityListener = null;\n }\n };\n\n return {\n name: PLUGIN_NAME,\n type: 'enrichment',\n setup,\n execute,\n teardown,\n };\n};\n"]}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var amplitudeWebVitalsPlugin=function(e){"use strict";function t(e,t,n,i){return new(n||(n=Promise))(function(r,o){function a(e){try{l(i.next(e))}catch(e){o(e)}}function s(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(a,s)}l((i=i.apply(e,t||[])).next())})}function n(e,t){var n,i,r,o={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=s(0),a.throw=s(1),a.return=s(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(s){return function(l){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,s[0]&&(o=0)),o;)try{if(n=1,i&&(r=2&s[0]?i.return:s[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,s[1])).done)return r;switch(i=0,r&&(s=[2&s[0],r.value]),s[0]){case 0:case 1:r=s;break;case 4:return o.label++,{value:s[1],done:!1};case 5:o.label++,i=s[1],s=[0];continue;case 7:s=o.ops.pop(),o.trys.pop();continue;default:if(!(r=o.trys,(r=r.length>0&&r[r.length-1])||6!==s[0]&&2!==s[0])){o=0;continue}if(3===s[0]&&(!r||s[1]>r[0]&&s[1]<r[3])){o.label=s[1];break}if(6===s[0]&&o.label<r[1]){o.label=r[1],r=s;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(s);break}r[2]&&o.ops.pop(),o.trys.pop();continue}s=t.call(e,o)}catch(e){s=[6,e],i=0}finally{n=r=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,l])}}}"function"==typeof SuppressedError&&SuppressedError;var i=function(e,t){var n=e;try{n=decodeURI(e)}catch(e){null==t||t.error("Malformed URI sequence: ",e)}return n};let r=-1;const o=e=>{addEventListener("pageshow",t=>{t.persisted&&(r=t.timeStamp,e(t))},!0)},a=(e,t,n,i)=>{let r,o;return a=>{t.value>=0&&(a||i)&&(o=t.value-(r??0),(o||void 0===r)&&(r=t.value,t.delta=o,t.rating=((e,t)=>e>t[1]?"poor":e>t[0]?"needs-improvement":"good")(t.value,n),e(t)))}},s=e=>{requestAnimationFrame(()=>requestAnimationFrame(()=>e()))},l=()=>{const e=performance.getEntriesByType("navigation")[0];if(e&&e.responseStart>0&&e.responseStart<performance.now())return e},u=()=>{const e=l();return e?.activationStart??0},c=(e,t=-1)=>{const n=l();let i="navigate";return r>=0?i="back-forward-cache":n&&(document.prerendering||u()>0?i="prerender":document.wasDiscarded?i="restore":n.type&&(i=n.type.replace(/_/g,"-"))),{name:e,value:t,rating:"good",delta:0,entries:[],id:`v5-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:i}},d=new WeakMap;function p(e,t){return d.get(e)||d.set(e,new t),d.get(e)}class f{t;i=0;o=[];h(e){if(e.hadRecentInput)return;const t=this.o[0],n=this.o.at(-1);this.i&&t&&n&&e.startTime-n.startTime<1e3&&e.startTime-t.startTime<5e3?(this.i+=e.value,this.o.push(e)):(this.i=e.value,this.o=[e]),this.t?.(e)}}const h=(e,t,n={})=>{try{if(PerformanceObserver.supportedEntryTypes.includes(e)){const i=new PerformanceObserver(e=>{Promise.resolve().then(()=>{t(e.getEntries())})});return i.observe({type:e,buffered:!0,...n}),i}}catch{}},v=e=>{let t=!1;return()=>{t||(e(),t=!0)}};let m=-1;const g=new Set,y=()=>"hidden"!==document.visibilityState||document.prerendering?1/0:0,b=e=>{if("hidden"===document.visibilityState){if("visibilitychange"===e.type)for(const e of g)e();isFinite(m)||(m="visibilitychange"===e.type?e.timeStamp:0,removeEventListener("prerenderingchange",b,!0))}},T=()=>{if(m<0){const e=u(),t=document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").filter(t=>"hidden"===t.name&&t.startTime>e)[0]?.startTime;m=t??y(),addEventListener("visibilitychange",b,!0),addEventListener("prerenderingchange",b,!0),o(()=>{setTimeout(()=>{m=y()})})}return{get firstHiddenTime(){return m},onHidden(e){g.add(e)}}},P=e=>{document.prerendering?addEventListener("prerenderingchange",()=>e(),!0):e()},w=[1800,3e3],A=(e,t={})=>{P(()=>{const n=T();let i,r=c("FCP");const l=h("paint",e=>{for(const t of e)"first-contentful-paint"===t.name&&(l.disconnect(),t.startTime<n.firstHiddenTime&&(r.value=Math.max(t.startTime-u(),0),r.entries.push(t),i(!0)))});l&&(i=a(e,r,w,t.reportAllChanges),o(n=>{r=c("FCP"),i=a(e,r,w,t.reportAllChanges),s(()=>{r.value=performance.now()-n.timeStamp,i(!0)})}))})},C=[.1,.25];let E=0,L=1/0,S=0;const I=e=>{for(const t of e)t.interactionId&&(L=Math.min(L,t.interactionId),S=Math.max(S,t.interactionId),E=S?(S-L)/7+1:0)};let M;const k=()=>M?E:performance.interactionCount??0;let x=0;class F{u=[];l=new Map;m;p;v(){x=k(),this.u.length=0,this.l.clear()}L(){const e=Math.min(this.u.length-1,Math.floor((k()-x)/50));return this.u[e]}h(e){if(this.m?.(e),!e.interactionId&&"first-input"!==e.entryType)return;const t=this.u.at(-1);let n=this.l.get(e.interactionId);if(n||this.u.length<10||e.duration>t.P){if(n?e.duration>n.P?(n.entries=[e],n.P=e.duration):e.duration===n.P&&e.startTime===n.entries[0].startTime&&n.entries.push(e):(n={id:e.interactionId,entries:[e],P:e.duration},this.l.set(n.id,n),this.u.push(n)),this.u.sort((e,t)=>t.P-e.P),this.u.length>10){const e=this.u.splice(10);for(const t of e)this.l.delete(t.id)}this.p?.(n)}}}const O=e=>{const t=globalThis.requestIdleCallback||setTimeout;"hidden"===document.visibilityState?e():(e=v(e),addEventListener("visibilitychange",e,{once:!0,capture:!0}),t(()=>{e(),removeEventListener("visibilitychange",e,{capture:!0})}))},R=[200,500],H=(e,t={})=>{if(!globalThis.PerformanceEventTiming||!("interactionId"in PerformanceEventTiming.prototype))return;const n=T();P(()=>{"interactionCount"in performance||M||(M=h("event",I,{type:"event",buffered:!0,durationThreshold:0}));let i,r=c("INP");const s=p(t,F),l=e=>{O(()=>{for(const t of e)s.h(t);const t=s.L();t&&t.P!==r.value&&(r.value=t.P,r.entries=t.entries,i())})},u=h("event",l,{durationThreshold:t.durationThreshold??40});i=a(e,r,R,t.reportAllChanges),u&&(u.observe({type:"first-input",buffered:!0}),n.onHidden(()=>{l(u.takeRecords()),i(!0)}),o(()=>{s.v(),r=c("INP"),i=a(e,r,R,t.reportAllChanges)}))})};class B{m;h(e){this.m?.(e)}}const q=[2500,4e3],N=[800,1800],V=e=>{document.prerendering?P(()=>V(e)):"complete"!==document.readyState?addEventListener("load",()=>V(e),!0):setTimeout(e)};function j(e){var t,n=(null===(t=e.entries[0])||void 0===t?void 0:t.startTime)||0;return performance.timeOrigin+n}function D(e){return{value:e.value,rating:e.rating,delta:e.delta,navigationType:e.navigationType,id:e.id,timestamp:Math.floor(j(e)),navigationStart:Math.floor(performance.timeOrigin)}}var U=function(){var e,r=null,d=(e="ampIntegrationContext","undefined"!=typeof globalThis&&void 0!==globalThis[e]?globalThis[e]:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:void 0),m=null==d?void 0:d.document,g=null==d?void 0:d.location;return{name:"web-vitals-browser",type:"enrichment",setup:function(e,d){return t(void 0,void 0,void 0,function(){var t,y;return n(this,function(n){return void 0===m||(t=i((null==g?void 0:g.href)||"",e.loggerProvider),y={"[Amplitude] Page Domain":(null==g?void 0:g.hostname)||"","[Amplitude] Page Location":t,"[Amplitude] Page Path":i((null==g?void 0:g.pathname)||"",e.loggerProvider),"[Amplitude] Page Title":"undefined"!=typeof document&&document.title||"","[Amplitude] Page URL":i(t.split("?")[0],e.loggerProvider)},((e,t={})=>{P(()=>{const n=T();let i,r=c("LCP");const l=p(t,B),d=e=>{t.reportAllChanges||(e=e.slice(-1));for(const t of e)l.h(t),t.startTime<n.firstHiddenTime&&(r.value=Math.max(t.startTime-u(),0),r.entries=[t],i())},f=h("largest-contentful-paint",d);if(f){i=a(e,r,q,t.reportAllChanges);const n=v(()=>{d(f.takeRecords()),f.disconnect(),i(!0)}),l=e=>{e.isTrusted&&(O(n),removeEventListener(e.type,l,{capture:!0}))};for(const e of["keydown","click","visibilitychange"])addEventListener(e,l,{capture:!0});o(n=>{r=c("LCP"),i=a(e,r,q,t.reportAllChanges),s(()=>{r.value=performance.now()-n.timeStamp,i(!0)})})}})})(function(e){y["[Amplitude] LCP"]=D(e)}),A(function(e){y["[Amplitude] FCP"]=D(e)}),H(function(e){y["[Amplitude] INP"]=D(e)}),((e,t={})=>{const n=T();A(v(()=>{let i,r=c("CLS",0);const l=p(t,f),u=e=>{for(const t of e)l.h(t);l.i>r.value&&(r.value=l.i,r.entries=l.o,i())},d=h("layout-shift",u);d&&(i=a(e,r,C,t.reportAllChanges),n.onHidden(()=>{u(d.takeRecords()),i(!0)}),o(()=>{l.i=0,r=c("CLS",0),i=a(e,r,C,t.reportAllChanges),s(()=>i())}),setTimeout(i))}))})(function(e){y["[Amplitude] CLS"]=D(e)}),((e,t={})=>{let n=c("TTFB"),i=a(e,n,N,t.reportAllChanges);V(()=>{const r=l();r&&(n.value=Math.max(r.responseStart-u(),0),n.entries=[r],i(!0),o(()=>{n=c("TTFB",0),i=a(e,n,N,t.reportAllChanges),i(!0)}))})})(function(e){y["[Amplitude] TTFB"]=D(e)}),r=function(){"hidden"===m.visibilityState&&r&&(d.track("[Amplitude] Web Vitals",y),m.removeEventListener("visibilitychange",r),r=null)},m.addEventListener("visibilitychange",r)),[2]})})},execute:function(e){return t(void 0,void 0,void 0,function(){return n(this,function(t){return[2,e]})})},teardown:function(){return t(void 0,void 0,void 0,function(){return n(this,function(e){return r&&(null==m||m.removeEventListener("visibilitychange",r)),[2]})})}}};return e.VERSION="1.1.49-video-analytics-5.0",e.plugin=U,e.webVitalsPlugin=U,Object.defineProperty(e,"__esModule",{value:!0}),e}({});
|
|
1
|
+
var amplitudeWebVitalsPlugin=function(t){"use strict";function e(t,e,n,i){return new(n||(n=Promise))(function(o,r){function a(t){try{l(i.next(t))}catch(t){r(t)}}function s(t){try{l(i.throw(t))}catch(t){r(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(a,s)}l((i=i.apply(t,e||[])).next())})}function n(t,e){var n,i,o,r={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=s(0),a.throw=s(1),a.return=s(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(s){return function(l){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,s[0]&&(r=0)),r;)try{if(n=1,i&&(o=2&s[0]?i.return:s[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,s[1])).done)return o;switch(i=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return r.label++,{value:s[1],done:!1};case 5:r.label++,i=s[1],s=[0];continue;case 7:s=r.ops.pop(),r.trys.pop();continue;default:if(!(o=r.trys,(o=o.length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){r=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){r.label=s[1];break}if(6===s[0]&&r.label<o[1]){r.label=o[1],o=s;break}if(o&&r.label<o[2]){r.label=o[2],r.ops.push(s);break}o[2]&&r.ops.pop(),r.trys.pop();continue}s=e.call(t,r)}catch(t){s=[6,t],i=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,l])}}}function i(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],i=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&i>=t.length&&(t=void 0),{value:t&&t[i++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}"function"==typeof SuppressedError&&SuppressedError;var o=function(){var t="ampIntegrationContext";return"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},r=function(t,e){var n=t;try{n=decodeURI(t)}catch(t){null==e||e.error("Malformed URI sequence: ",t)}return n},a="web-vitals-browser",s="[Amplitude] Web Vitals",l={LCP:"[Amplitude] LCP",FCP:"[Amplitude] FCP",INP:"[Amplitude] INP",CLS:"[Amplitude] CLS",TTFB:"[Amplitude] TTFB"};let c=-1;const u=()=>c,d=t=>{addEventListener("pageshow",e=>{e.persisted&&(c=e.timeStamp,t(e))},!0)},v=(t,e,n,i)=>{let o,r;return a=>{e.value>=0&&(a||i)&&(r=e.value-(o??0),(r||void 0===o)&&(o=e.value,e.delta=r,e.rating=((t,e)=>t>e[1]?"poor":t>e[0]?"needs-improvement":"good")(e.value,n),t(e)))}},f=t=>{requestAnimationFrame(()=>requestAnimationFrame(()=>t()))},g=()=>{const t=performance.getEntriesByType("navigation")[0];if(t&&t.responseStart>0&&t.responseStart<performance.now())return t},p=()=>g()?.activationStart??0;let h=-1;const m=new Set,y=()=>"hidden"!==document.visibilityState||document.prerendering?1/0:0,T=t=>{if("hidden"===document.visibilityState){if("visibilitychange"===t.type)for(const t of m)t();isFinite(h)||(h="visibilitychange"===t.type?t.timeStamp:0,removeEventListener("prerenderingchange",T,!0))}},b=(t=!1)=>{if(t&&(h=1/0),h<0){const t=p(),e=document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").find(e=>"hidden"===e.name&&e.startTime>=t)?.startTime;h=e??y(),addEventListener("visibilitychange",T,!0),addEventListener("prerenderingchange",T,!0),d(()=>{setTimeout(()=>{h=y()})})}return{get firstHiddenTime(){return h},onHidden(t){m.add(t)}}},I=(t,e=-1,n,i=0,o,r,a)=>{const s=g(),l=s?.navigationId||0;let c="navigate";return n?c=n:u()>=0?c="back-forward-cache":s&&(document.prerendering||p()>0?c="prerender":document.wasDiscarded?c="restore":s.type&&(c=s.type.replace(/_/g,"-"))),{name:t,value:e,rating:"good",delta:0,entries:[],id:`v6-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:c,navigationId:i||l,navigationInteractionId:o,navigationURL:r||s?.name,navigationStartTime:a||0}},P=new WeakMap;function w(t,e){let n=P.get(e);return n||(n=new WeakMap,P.set(e,n)),n.get(t)||n.set(t,new e),n.get(t)}class C{t;i=0;o=[];h(t){if(t.hadRecentInput)return;const e=this.o[0],n=this.o.at(-1);this.i&&e&&n&&t.startTime-n.startTime<1e3&&t.startTime-e.startTime<5e3?(this.i+=t.value,this.o.push(t)):(this.i=t.value,this.o=[t]),this.t?.(t)}}const L=(t,e,n={})=>{try{const i=t.filter(t=>PerformanceObserver.supportedEntryTypes.includes(t));if(i.length>0){const t=new PerformanceObserver(t=>{queueMicrotask(()=>{const n=t.getEntries();i.length>1&&n.sort((t,e)=>t.startTime+t.duration-(e.startTime+e.duration)),e(n)})});for(const e of i)t.observe({type:e,buffered:!0,...n});return t}}catch{}},A=t=>globalThis.PerformanceObserver?.supportedEntryTypes?.includes("soft-navigation")&&"function"==typeof globalThis.PerformanceSoftNavigation?.prototype?.getLargestInteractionContentfulPaint&&t&&t.reportSoftNavs,S=(t,e)=>{if(t.set(e.navigationId,e),t.size>2){const e=t.keys().next().value;void 0!==e&&t.delete(e)}},E=t=>{let e=!1;return()=>{e||(t(),e=!0)}};class k{l}const M=t=>{document.prerendering?addEventListener("prerenderingchange",t,!0):t()},x=[1800,3e3],F=(t,e={})=>{const n=A(e);M(()=>{const i=w(e,k),o=b();let r,a=I("FCP");const s=L(["paint"],t=>{for(const e of t)"first-contentful-paint"===e.name&&(s.disconnect(),e.startTime<o.firstHiddenTime&&(a.value=Math.max(e.startTime-p(),0),a.entries.push(e),a.navigationId=e.navigationId||a.navigationId,r(!0)))});s&&(r=v(t,a,x,e.reportAllChanges),d(n=>{a=I("FCP",-1,"back-forward-cache",a.navigationId,a.navigationInteractionId,a.navigationURL,u()),r=v(t,a,x,e.reportAllChanges),f(()=>{a.value=performance.now()-n.timeStamp,r(!0)})})),n&&L(["soft-navigation"],n=>{n.forEach(n=>{i.l&&n.navigationId&&S(i.l,n);const o=Math.max((n.presentationTime||n.paintTime||0)-n.startTime,0);a=I("FCP",o,"soft-navigation",n.navigationId,n.interactionId,n.name,n.startTime),r=v(t,a,x,e.reportAllChanges),r(!0)})},e)})},R=[.1,.25],N=(t,e={})=>{const n=b();F(E(()=>{let i,o=I("CLS",0);const r=w(e,C),a=(n,a,s,l,c)=>{o=I("CLS",0,n,a,s,l,c),r.i=0,i=v(t,o,R,e.reportAllChanges)},s=(t=!1)=>{r.i>o.value&&(o.value=r.i,o.entries=r.o),i(t)},l=t=>{s(!0),a("soft-navigation",t.navigationId,t.interactionId,t.name,t.startTime)},c=t=>{for(const e of t)"soft-navigation"!==e.entryType?r.h(e):l(e);s()},g=["layout-shift"];A(e)&&g.push("soft-navigation");const p=L(g,c);p&&(i=v(t,o,R,e.reportAllChanges),n.onHidden(()=>{c(p.takeRecords()),i(!0)}),d(()=>{a("back-forward-cache",o.navigationId,o.navigationInteractionId,o.navigationURL,u()),f(i)}),setTimeout(i))}))};let U=0,O=1/0,B=0;const q=t=>{for(const e of t)e.interactionId&&(O=Math.min(O,e.interactionId),B=Math.max(B,e.interactionId),U=B?(B-O)/7+1:0)};let H;const j=()=>H?U:performance.interactionCount??0;class V{u=0;v=[];m=new Map;p;T;_(){return j()-this.u}M(){this.u=j(),this.v.length=0,this.m.clear()}L(t){const e=this._(),n=Math.min(this.v.length-1,Math.floor(e/50));return!e||-1!==n||"soft-navigation"!==t&&"back-forward-cache"!==t?this.v[n]:{P:8,id:-1,entries:[]}}h(t){if(this.p?.(t),!t.interactionId)return;const e=this.v.at(-1);let n=this.m.get(t.interactionId);if(n||this.v.length<10||t.duration>e.P){if(n?t.duration>n.P?(n.entries=[t],n.P=t.duration):t.duration===n.P&&t.startTime===n.entries[0].startTime&&n.entries.push(t):(n={id:t.interactionId,entries:[t],P:t.duration},this.m.set(n.id,n),this.v.push(n)),this.v.sort((t,e)=>e.P-t.P),this.v.length>10){const t=this.v.splice(10);for(const e of t)this.m.delete(e.id)}this.T?.(n)}}}const W=t=>{const e="requestIdleCallback"in globalThis?1e3:0,n=globalThis.requestIdleCallback||setTimeout,i=globalThis.cancelIdleCallback||clearTimeout;if("hidden"===document.visibilityState)t();else{const o=E(t);let r=-1;const a=()=>{i(r),o()};addEventListener("visibilitychange",a,{once:!0,capture:!0}),r=n(()=>{removeEventListener("visibilitychange",a,{capture:!0}),o()},{timeout:e})}},_=[200,500],D=(t,e={})=>{if(!globalThis.PerformanceEventTiming||!("interactionId"in PerformanceEventTiming.prototype))return;const n=b();M(()=>{"interactionCount"in performance||H||(H=L(["event"],q,{durationThreshold:0}));let i,o=I("INP");const r=w(e,V),a=(n,a,s,l,c)=>{r.M(),o=I("INP",-1,n,a,s,l,c),i=v(t,o,_,e.reportAllChanges)},s=()=>{const t=r.L(o.navigationType);t&&t.P!==o.value&&(o.value=t.P,o.entries=t.entries,i())},l=t=>{s(),i(!0),a("soft-navigation",t.navigationId,t.interactionId,t.name,t.startTime)},c=(t,e=!1)=>{W(()=>{for(const e of t)"soft-navigation"!==e.entryType?r.h(e):l(e);s(),e&&i(!0)})},f=["event","first-input"];A(e)&&f.push("soft-navigation");const g=L(f,c,{...e,durationThreshold:e.durationThreshold??40});i=v(t,o,_,e.reportAllChanges),g&&(n.onHidden(()=>{c(g.takeRecords(),!0)}),d(()=>{a("back-forward-cache",o.navigationId,o.navigationInteractionId,o.navigationURL,u())}))})};class ${p;l;h(t){this.p?.(t)}}const z=[2500,4e3],G=(t,e={})=>{let n=!1;const i=A(e);M(()=>{let o,r=b(),a=I("LCP");const s=w(e,$),l=(i,s,l,c,u)=>{a=I("LCP",-1,i,s,l,c,u),o=v(t,a,z,e.reportAllChanges),n=!1,"soft-navigation"===i&&(r=b(!0))},c=t=>{s.l&&t.navigationId&&S(s.l,t),n||o(!0),l("soft-navigation",t.navigationId,t.interactionId,t.name,t.startTime);const e=t.getLargestInteractionContentfulPaint?.();e&&g([e])},g=t=>{e.reportAllChanges||i||(t=t.slice(-1));for(const e of t){if(!e)continue;if("soft-navigation"===e.entryType){c(e);continue}let t=0,n=[],i=e.startTime;if("largest-contentful-paint"===e.entryType)t=Math.max(e.startTime-p(),0),s.h(e),n=[e];else if("interaction-contentful-paint"===e.entryType){const o=e;if(!a.navigationId)continue;if("interactionId"in o&&o.interactionId!=a.navigationInteractionId)continue;i=o.largestContentfulPaint?.renderTime||0,t=Math.max(i-e.startTime,0),o.largestContentfulPaint&&(s.h(o.largestContentfulPaint),n=[o.largestContentfulPaint])}i<r.firstHiddenTime&&(a.value=t,a.entries=n,o())}},h=["largest-contentful-paint"];i&&h.push("interaction-contentful-paint","soft-navigation");const m=L(h,g);if(m){o=v(t,a,z,e.reportAllChanges);const r=["keydown","click","visibilitychange"],s=t=>{if(t.isTrusted&&!n){const t=a.id;W(()=>{if(!n){if(!i){m.disconnect();for(const t of r)removeEventListener(t,s,{capture:!0})}t===a.id&&(n=!0,o(!0))}})}};for(const t of r)addEventListener(t,s,{capture:!0});d(i=>{l("back-forward-cache",a.navigationId,a.navigationInteractionId,a.navigationURL,u()),o=v(t,a,z,e.reportAllChanges),f(()=>{a.value=performance.now()-i.timeStamp,n=!0,o(!0)})})}})},J=[800,1800],K=t=>{document.prerendering?M(()=>K(t)):"complete"!==document.readyState?addEventListener("load",()=>K(t),!0):setTimeout(t)},Q=(t,e={})=>{const n=A(e);let i=I("TTFB"),o=v(t,i,J,e.reportAllChanges);K(()=>{const r=g();if(r){const a=r.responseStart;i.value=Math.max(a-p(),0),i.entries=[r],o(!0),d(()=>{i=I("TTFB",0,"back-forward-cache",i.navigationId,i.navigationInteractionId,i.navigationURL,u()),o=v(t,i,J,e.reportAllChanges),o(!0)}),n&&L(["soft-navigation"],n=>{n.forEach(n=>{n.navigationId&&(i=I("TTFB",0,"soft-navigation",n.navigationId,n.interactionId,n.name,n.startTime),i.entries=[n],o=v(t,i,J,e.reportAllChanges),o(!0))})},e)}})};function X(t){var e,n=(null===(e=t.entries[0])||void 0===e?void 0:e.startTime)||0;return performance.timeOrigin+n}function Y(t,e){void 0===e&&(e=!1);var n={value:t.value,rating:t.rating,delta:t.delta,navigationType:t.navigationType,id:t.id,timestamp:Math.floor(X(t)),navigationStart:e?Math.floor(performance.timeOrigin+(t.navigationStartTime||0)):Math.floor(performance.timeOrigin)};return e&&(n.navigationId=t.navigationId),n}var Z=Object.values(l);var tt=function(){var t,c=null,u=o(),d=null==u?void 0:u.document,v=null==u?void 0:u.location,f=!1,g=new Set;return{name:a,type:"enrichment",setup:function(o,a){return e(void 0,void 0,void 0,function(){var e,u,p,h,m,y,T,b;return n(this,function(n){return void 0===d||(f=!0,e=new Map,u=-1,p=function(t){var n=t.navigationId,i=e.get(n);return i||(i=function(t,e,n){var i="",o="";try{var a=new URL(t);i=a.hostname,o=a.pathname}catch(t){n.debug("Web vitals plugin is unable to parse page URL: ",t)}var s=r(t,n);return{"[Amplitude] Page Domain":i,"[Amplitude] Page Location":s,"[Amplitude] Page Path":r(o,n),"[Amplitude] Page Title":e,"[Amplitude] Page URL":r(s.split("?")[0],n)}}(t.navigationURL||(null==v?void 0:v.href)||"",d.title||"",o.loggerProvider),e.set(n,i)),i},h=function(t){g.add(t);var n=e.get(t);n&&(e.delete(t),function(t){return Z.some(function(e){return e in t})}(n)&&a.track(s,n))},m=function(){var t,n;try{for(var o=i(Array.from(e.keys())),r=o.next();!r.done;r=o.next()){var a=r.value;h(a)}}catch(e){t={error:e}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(t)throw t.error}}},y=function(n){void 0!==t&&clearTimeout(t),t=setTimeout(function(){var o,r;t=void 0;try{for(var a=i(Array.from(e.keys())),s=a.next();!s.done;s=a.next()){var l=s.value;l<n&&h(l)}}catch(t){o={error:t}}finally{try{s&&!s.done&&(r=a.return)&&r.call(a)}finally{if(o)throw o.error}}},1e3)},b={reportSoftNavs:!0},G((T=function(t){return function(e){f&&!g.has(e.navigationId)&&(p(e)[t]=Y(e,!0),e.navigationId>u&&(-1!==u&&y(e.navigationId),u=e.navigationId))}})(l.LCP),b),F(T(l.FCP),b),D(T(l.INP),b),N(T(l.CLS),b),Q(T(l.TTFB),b),c=function(){"hidden"===d.visibilityState&&c&&m()},d.addEventListener("visibilitychange",c)),[2]})})},execute:function(t){return e(void 0,void 0,void 0,function(){return n(this,function(e){return[2,t]})})},teardown:function(){return e(void 0,void 0,void 0,function(){return n(this,function(e){return f=!1,void 0!==t&&(clearTimeout(t),t=void 0),c&&(null==d||d.removeEventListener("visibilitychange",c),c=null),[2]})})}}},et=function(t){return void 0===t&&(t={}),!0===t.reportSoftNav&&((i=o())&&"PerformanceSoftNavigation"in i)?tt():function(){var t=null,i=o(),l=null==i?void 0:i.document,c=null==i?void 0:i.location;return{name:a,type:"enrichment",setup:function(i,o){return e(void 0,void 0,void 0,function(){var e,a;return n(this,function(n){return void 0===l||(e=r((null==c?void 0:c.href)||"",i.loggerProvider),a={"[Amplitude] Page Domain":(null==c?void 0:c.hostname)||"","[Amplitude] Page Location":e,"[Amplitude] Page Path":r((null==c?void 0:c.pathname)||"",i.loggerProvider),"[Amplitude] Page Title":"undefined"!=typeof document&&document.title||"","[Amplitude] Page URL":r(e.split("?")[0],i.loggerProvider)},G(function(t){a["[Amplitude] LCP"]=Y(t)}),F(function(t){a["[Amplitude] FCP"]=Y(t)}),D(function(t){a["[Amplitude] INP"]=Y(t)}),N(function(t){a["[Amplitude] CLS"]=Y(t)}),Q(function(t){a["[Amplitude] TTFB"]=Y(t)}),t=function(){"hidden"===l.visibilityState&&t&&(o.track(s,a),l.removeEventListener("visibilitychange",t),t=null)},l.addEventListener("visibilitychange",t)),[2]})})},execute:function(t){return e(void 0,void 0,void 0,function(){return n(this,function(e){return[2,t]})})},teardown:function(){return e(void 0,void 0,void 0,function(){return n(this,function(e){return t&&(null==l||l.removeEventListener("visibilitychange",t)),[2]})})}}}();var i};return t.VERSION="1.2.1",t.plugin=et,t.webVitalsPlugin=et,t.webVitalsSoftNavPlugin=tt,Object.defineProperty(t,"__esModule",{value:!0}),t}({});
|
|
2
2
|
//# sourceMappingURL=amplitude-min.js.map
|
|
Binary file
|