123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- this.workbox = this.workbox || {};
- this.workbox.googleAnalytics = (function (exports, BackgroundSyncPlugin_js, cacheNames_js, getFriendlyURL_js, logger_js, Route_js, Router_js, NetworkFirst_js, NetworkOnly_js) {
- 'use strict';
- try {
- self['workbox:google-analytics:5.1.4'] && _();
- } catch (e) {}
-
- const QUEUE_NAME = 'workbox-google-analytics';
- const MAX_RETENTION_TIME = 60 * 48;
- const GOOGLE_ANALYTICS_HOST = 'www.google-analytics.com';
- const GTM_HOST = 'www.googletagmanager.com';
- const ANALYTICS_JS_PATH = '/analytics.js';
- const GTAG_JS_PATH = '/gtag/js';
- const GTM_JS_PATH = '/gtm.js';
-
-
-
- const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
-
-
- const createOnSyncCallback = config => {
- return async ({
- queue
- }) => {
- let entry;
- while (entry = await queue.shiftRequest()) {
- const {
- request,
- timestamp
- } = entry;
- const url = new URL(request.url);
- try {
-
-
- const params = request.method === 'POST' ? new URLSearchParams(await request.clone().text()) : url.searchParams;
-
- const originalHitTime = timestamp - (Number(params.get('qt')) || 0);
- const queueTime = Date.now() - originalHitTime;
- params.set('qt', String(queueTime));
- if (config.parameterOverrides) {
- for (const param of Object.keys(config.parameterOverrides)) {
- const value = config.parameterOverrides[param];
- params.set(param, value);
- }
- }
- if (typeof config.hitFilter === 'function') {
- config.hitFilter.call(null, params);
- }
-
- await fetch(new Request(url.origin + url.pathname, {
- body: params.toString(),
- method: 'POST',
- mode: 'cors',
- credentials: 'omit',
- headers: {
- 'Content-Type': 'text/plain'
- }
- }));
- if ("dev" !== 'production') {
- logger_js.logger.log(`Request for '${getFriendlyURL_js.getFriendlyURL(url.href)}'` + `has been replayed`);
- }
- } catch (err) {
- await queue.unshiftRequest(entry);
- {
- logger_js.logger.log(`Request for '${getFriendlyURL_js.getFriendlyURL(url.href)}'` + `failed to replay, putting it back in the queue.`);
- }
- throw err;
- }
- }
- {
- logger_js.logger.log(`All Google Analytics request successfully replayed; ` + `the queue is now empty!`);
- }
- };
- };
-
- const createCollectRoutes = bgSyncPlugin => {
- const match = ({
- url
- }) => url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
- const handler = new NetworkOnly_js.NetworkOnly({
- plugins: [bgSyncPlugin]
- });
- return [new Route_js.Route(match, handler, 'GET'), new Route_js.Route(match, handler, 'POST')];
- };
-
- const createAnalyticsJsRoute = cacheName => {
- const match = ({
- url
- }) => url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
- const handler = new NetworkFirst_js.NetworkFirst({
- cacheName
- });
- return new Route_js.Route(match, handler, 'GET');
- };
-
- const createGtagJsRoute = cacheName => {
- const match = ({
- url
- }) => url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
- const handler = new NetworkFirst_js.NetworkFirst({
- cacheName
- });
- return new Route_js.Route(match, handler, 'GET');
- };
-
- const createGtmJsRoute = cacheName => {
- const match = ({
- url
- }) => url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
- const handler = new NetworkFirst_js.NetworkFirst({
- cacheName
- });
- return new Route_js.Route(match, handler, 'GET');
- };
-
- const initialize = (options = {}) => {
- const cacheName = cacheNames_js.cacheNames.getGoogleAnalyticsName(options.cacheName);
- const bgSyncPlugin = new BackgroundSyncPlugin_js.BackgroundSyncPlugin(QUEUE_NAME, {
- maxRetentionTime: MAX_RETENTION_TIME,
- onSync: createOnSyncCallback(options)
- });
- const routes = [createGtmJsRoute(cacheName), createAnalyticsJsRoute(cacheName), createGtagJsRoute(cacheName), ...createCollectRoutes(bgSyncPlugin)];
- const router = new Router_js.Router();
- for (const route of routes) {
- router.registerRoute(route);
- }
- router.addFetchListener();
- };
- exports.initialize = initialize;
- return exports;
- }({}, workbox.backgroundSync, workbox.core._private, workbox.core._private, workbox.core._private, workbox.routing, workbox.routing, workbox.strategies, workbox.strategies));
|