1 | this.workbox = this.workbox || {};
|
2 | this.workbox.googleAnalytics = (function (exports, BackgroundSyncPlugin_js, cacheNames_js, getFriendlyURL_js, logger_js, Route_js, Router_js, NetworkFirst_js, NetworkOnly_js) {
|
3 | 'use strict';
|
4 |
|
5 | try {
|
6 | self['workbox:google-analytics:5.1.4'] && _();
|
7 | } catch (e) {}
|
8 |
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const QUEUE_NAME = 'workbox-google-analytics';
|
17 | const MAX_RETENTION_TIME = 60 * 48;
|
18 |
|
19 | const GOOGLE_ANALYTICS_HOST = 'www.google-analytics.com';
|
20 | const GTM_HOST = 'www.googletagmanager.com';
|
21 | const ANALYTICS_JS_PATH = '/analytics.js';
|
22 | const GTAG_JS_PATH = '/gtag/js';
|
23 | const GTM_JS_PATH = '/gtm.js';
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | const createOnSyncCallback = config => {
|
50 | return async ({
|
51 | queue
|
52 | }) => {
|
53 | let entry;
|
54 |
|
55 | while (entry = await queue.shiftRequest()) {
|
56 | const {
|
57 | request,
|
58 | timestamp
|
59 | } = entry;
|
60 | const url = new URL(request.url);
|
61 |
|
62 | try {
|
63 |
|
64 |
|
65 | const params = request.method === 'POST' ? new URLSearchParams(await request.clone().text()) : url.searchParams;
|
66 |
|
67 |
|
68 | const originalHitTime = timestamp - (Number(params.get('qt')) || 0);
|
69 | const queueTime = Date.now() - originalHitTime;
|
70 |
|
71 | params.set('qt', String(queueTime));
|
72 |
|
73 | if (config.parameterOverrides) {
|
74 | for (const param of Object.keys(config.parameterOverrides)) {
|
75 | const value = config.parameterOverrides[param];
|
76 | params.set(param, value);
|
77 | }
|
78 | }
|
79 |
|
80 |
|
81 | if (typeof config.hitFilter === 'function') {
|
82 | config.hitFilter.call(null, params);
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 | await fetch(new Request(url.origin + url.pathname, {
|
88 | body: params.toString(),
|
89 | method: 'POST',
|
90 | mode: 'cors',
|
91 | credentials: 'omit',
|
92 | headers: {
|
93 | 'Content-Type': 'text/plain'
|
94 | }
|
95 | }));
|
96 |
|
97 | if ("dev" !== 'production') {
|
98 | logger_js.logger.log(`Request for '${getFriendlyURL_js.getFriendlyURL(url.href)}'` + `has been replayed`);
|
99 | }
|
100 | } catch (err) {
|
101 | await queue.unshiftRequest(entry);
|
102 |
|
103 | {
|
104 | logger_js.logger.log(`Request for '${getFriendlyURL_js.getFriendlyURL(url.href)}'` + `failed to replay, putting it back in the queue.`);
|
105 | }
|
106 |
|
107 | throw err;
|
108 | }
|
109 | }
|
110 |
|
111 | {
|
112 | logger_js.logger.log(`All Google Analytics request successfully replayed; ` + `the queue is now empty!`);
|
113 | }
|
114 | };
|
115 | };
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | const createCollectRoutes = bgSyncPlugin => {
|
127 | const match = ({
|
128 | url
|
129 | }) => url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
130 |
|
131 | const handler = new NetworkOnly_js.NetworkOnly({
|
132 | plugins: [bgSyncPlugin]
|
133 | });
|
134 | return [new Route_js.Route(match, handler, 'GET'), new Route_js.Route(match, handler, 'POST')];
|
135 | };
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | const createAnalyticsJsRoute = cacheName => {
|
147 | const match = ({
|
148 | url
|
149 | }) => url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
150 |
|
151 | const handler = new NetworkFirst_js.NetworkFirst({
|
152 | cacheName
|
153 | });
|
154 | return new Route_js.Route(match, handler, 'GET');
|
155 | };
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | const createGtagJsRoute = cacheName => {
|
167 | const match = ({
|
168 | url
|
169 | }) => url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
170 |
|
171 | const handler = new NetworkFirst_js.NetworkFirst({
|
172 | cacheName
|
173 | });
|
174 | return new Route_js.Route(match, handler, 'GET');
|
175 | };
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | const createGtmJsRoute = cacheName => {
|
187 | const match = ({
|
188 | url
|
189 | }) => url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
190 |
|
191 | const handler = new NetworkFirst_js.NetworkFirst({
|
192 | cacheName
|
193 | });
|
194 | return new Route_js.Route(match, handler, 'GET');
|
195 | };
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | const initialize = (options = {}) => {
|
215 | const cacheName = cacheNames_js.cacheNames.getGoogleAnalyticsName(options.cacheName);
|
216 | const bgSyncPlugin = new BackgroundSyncPlugin_js.BackgroundSyncPlugin(QUEUE_NAME, {
|
217 | maxRetentionTime: MAX_RETENTION_TIME,
|
218 | onSync: createOnSyncCallback(options)
|
219 | });
|
220 | const routes = [createGtmJsRoute(cacheName), createAnalyticsJsRoute(cacheName), createGtagJsRoute(cacheName), ...createCollectRoutes(bgSyncPlugin)];
|
221 | const router = new Router_js.Router();
|
222 |
|
223 | for (const route of routes) {
|
224 | router.registerRoute(route);
|
225 | }
|
226 |
|
227 | router.addFetchListener();
|
228 | };
|
229 |
|
230 | exports.initialize = initialize;
|
231 |
|
232 | return exports;
|
233 |
|
234 | }({}, workbox.backgroundSync, workbox.core._private, workbox.core._private, workbox.core._private, workbox.routing, workbox.routing, workbox.strategies, workbox.strategies));
|
235 |
|