workbox-google-analytics
Advanced tools
Comparing version 5.0.0-alpha.0 to 5.0.0-alpha.1
this.workbox = this.workbox || {}; | ||
this.workbox.googleAnalytics = (function (exports, Plugin_mjs, cacheNames_mjs, getFriendlyURL_mjs, logger_mjs, Route_mjs, Router_mjs, NetworkFirst_mjs, NetworkOnly_mjs) { | ||
'use strict'; | ||
this.workbox.googleAnalytics = (function (exports, Plugin_js, cacheNames_js, getFriendlyURL_js, logger_js, Route_js, Router_js, NetworkFirst_js, NetworkOnly_js) { | ||
'use strict'; | ||
try { | ||
self['workbox:google-analytics:5.0.0-alpha.0'] && _(); | ||
} catch (e) {} // eslint-disable-line | ||
// @ts-ignore | ||
try { | ||
self['workbox:google-analytics:5.0.0-alpha.1'] && _(); | ||
} catch (e) {} | ||
/* | ||
Copyright 2018 Google LLC | ||
/* | ||
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 | ||
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 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/; | ||
const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/; | ||
/* | ||
Copyright 2018 Google LLC | ||
/* | ||
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 queue 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 workbox.googleAnalytics.initialize. | ||
* @return {Function} The requestWillDequeu callback function. | ||
* | ||
* @private | ||
*/ | ||
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 queue 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 workbox.googleAnalytics.initialize. | ||
* @return {Function} The requestWillDequeu callback function. | ||
* | ||
* @private | ||
*/ | ||
const createOnSyncCallback = config => { | ||
return async ({ | ||
queue | ||
}) => { | ||
let entry; | ||
const createOnSyncCallback = config => { | ||
return async ({ | ||
queue | ||
}) => { | ||
let entry; | ||
while (entry = await queue.shiftRequest()) { | ||
const { | ||
request, | ||
timestamp | ||
} = entry; | ||
const url = new URL(request.url); | ||
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. | ||
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. | ||
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', queueTime); // Apply `paramterOverrideds`, if set. | ||
params.set('qt', String(queueTime)); // Apply `paramterOverrideds`, 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 (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. | ||
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' | ||
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_mjs.logger.log(`Request for '${getFriendlyURL_mjs.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.`); | ||
} | ||
{ | ||
logger_mjs.logger.log(`Request for '${getFriendlyURL_mjs.getFriendlyURL(url.href)}'` + `failed to replay, putting it back in the queue.`); | ||
throw err; | ||
} | ||
} | ||
throw err; | ||
{ | ||
logger_js.logger.log(`All Google Analytics request successfully replayed; ` + `the queue is now empty!`); | ||
} | ||
} | ||
{ | ||
logger_mjs.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 {Plugin} queuePlugin | ||
* @return {Array<Route>} The created routes. | ||
* | ||
* @private | ||
*/ | ||
/** | ||
* Creates GET and POST routes to catch failed Measurement Protocol hits. | ||
* | ||
* @param {Plugin} queuePlugin | ||
* @return {Array<Route>} The created routes. | ||
* | ||
* @private | ||
*/ | ||
const createCollectRoutes = queuePlugin => { | ||
const match = ({ | ||
url | ||
}) => url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname); | ||
const createCollectRoutes = queuePlugin => { | ||
const match = ({ | ||
url | ||
}) => url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname); | ||
const handler = new NetworkOnly_mjs.NetworkOnly({ | ||
plugins: [queuePlugin] | ||
}); | ||
return [new Route_mjs.Route(match, handler, 'GET'), new Route_mjs.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 handler = new NetworkOnly_js.NetworkOnly({ | ||
plugins: [queuePlugin] | ||
}); | ||
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 createAnalyticsJsRoute = cacheName => { | ||
const match = ({ | ||
url | ||
}) => url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH; | ||
const handler = new NetworkFirst_mjs.NetworkFirst({ | ||
cacheName | ||
}); | ||
return new Route_mjs.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 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 createGtagJsRoute = cacheName => { | ||
const match = ({ | ||
url | ||
}) => url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH; | ||
const handler = new NetworkFirst_mjs.NetworkFirst({ | ||
cacheName | ||
}); | ||
return new Route_mjs.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 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 createGtmJsRoute = cacheName => { | ||
const match = ({ | ||
url | ||
}) => url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH; | ||
const handler = new NetworkFirst_mjs.NetworkFirst({ | ||
cacheName | ||
}); | ||
return new Route_mjs.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.googleAnalytics | ||
*/ | ||
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.googleAnalytics | ||
*/ | ||
const initialize = (options = {}) => { | ||
const cacheName = cacheNames_mjs.cacheNames.getGoogleAnalyticsName(options.cacheName); | ||
const queuePlugin = new Plugin_mjs.Plugin(QUEUE_NAME, { | ||
maxRetentionTime: MAX_RETENTION_TIME, | ||
onSync: createOnSyncCallback(options) | ||
}); | ||
const routes = [createGtmJsRoute(cacheName), createAnalyticsJsRoute(cacheName), createGtagJsRoute(cacheName), ...createCollectRoutes(queuePlugin)]; | ||
const router = new Router_mjs.Router(); | ||
const initialize = (options = {}) => { | ||
const cacheName = cacheNames_js.cacheNames.getGoogleAnalyticsName(options.cacheName); | ||
const queuePlugin = new Plugin_js.Plugin(QUEUE_NAME, { | ||
maxRetentionTime: MAX_RETENTION_TIME, | ||
onSync: createOnSyncCallback(options) | ||
}); | ||
const routes = [createGtmJsRoute(cacheName), createAnalyticsJsRoute(cacheName), createGtagJsRoute(cacheName), ...createCollectRoutes(queuePlugin)]; | ||
const router = new Router_js.Router(); | ||
for (const route of routes) { | ||
router.registerRoute(route); | ||
} | ||
for (const route of routes) { | ||
router.registerRoute(route); | ||
} | ||
router.addFetchListener(); | ||
}; | ||
router.addFetchListener(); | ||
}; | ||
/* | ||
Copyright 2018 Google LLC | ||
/* | ||
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. | ||
*/ | ||
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. | ||
*/ | ||
exports.initialize = initialize; | ||
exports.initialize = initialize; | ||
return exports; | ||
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 |
@@ -1,2 +0,2 @@ | ||
this.workbox=this.workbox||{},this.workbox.googleAnalytics=function(e,t,o,n,a,c,w){"use strict";try{self["workbox:google-analytics:5.0.0-alpha.0"]&&_()}catch(e){}const r=/^\/(\w+\/)?collect/,s=e=>async({queue:t})=>{let o;for(;o=await t.shiftRequest();){const{request:n,timestamp:a}=o,c=new URL(n.url);try{const w="POST"===n.method?new URLSearchParams(await n.clone().text()):c.searchParams,r=a-(Number(w.get("qt"))||0),s=Date.now()-r;if(w.set("qt",s),e.parameterOverrides)for(const t of Object.keys(e.parameterOverrides)){const o=e.parameterOverrides[t];w.set(t,o)}"function"==typeof e.hitFilter&&e.hitFilter.call(null,w),await fetch(new Request(c.origin+c.pathname,{body:w.toString(),method:"POST",mode:"cors",credentials:"omit",headers:{"Content-Type":"text/plain"}}))}catch(e){throw await t.unshiftRequest(o),e}}},i=e=>{const t=({url:e})=>"www.google-analytics.com"===e.hostname&&r.test(e.pathname),o=new w.NetworkOnly({plugins:[e]});return[new n.Route(t,o,"GET"),new n.Route(t,o,"POST")]},l=e=>{const t=new c.NetworkFirst({cacheName:e});return new n.Route(({url:e})=>"www.google-analytics.com"===e.hostname&&"/analytics.js"===e.pathname,t,"GET")},m=e=>{const t=new c.NetworkFirst({cacheName:e});return new n.Route(({url:e})=>"www.googletagmanager.com"===e.hostname&&"/gtag/js"===e.pathname,t,"GET")},u=e=>{const t=new c.NetworkFirst({cacheName:e});return new n.Route(({url:e})=>"www.googletagmanager.com"===e.hostname&&"/gtm.js"===e.pathname,t,"GET")};return e.initialize=((e={})=>{const n=o.cacheNames.getGoogleAnalyticsName(e.cacheName),c=new t.Plugin("workbox-google-analytics",{maxRetentionTime:2880,onSync:s(e)}),w=[u(n),l(n),m(n),...i(c)],r=new a.Router;for(const e of w)r.registerRoute(e);r.addFetchListener()}),e}({},workbox.backgroundSync,workbox.core._private,workbox.routing,workbox.routing,workbox.strategies,workbox.strategies); | ||
this.workbox=this.workbox||{},this.workbox.googleAnalytics=function(t,e,o,n,a,c,w,r,s){"use strict";try{self["workbox:google-analytics:5.0.0-alpha.1"]&&_()}catch(t){}const i=/^\/(\w+\/)?collect/,l=t=>async({queue:e})=>{let o;for(;o=await e.shiftRequest();){const{request:n,timestamp:a}=o,c=new URL(n.url);try{const w="POST"===n.method?new URLSearchParams(await n.clone().text()):c.searchParams,r=a-(Number(w.get("qt"))||0),s=Date.now()-r;if(w.set("qt",String(s)),t.parameterOverrides)for(const e of Object.keys(t.parameterOverrides)){const o=t.parameterOverrides[e];w.set(e,o)}"function"==typeof t.hitFilter&&t.hitFilter.call(null,w),await fetch(new Request(c.origin+c.pathname,{body:w.toString(),method:"POST",mode:"cors",credentials:"omit",headers:{"Content-Type":"text/plain"}}))}catch(t){throw await e.unshiftRequest(o),t}}},g=t=>{const e=({url:t})=>"www.google-analytics.com"===t.hostname&&i.test(t.pathname),o=new s.NetworkOnly({plugins:[t]});return[new c.Route(e,o,"GET"),new c.Route(e,o,"POST")]},m=t=>{const e=new r.NetworkFirst({cacheName:t});return new c.Route(({url:t})=>"www.google-analytics.com"===t.hostname&&"/analytics.js"===t.pathname,e,"GET")},u=t=>{const e=new r.NetworkFirst({cacheName:t});return new c.Route(({url:t})=>"www.googletagmanager.com"===t.hostname&&"/gtag/js"===t.pathname,e,"GET")},h=t=>{const e=new r.NetworkFirst({cacheName:t});return new c.Route(({url:t})=>"www.googletagmanager.com"===t.hostname&&"/gtm.js"===t.pathname,e,"GET")};return t.initialize=((t={})=>{const n=o.cacheNames.getGoogleAnalyticsName(t.cacheName),a=new e.Plugin("workbox-google-analytics",{maxRetentionTime:2880,onSync:l(t)}),c=[h(n),m(n),u(n),...g(a)],r=new w.Router;for(const t of c)r.registerRoute(t);r.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 |
{ | ||
"name": "workbox-google-analytics", | ||
"version": "5.0.0-alpha.0", | ||
"version": "5.0.0-alpha.1", | ||
"license": "MIT", | ||
@@ -29,11 +29,12 @@ "author": "Google's Web DevRel Team", | ||
}, | ||
"main": "build/workbox-offline-ga.prod.js", | ||
"main": "index.js", | ||
"module": "index.mjs", | ||
"types": "index.d.ts", | ||
"dependencies": { | ||
"workbox-background-sync": "^5.0.0-alpha.0", | ||
"workbox-core": "^5.0.0-alpha.0", | ||
"workbox-routing": "^5.0.0-alpha.0", | ||
"workbox-strategies": "^5.0.0-alpha.0" | ||
"workbox-background-sync": "^5.0.0-alpha.1", | ||
"workbox-core": "^5.0.0-alpha.1", | ||
"workbox-routing": "^5.0.0-alpha.1", | ||
"workbox-strategies": "^5.0.0-alpha.1" | ||
}, | ||
"gitHead": "7f231c04023669bc42d5a939d1359b0867e2efda" | ||
"gitHead": "20d2110ddace710a46af06addd4977cae08f5942" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
88023
25
725
4