This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

19
frontend/node_modules/workbox-google-analytics/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,19 @@
Copyright 2018 Google LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1 @@
This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-google-analytics

View File

View File

@@ -0,0 +1,6 @@
"use strict";
// @ts-ignore
try {
self['workbox:google-analytics:6.5.4'] && _();
}
catch (e) { }

View File

@@ -0,0 +1 @@
try{self['workbox:google-analytics:6.6.0']&&_()}catch(e){}// eslint-disable-line

View File

@@ -0,0 +1,235 @@
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:6.5.4'] && _();
} catch (e) {}
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
const QUEUE_NAME = 'workbox-google-analytics';
const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
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';
// endpoints. Most of the time the default path (/collect) is used, but
// occasionally an experimental endpoint is used when testing new features,
// (e.g. /r/collect or /j/collect)
const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**
* Creates the requestWillDequeue callback to be used with the background
* sync plugin. The callback takes the failed request and adds the
* `qt` param based on the current time, as well as applies any other
* user-defined hit modifications.
*
* @param {Object} config See {@link workbox-google-analytics.initialize}.
* @return {Function} The requestWillDequeue callback function.
*
* @private
*/
const createOnSyncCallback = config => {
return async ({
queue
}) => {
let entry;
while (entry = await queue.shiftRequest()) {
const {
request,
timestamp
} = entry;
const url = new URL(request.url);
try {
// Measurement protocol requests can set their payload parameters in
// either the URL query string (for GET requests) or the POST body.
const params = request.method === 'POST' ? new URLSearchParams(await request.clone().text()) : url.searchParams; // Calculate the qt param, accounting for the fact that an existing
// qt param may be present and should be updated rather than replaced.
const originalHitTime = timestamp - (Number(params.get('qt')) || 0);
const queueTime = Date.now() - originalHitTime; // Set the qt param prior to applying hitFilter or parameterOverrides.
params.set('qt', String(queueTime)); // Apply `parameterOverrides`, if set.
if (config.parameterOverrides) {
for (const param of Object.keys(config.parameterOverrides)) {
const value = config.parameterOverrides[param];
params.set(param, value);
}
} // Apply `hitFilter`, if set.
if (typeof config.hitFilter === 'function') {
config.hitFilter.call(null, params);
} // Retry the fetch. Ignore URL search params from the URL as they're
// now in the post body.
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!`);
}
};
};
/**
* Creates GET and POST routes to catch failed Measurement Protocol hits.
*
* @param {BackgroundSyncPlugin} bgSyncPlugin
* @return {Array<Route>} The created routes.
*
* @private
*/
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')];
};
/**
* Creates a route with a network first strategy for the analytics.js script.
*
* @param {string} cacheName
* @return {Route} The created route.
*
* @private
*/
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');
};
/**
* Creates a route with a network first strategy for the gtag.js script.
*
* @param {string} cacheName
* @return {Route} The created route.
*
* @private
*/
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');
};
/**
* Creates a route with a network first strategy for the gtm.js script.
*
* @param {string} cacheName
* @return {Route} The created route.
*
* @private
*/
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');
};
/**
* @param {Object=} [options]
* @param {Object} [options.cacheName] The cache name to store and retrieve
* analytics.js. Defaults to the cache names provided by `workbox-core`.
* @param {Object} [options.parameterOverrides]
* [Measurement Protocol parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters),
* expressed as key/value pairs, to be added to replayed Google Analytics
* requests. This can be used to, e.g., set a custom dimension indicating
* that the request was replayed.
* @param {Function} [options.hitFilter] A function that allows you to modify
* the hit parameters prior to replaying
* the hit. The function is invoked with the original hit's URLSearchParams
* object as its only argument.
*
* @memberof workbox-google-analytics
*/
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));
//# sourceMappingURL=workbox-offline-ga.dev.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,2 @@
this.workbox=this.workbox||{},this.workbox.googleAnalytics=function(t,e,o,n,r,c,a,w,s){"use strict";try{self["workbox:google-analytics:6.5.4"]&&_()}catch(t){}const i="www.google-analytics.com",l="www.googletagmanager.com",u=/^\/(\w+\/)?collect/,m=t=>{const e=({url:t})=>t.hostname===i&&u.test(t.pathname),o=new s.NetworkOnly({plugins:[t]});return[new c.Route(e,o,"GET"),new c.Route(e,o,"POST")]},g=t=>{const e=new w.NetworkFirst({cacheName:t});return new c.Route((({url:t})=>t.hostname===i&&"/analytics.js"===t.pathname),e,"GET")},h=t=>{const e=new w.NetworkFirst({cacheName:t});return new c.Route((({url:t})=>t.hostname===l&&"/gtag/js"===t.pathname),e,"GET")},b=t=>{const e=new w.NetworkFirst({cacheName:t});return new c.Route((({url:t})=>t.hostname===l&&"/gtm.js"===t.pathname),e,"GET")};return t.initialize=(t={})=>{const n=o.cacheNames.getGoogleAnalyticsName(t.cacheName),r=new e.BackgroundSyncPlugin("workbox-google-analytics",{maxRetentionTime:2880,onSync:(c=t,async({queue:t})=>{let e;for(;e=await t.shiftRequest();){const{request:o,timestamp:n}=e,r=new URL(o.url);try{const t="POST"===o.method?new URLSearchParams(await o.clone().text()):r.searchParams,e=n-(Number(t.get("qt"))||0),a=Date.now()-e;if(t.set("qt",String(a)),c.parameterOverrides)for(const e of Object.keys(c.parameterOverrides)){const o=c.parameterOverrides[e];t.set(e,o)}"function"==typeof c.hitFilter&&c.hitFilter.call(null,t),await fetch(new Request(r.origin+r.pathname,{body:t.toString(),method:"POST",mode:"cors",credentials:"omit",headers:{"Content-Type":"text/plain"}}))}catch(o){throw await t.unshiftRequest(e),o}}})});var c;const w=[b(n),g(n),h(n),...m(r)],s=new a.Router;for(const t of w)s.registerRoute(t);s.addFetchListener()},t}({},workbox.backgroundSync,workbox.core._private,workbox.core._private,workbox.core._private,workbox.routing,workbox.routing,workbox.strategies,workbox.strategies);
//# sourceMappingURL=workbox-offline-ga.prod.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,6 @@
import { initialize, GoogleAnalyticsInitializeOptions } from './initialize.js';
import './_version.js';
/**
* @module workbox-google-analytics
*/
export { initialize, GoogleAnalyticsInitializeOptions };

View File

@@ -0,0 +1,13 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { initialize } from './initialize.js';
import './_version.js';
/**
* @module workbox-google-analytics
*/
export { initialize };

View File

@@ -0,0 +1 @@
export * from './index.js';

View File

@@ -0,0 +1,26 @@
import './_version.js';
export interface GoogleAnalyticsInitializeOptions {
cacheName?: string;
parameterOverrides?: {
[paramName: string]: string;
};
hitFilter?: (params: URLSearchParams) => void;
}
/**
* @param {Object=} [options]
* @param {Object} [options.cacheName] The cache name to store and retrieve
* analytics.js. Defaults to the cache names provided by `workbox-core`.
* @param {Object} [options.parameterOverrides]
* [Measurement Protocol parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters),
* expressed as key/value pairs, to be added to replayed Google Analytics
* requests. This can be used to, e.g., set a custom dimension indicating
* that the request was replayed.
* @param {Function} [options.hitFilter] A function that allows you to modify
* the hit parameters prior to replaying
* the hit. The function is invoked with the original hit's URLSearchParams
* object as its only argument.
*
* @memberof workbox-google-analytics
*/
declare const initialize: (options?: GoogleAnalyticsInitializeOptions) => void;
export { initialize };

View File

@@ -0,0 +1,176 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { BackgroundSyncPlugin } from 'workbox-background-sync/BackgroundSyncPlugin.js';
import { cacheNames } from 'workbox-core/_private/cacheNames.js';
import { getFriendlyURL } from 'workbox-core/_private/getFriendlyURL.js';
import { logger } from 'workbox-core/_private/logger.js';
import { Route } from 'workbox-routing/Route.js';
import { Router } from 'workbox-routing/Router.js';
import { NetworkFirst } from 'workbox-strategies/NetworkFirst.js';
import { NetworkOnly } from 'workbox-strategies/NetworkOnly.js';
import { QUEUE_NAME, MAX_RETENTION_TIME, GOOGLE_ANALYTICS_HOST, GTM_HOST, ANALYTICS_JS_PATH, GTAG_JS_PATH, GTM_JS_PATH, COLLECT_PATHS_REGEX, } from './utils/constants.js';
import './_version.js';
/**
* Creates the requestWillDequeue callback to be used with the background
* sync plugin. The callback takes the failed request and adds the
* `qt` param based on the current time, as well as applies any other
* user-defined hit modifications.
*
* @param {Object} config See {@link workbox-google-analytics.initialize}.
* @return {Function} The requestWillDequeue callback function.
*
* @private
*/
const createOnSyncCallback = (config) => {
return async ({ queue }) => {
let entry;
while ((entry = await queue.shiftRequest())) {
const { request, timestamp } = entry;
const url = new URL(request.url);
try {
// Measurement protocol requests can set their payload parameters in
// either the URL query string (for GET requests) or the POST body.
const params = request.method === 'POST'
? new URLSearchParams(await request.clone().text())
: url.searchParams;
// Calculate the qt param, accounting for the fact that an existing
// qt param may be present and should be updated rather than replaced.
const originalHitTime = timestamp - (Number(params.get('qt')) || 0);
const queueTime = Date.now() - originalHitTime;
// Set the qt param prior to applying hitFilter or parameterOverrides.
params.set('qt', String(queueTime));
// Apply `parameterOverrides`, if set.
if (config.parameterOverrides) {
for (const param of Object.keys(config.parameterOverrides)) {
const value = config.parameterOverrides[param];
params.set(param, value);
}
}
// Apply `hitFilter`, if set.
if (typeof config.hitFilter === 'function') {
config.hitFilter.call(null, params);
}
// Retry the fetch. Ignore URL search params from the URL as they're
// now in the post body.
await fetch(new Request(url.origin + url.pathname, {
body: params.toString(),
method: 'POST',
mode: 'cors',
credentials: 'omit',
headers: { 'Content-Type': 'text/plain' },
}));
if (process.env.NODE_ENV !== 'production') {
logger.log(`Request for '${getFriendlyURL(url.href)}' ` + `has been replayed`);
}
}
catch (err) {
await queue.unshiftRequest(entry);
if (process.env.NODE_ENV !== 'production') {
logger.log(`Request for '${getFriendlyURL(url.href)}' ` +
`failed to replay, putting it back in the queue.`);
}
throw err;
}
}
if (process.env.NODE_ENV !== 'production') {
logger.log(`All Google Analytics request successfully replayed; ` +
`the queue is now empty!`);
}
};
};
/**
* Creates GET and POST routes to catch failed Measurement Protocol hits.
*
* @param {BackgroundSyncPlugin} bgSyncPlugin
* @return {Array<Route>} The created routes.
*
* @private
*/
const createCollectRoutes = (bgSyncPlugin) => {
const match = ({ url }) => url.hostname === GOOGLE_ANALYTICS_HOST &&
COLLECT_PATHS_REGEX.test(url.pathname);
const handler = new NetworkOnly({
plugins: [bgSyncPlugin],
});
return [new Route(match, handler, 'GET'), new Route(match, handler, 'POST')];
};
/**
* Creates a route with a network first strategy for the analytics.js script.
*
* @param {string} cacheName
* @return {Route} The created route.
*
* @private
*/
const createAnalyticsJsRoute = (cacheName) => {
const match = ({ url }) => url.hostname === GOOGLE_ANALYTICS_HOST &&
url.pathname === ANALYTICS_JS_PATH;
const handler = new NetworkFirst({ cacheName });
return new Route(match, handler, 'GET');
};
/**
* Creates a route with a network first strategy for the gtag.js script.
*
* @param {string} cacheName
* @return {Route} The created route.
*
* @private
*/
const createGtagJsRoute = (cacheName) => {
const match = ({ url }) => url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
const handler = new NetworkFirst({ cacheName });
return new Route(match, handler, 'GET');
};
/**
* Creates a route with a network first strategy for the gtm.js script.
*
* @param {string} cacheName
* @return {Route} The created route.
*
* @private
*/
const createGtmJsRoute = (cacheName) => {
const match = ({ url }) => url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
const handler = new NetworkFirst({ cacheName });
return new Route(match, handler, 'GET');
};
/**
* @param {Object=} [options]
* @param {Object} [options.cacheName] The cache name to store and retrieve
* analytics.js. Defaults to the cache names provided by `workbox-core`.
* @param {Object} [options.parameterOverrides]
* [Measurement Protocol parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters),
* expressed as key/value pairs, to be added to replayed Google Analytics
* requests. This can be used to, e.g., set a custom dimension indicating
* that the request was replayed.
* @param {Function} [options.hitFilter] A function that allows you to modify
* the hit parameters prior to replaying
* the hit. The function is invoked with the original hit's URLSearchParams
* object as its only argument.
*
* @memberof workbox-google-analytics
*/
const initialize = (options = {}) => {
const cacheName = cacheNames.getGoogleAnalyticsName(options.cacheName);
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
maxRetentionTime: MAX_RETENTION_TIME,
onSync: createOnSyncCallback(options),
});
const routes = [
createGtmJsRoute(cacheName),
createAnalyticsJsRoute(cacheName),
createGtagJsRoute(cacheName),
...createCollectRoutes(bgSyncPlugin),
];
const router = new Router();
for (const route of routes) {
router.registerRoute(route);
}
router.addFetchListener();
};
export { initialize };

View File

@@ -0,0 +1 @@
export * from './initialize.js';

View File

@@ -0,0 +1,34 @@
{
"name": "workbox-google-analytics",
"version": "6.6.0",
"license": "MIT",
"author": "Google's Web DevRel Team",
"description": "Queues failed requests and uses the Background Sync API to replay them when the network is available",
"repository": "googlechrome/workbox",
"bugs": "https://github.com/googlechrome/workbox/issues",
"homepage": "https://github.com/GoogleChrome/workbox",
"keywords": [
"workbox",
"workboxjs",
"service worker",
"sw",
"offline",
"google",
"analytics"
],
"workbox": {
"browserNamespace": "workbox.googleAnalytics",
"outputFilename": "workbox-offline-ga",
"packageType": "sw"
},
"main": "index.js",
"module": "index.mjs",
"types": "index.d.ts",
"dependencies": {
"workbox-background-sync": "6.6.0",
"workbox-core": "6.6.0",
"workbox-routing": "6.6.0",
"workbox-strategies": "6.6.0"
},
"gitHead": "252644491d9bb5a67518935ede6df530107c9475"
}

View File

@@ -0,0 +1,2 @@
// @ts-ignore
try{self['workbox:google-analytics:6.6.0']&&_()}catch(e){}

View File

@@ -0,0 +1,17 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {initialize, GoogleAnalyticsInitializeOptions} from './initialize.js';
import './_version.js';
/**
* @module workbox-google-analytics
*/
export {initialize, GoogleAnalyticsInitializeOptions};

View File

@@ -0,0 +1,233 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {BackgroundSyncPlugin} from 'workbox-background-sync/BackgroundSyncPlugin.js';
import {Queue} from 'workbox-background-sync/Queue.js';
import {cacheNames} from 'workbox-core/_private/cacheNames.js';
import {getFriendlyURL} from 'workbox-core/_private/getFriendlyURL.js';
import {logger} from 'workbox-core/_private/logger.js';
import {RouteMatchCallbackOptions} from 'workbox-core/types.js';
import {Route} from 'workbox-routing/Route.js';
import {Router} from 'workbox-routing/Router.js';
import {NetworkFirst} from 'workbox-strategies/NetworkFirst.js';
import {NetworkOnly} from 'workbox-strategies/NetworkOnly.js';
import {
QUEUE_NAME,
MAX_RETENTION_TIME,
GOOGLE_ANALYTICS_HOST,
GTM_HOST,
ANALYTICS_JS_PATH,
GTAG_JS_PATH,
GTM_JS_PATH,
COLLECT_PATHS_REGEX,
} from './utils/constants.js';
import './_version.js';
export interface GoogleAnalyticsInitializeOptions {
cacheName?: string;
parameterOverrides?: {[paramName: string]: string};
hitFilter?: (params: URLSearchParams) => void;
}
/**
* Creates the requestWillDequeue callback to be used with the background
* sync plugin. The callback takes the failed request and adds the
* `qt` param based on the current time, as well as applies any other
* user-defined hit modifications.
*
* @param {Object} config See {@link workbox-google-analytics.initialize}.
* @return {Function} The requestWillDequeue callback function.
*
* @private
*/
const createOnSyncCallback = (config: GoogleAnalyticsInitializeOptions) => {
return async ({queue}: {queue: Queue}) => {
let entry;
while ((entry = await queue.shiftRequest())) {
const {request, timestamp} = entry;
const url = new URL(request.url);
try {
// Measurement protocol requests can set their payload parameters in
// either the URL query string (for GET requests) or the POST body.
const params =
request.method === 'POST'
? new URLSearchParams(await request.clone().text())
: url.searchParams;
// Calculate the qt param, accounting for the fact that an existing
// qt param may be present and should be updated rather than replaced.
const originalHitTime = timestamp! - (Number(params.get('qt')) || 0);
const queueTime = Date.now() - originalHitTime;
// Set the qt param prior to applying hitFilter or parameterOverrides.
params.set('qt', String(queueTime));
// Apply `parameterOverrides`, if set.
if (config.parameterOverrides) {
for (const param of Object.keys(config.parameterOverrides)) {
const value = config.parameterOverrides[param];
params.set(param, value);
}
}
// Apply `hitFilter`, if set.
if (typeof config.hitFilter === 'function') {
config.hitFilter.call(null, params);
}
// Retry the fetch. Ignore URL search params from the URL as they're
// now in the post body.
await fetch(
new Request(url.origin + url.pathname, {
body: params.toString(),
method: 'POST',
mode: 'cors',
credentials: 'omit',
headers: {'Content-Type': 'text/plain'},
}),
);
if (process.env.NODE_ENV !== 'production') {
logger.log(
`Request for '${getFriendlyURL(url.href)}' ` + `has been replayed`,
);
}
} catch (err) {
await queue.unshiftRequest(entry);
if (process.env.NODE_ENV !== 'production') {
logger.log(
`Request for '${getFriendlyURL(url.href)}' ` +
`failed to replay, putting it back in the queue.`,
);
}
throw err;
}
}
if (process.env.NODE_ENV !== 'production') {
logger.log(
`All Google Analytics request successfully replayed; ` +
`the queue is now empty!`,
);
}
};
};
/**
* Creates GET and POST routes to catch failed Measurement Protocol hits.
*
* @param {BackgroundSyncPlugin} bgSyncPlugin
* @return {Array<Route>} The created routes.
*
* @private
*/
const createCollectRoutes = (bgSyncPlugin: BackgroundSyncPlugin) => {
const match = ({url}: RouteMatchCallbackOptions) =>
url.hostname === GOOGLE_ANALYTICS_HOST &&
COLLECT_PATHS_REGEX.test(url.pathname);
const handler = new NetworkOnly({
plugins: [bgSyncPlugin],
});
return [new Route(match, handler, 'GET'), new Route(match, handler, 'POST')];
};
/**
* Creates a route with a network first strategy for the analytics.js script.
*
* @param {string} cacheName
* @return {Route} The created route.
*
* @private
*/
const createAnalyticsJsRoute = (cacheName: string) => {
const match = ({url}: RouteMatchCallbackOptions) =>
url.hostname === GOOGLE_ANALYTICS_HOST &&
url.pathname === ANALYTICS_JS_PATH;
const handler = new NetworkFirst({cacheName});
return new Route(match, handler, 'GET');
};
/**
* Creates a route with a network first strategy for the gtag.js script.
*
* @param {string} cacheName
* @return {Route} The created route.
*
* @private
*/
const createGtagJsRoute = (cacheName: string) => {
const match = ({url}: RouteMatchCallbackOptions) =>
url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
const handler = new NetworkFirst({cacheName});
return new Route(match, handler, 'GET');
};
/**
* Creates a route with a network first strategy for the gtm.js script.
*
* @param {string} cacheName
* @return {Route} The created route.
*
* @private
*/
const createGtmJsRoute = (cacheName: string) => {
const match = ({url}: RouteMatchCallbackOptions) =>
url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
const handler = new NetworkFirst({cacheName});
return new Route(match, handler, 'GET');
};
/**
* @param {Object=} [options]
* @param {Object} [options.cacheName] The cache name to store and retrieve
* analytics.js. Defaults to the cache names provided by `workbox-core`.
* @param {Object} [options.parameterOverrides]
* [Measurement Protocol parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters),
* expressed as key/value pairs, to be added to replayed Google Analytics
* requests. This can be used to, e.g., set a custom dimension indicating
* that the request was replayed.
* @param {Function} [options.hitFilter] A function that allows you to modify
* the hit parameters prior to replaying
* the hit. The function is invoked with the original hit's URLSearchParams
* object as its only argument.
*
* @memberof workbox-google-analytics
*/
const initialize = (options: GoogleAnalyticsInitializeOptions = {}): void => {
const cacheName = cacheNames.getGoogleAnalyticsName(options.cacheName);
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
maxRetentionTime: MAX_RETENTION_TIME,
onSync: createOnSyncCallback(options),
});
const routes = [
createGtmJsRoute(cacheName),
createAnalyticsJsRoute(cacheName),
createGtagJsRoute(cacheName),
...createCollectRoutes(bgSyncPlugin),
];
const router = new Router();
for (const route of routes) {
router.registerRoute(route);
}
router.addFetchListener();
};
export {initialize};

View File

@@ -0,0 +1,24 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import '../_version.js';
export const QUEUE_NAME = 'workbox-google-analytics';
export const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
export const GOOGLE_ANALYTICS_HOST = 'www.google-analytics.com';
export const GTM_HOST = 'www.googletagmanager.com';
export const ANALYTICS_JS_PATH = '/analytics.js';
export const GTAG_JS_PATH = '/gtag/js';
export const GTM_JS_PATH = '/gtm.js';
export const COLLECT_DEFAULT_PATH = '/collect';
// This RegExp matches all known Measurement Protocol single-hit collect
// endpoints. Most of the time the default path (/collect) is used, but
// occasionally an experimental endpoint is used when testing new features,
// (e.g. /r/collect or /j/collect)
export const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;

View File

@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"outDir": "./",
"rootDir": "./src",
"tsBuildInfoFile": "./tsconfig.tsbuildinfo"
},
"include": ["src/**/*.ts"],
"references": [
{"path": "../workbox-background-sync/"},
{"path": "../workbox-core/"},
{"path": "../workbox-routing/"},
{"path": "../workbox-strategies/"}
]
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,10 @@
import '../_version.js';
export declare const QUEUE_NAME = "workbox-google-analytics";
export declare const MAX_RETENTION_TIME: number;
export declare const GOOGLE_ANALYTICS_HOST = "www.google-analytics.com";
export declare const GTM_HOST = "www.googletagmanager.com";
export declare const ANALYTICS_JS_PATH = "/analytics.js";
export declare const GTAG_JS_PATH = "/gtag/js";
export declare const GTM_JS_PATH = "/gtm.js";
export declare const COLLECT_DEFAULT_PATH = "/collect";
export declare const COLLECT_PATHS_REGEX: RegExp;

View File

@@ -0,0 +1,21 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import '../_version.js';
export const QUEUE_NAME = 'workbox-google-analytics';
export const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
export const GOOGLE_ANALYTICS_HOST = 'www.google-analytics.com';
export const GTM_HOST = 'www.googletagmanager.com';
export const ANALYTICS_JS_PATH = '/analytics.js';
export const GTAG_JS_PATH = '/gtag/js';
export const GTM_JS_PATH = '/gtm.js';
export const COLLECT_DEFAULT_PATH = '/collect';
// This RegExp matches all known Measurement Protocol single-hit collect
// endpoints. Most of the time the default path (/collect) is used, but
// occasionally an experimental endpoint is used when testing new features,
// (e.g. /r/collect or /j/collect)
export const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;

View File

@@ -0,0 +1 @@
export * from './constants.js';