@serwist/google-analytics 8.4.4 → 9.0.0-preview.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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -166
- package/dist/initialize.d.ts +1 -0
- package/dist/initialize.d.ts.map +1 -0
- package/dist/initialize.js +9 -63
- package/dist/utils/constants.d.ts +1 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/package.json +30 -27
- package/{dist/index.d.cts → src/index.ts} +9 -0
- package/src/initialize.ts +203 -0
- package/src/utils/constants.ts +22 -0
- package/dist/index.cjs +0 -168
- package/dist/initialize.cjs +0 -168
- package/dist/initialize.d.cts +0 -26
package/dist/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,gCAAgC,EAAE,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,OAAO,EAAE,UAAU,EAAE,CAAC;AACtB,YAAY,EAAE,gCAAgC,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,166 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
/*
|
|
7
|
-
Copyright 2018 Google LLC
|
|
8
|
-
|
|
9
|
-
Use of this source code is governed by an MIT-style
|
|
10
|
-
license that can be found in the LICENSE file or at
|
|
11
|
-
https://opensource.org/licenses/MIT.
|
|
12
|
-
*/ const QUEUE_NAME = "serwist-google-analytics";
|
|
13
|
-
const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
|
|
14
|
-
const GOOGLE_ANALYTICS_HOST = "www.google-analytics.com";
|
|
15
|
-
const GTM_HOST = "www.googletagmanager.com";
|
|
16
|
-
const ANALYTICS_JS_PATH = "/analytics.js";
|
|
17
|
-
const GTAG_JS_PATH = "/gtag/js";
|
|
18
|
-
const GTM_JS_PATH = "/gtm.js";
|
|
19
|
-
// This RegExp matches all known Measurement Protocol single-hit collect
|
|
20
|
-
// endpoints. Most of the time the default path (/collect) is used, but
|
|
21
|
-
// occasionally an experimental endpoint is used when testing new features,
|
|
22
|
-
// (e.g. /r/collect or /j/collect)
|
|
23
|
-
const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Creates the requestWillDequeue callback to be used with the background
|
|
27
|
-
* sync plugin. The callback takes the failed request and adds the
|
|
28
|
-
* `qt` param based on the current time, as well as applies any other
|
|
29
|
-
* user-defined hit modifications.
|
|
30
|
-
*
|
|
31
|
-
* @param config
|
|
32
|
-
* @returns The requestWillDequeue callback function.
|
|
33
|
-
* @private
|
|
34
|
-
*/ const createOnSyncCallback = (config)=>{
|
|
35
|
-
return async ({ queue })=>{
|
|
36
|
-
let entry = undefined;
|
|
37
|
-
while(entry = await queue.shiftRequest()){
|
|
38
|
-
const { request, timestamp } = entry;
|
|
39
|
-
const url = new URL(request.url);
|
|
40
|
-
try {
|
|
41
|
-
// Measurement protocol requests can set their payload parameters in
|
|
42
|
-
// either the URL query string (for GET requests) or the POST body.
|
|
43
|
-
const params = request.method === "POST" ? new URLSearchParams(await request.clone().text()) : url.searchParams;
|
|
44
|
-
// Calculate the qt param, accounting for the fact that an existing
|
|
45
|
-
// qt param may be present and should be updated rather than replaced.
|
|
46
|
-
const originalHitTime = timestamp - (Number(params.get("qt")) || 0);
|
|
47
|
-
const queueTime = Date.now() - originalHitTime;
|
|
48
|
-
// Set the qt param prior to applying hitFilter or parameterOverrides.
|
|
49
|
-
params.set("qt", String(queueTime));
|
|
50
|
-
// Apply `parameterOverrides`, if set.
|
|
51
|
-
if (config.parameterOverrides) {
|
|
52
|
-
for (const param of Object.keys(config.parameterOverrides)){
|
|
53
|
-
const value = config.parameterOverrides[param];
|
|
54
|
-
params.set(param, value);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
// Apply `hitFilter`, if set.
|
|
58
|
-
if (typeof config.hitFilter === "function") {
|
|
59
|
-
config.hitFilter.call(null, params);
|
|
60
|
-
}
|
|
61
|
-
// Retry the fetch. Ignore URL search params from the URL as they're
|
|
62
|
-
// now in the post body.
|
|
63
|
-
await fetch(new Request(url.origin + url.pathname, {
|
|
64
|
-
body: params.toString(),
|
|
65
|
-
method: "POST",
|
|
66
|
-
mode: "cors",
|
|
67
|
-
credentials: "omit",
|
|
68
|
-
headers: {
|
|
69
|
-
"Content-Type": "text/plain"
|
|
70
|
-
}
|
|
71
|
-
}));
|
|
72
|
-
if (process.env.NODE_ENV !== "production") {
|
|
73
|
-
logger.log(`Request for '${getFriendlyURL(url.href)}' has been replayed`);
|
|
74
|
-
}
|
|
75
|
-
} catch (err) {
|
|
76
|
-
await queue.unshiftRequest(entry);
|
|
77
|
-
if (process.env.NODE_ENV !== "production") {
|
|
78
|
-
logger.log(`Request for '${getFriendlyURL(url.href)}' failed to replay, putting it back in the queue.`);
|
|
79
|
-
}
|
|
80
|
-
throw err;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (process.env.NODE_ENV !== "production") {
|
|
84
|
-
logger.log("All Google Analytics request successfully replayed; " + "the queue is now empty!");
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
/**
|
|
89
|
-
* Creates GET and POST routes to catch failed Measurement Protocol hits.
|
|
90
|
-
*
|
|
91
|
-
* @param bgSyncPlugin
|
|
92
|
-
* @returns The created routes.
|
|
93
|
-
* @private
|
|
94
|
-
*/ const createCollectRoutes = (bgSyncPlugin)=>{
|
|
95
|
-
const match = ({ url })=>url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
|
96
|
-
const handler = new NetworkOnly({
|
|
97
|
-
plugins: [
|
|
98
|
-
bgSyncPlugin
|
|
99
|
-
]
|
|
100
|
-
});
|
|
101
|
-
return [
|
|
102
|
-
new Route(match, handler, "GET"),
|
|
103
|
-
new Route(match, handler, "POST")
|
|
104
|
-
];
|
|
105
|
-
};
|
|
106
|
-
/**
|
|
107
|
-
* Creates a route with a network first strategy for the analytics.js script.
|
|
108
|
-
*
|
|
109
|
-
* @param cacheName
|
|
110
|
-
* @returns The created route.
|
|
111
|
-
* @private
|
|
112
|
-
*/ const createAnalyticsJsRoute = (cacheName)=>{
|
|
113
|
-
const match = ({ url })=>url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
|
114
|
-
const handler = new NetworkFirst({
|
|
115
|
-
cacheName
|
|
116
|
-
});
|
|
117
|
-
return new Route(match, handler, "GET");
|
|
118
|
-
};
|
|
119
|
-
/**
|
|
120
|
-
* Creates a route with a network first strategy for the gtag.js script.
|
|
121
|
-
*
|
|
122
|
-
* @param cacheName
|
|
123
|
-
* @returns The created route.
|
|
124
|
-
* @private
|
|
125
|
-
*/ const createGtagJsRoute = (cacheName)=>{
|
|
126
|
-
const match = ({ url })=>url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
|
127
|
-
const handler = new NetworkFirst({
|
|
128
|
-
cacheName
|
|
129
|
-
});
|
|
130
|
-
return new Route(match, handler, "GET");
|
|
131
|
-
};
|
|
132
|
-
/**
|
|
133
|
-
* Creates a route with a network first strategy for the gtm.js script.
|
|
134
|
-
*
|
|
135
|
-
* @param cacheName
|
|
136
|
-
* @returns The created route.
|
|
137
|
-
* @private
|
|
138
|
-
*/ const createGtmJsRoute = (cacheName)=>{
|
|
139
|
-
const match = ({ url })=>url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
|
140
|
-
const handler = new NetworkFirst({
|
|
141
|
-
cacheName
|
|
142
|
-
});
|
|
143
|
-
return new Route(match, handler, "GET");
|
|
144
|
-
};
|
|
145
|
-
/**
|
|
146
|
-
* @param options
|
|
147
|
-
*/ const initialize = (options = {})=>{
|
|
148
|
-
const cacheName = privateCacheNames.getGoogleAnalyticsName(options.cacheName);
|
|
149
|
-
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
|
|
150
|
-
maxRetentionTime: MAX_RETENTION_TIME,
|
|
151
|
-
onSync: createOnSyncCallback(options)
|
|
152
|
-
});
|
|
153
|
-
const routes = [
|
|
154
|
-
createGtmJsRoute(cacheName),
|
|
155
|
-
createAnalyticsJsRoute(cacheName),
|
|
156
|
-
createGtagJsRoute(cacheName),
|
|
157
|
-
...createCollectRoutes(bgSyncPlugin)
|
|
158
|
-
];
|
|
159
|
-
const router = new Router();
|
|
160
|
-
for (const route of routes){
|
|
161
|
-
router.registerRoute(route);
|
|
162
|
-
}
|
|
163
|
-
router.addFetchListener();
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
export { initialize };
|
|
1
|
+
export { initialize } from './initialize.js';
|
|
2
|
+
import '@serwist/background-sync';
|
|
3
|
+
import '@serwist/core/internal';
|
|
4
|
+
import '@serwist/routing';
|
|
5
|
+
import '@serwist/strategies';
|
package/dist/initialize.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"initialize.d.ts","sourceRoot":"","sources":["../src/initialize.ts"],"names":[],"mappings":"AA0BA,MAAM,WAAW,gCAAgC;IAC/C;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE;QAAE,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IACrD;;;;OAIG;IACH,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,KAAK,IAAI,CAAC;CAC/C;AAyID;;GAEG;AACH,QAAA,MAAM,UAAU,aAAa,gCAAgC,KAAQ,IAgBpE,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
package/dist/initialize.js
CHANGED
|
@@ -1,65 +1,37 @@
|
|
|
1
1
|
import { BackgroundSyncPlugin } from '@serwist/background-sync';
|
|
2
|
-
import { privateCacheNames,
|
|
2
|
+
import { privateCacheNames, logger, getFriendlyURL } from '@serwist/core/internal';
|
|
3
3
|
import { Router, Route } from '@serwist/routing';
|
|
4
4
|
import { NetworkOnly, NetworkFirst } from '@serwist/strategies';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Use of this source code is governed by an MIT-style
|
|
10
|
-
license that can be found in the LICENSE file or at
|
|
11
|
-
https://opensource.org/licenses/MIT.
|
|
12
|
-
*/ const QUEUE_NAME = "serwist-google-analytics";
|
|
13
|
-
const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
|
|
6
|
+
const QUEUE_NAME = "serwist-google-analytics";
|
|
7
|
+
const MAX_RETENTION_TIME = 60 * 48;
|
|
14
8
|
const GOOGLE_ANALYTICS_HOST = "www.google-analytics.com";
|
|
15
9
|
const GTM_HOST = "www.googletagmanager.com";
|
|
16
10
|
const ANALYTICS_JS_PATH = "/analytics.js";
|
|
17
11
|
const GTAG_JS_PATH = "/gtag/js";
|
|
18
12
|
const GTM_JS_PATH = "/gtm.js";
|
|
19
|
-
// This RegExp matches all known Measurement Protocol single-hit collect
|
|
20
|
-
// endpoints. Most of the time the default path (/collect) is used, but
|
|
21
|
-
// occasionally an experimental endpoint is used when testing new features,
|
|
22
|
-
// (e.g. /r/collect or /j/collect)
|
|
23
13
|
const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
|
24
14
|
|
|
25
|
-
|
|
26
|
-
* Creates the requestWillDequeue callback to be used with the background
|
|
27
|
-
* sync plugin. The callback takes the failed request and adds the
|
|
28
|
-
* `qt` param based on the current time, as well as applies any other
|
|
29
|
-
* user-defined hit modifications.
|
|
30
|
-
*
|
|
31
|
-
* @param config
|
|
32
|
-
* @returns The requestWillDequeue callback function.
|
|
33
|
-
* @private
|
|
34
|
-
*/ const createOnSyncCallback = (config)=>{
|
|
15
|
+
const createOnSyncCallback = (config)=>{
|
|
35
16
|
return async ({ queue })=>{
|
|
36
17
|
let entry = undefined;
|
|
37
18
|
while(entry = await queue.shiftRequest()){
|
|
38
19
|
const { request, timestamp } = entry;
|
|
39
20
|
const url = new URL(request.url);
|
|
40
21
|
try {
|
|
41
|
-
// Measurement protocol requests can set their payload parameters in
|
|
42
|
-
// either the URL query string (for GET requests) or the POST body.
|
|
43
22
|
const params = request.method === "POST" ? new URLSearchParams(await request.clone().text()) : url.searchParams;
|
|
44
|
-
// Calculate the qt param, accounting for the fact that an existing
|
|
45
|
-
// qt param may be present and should be updated rather than replaced.
|
|
46
23
|
const originalHitTime = timestamp - (Number(params.get("qt")) || 0);
|
|
47
24
|
const queueTime = Date.now() - originalHitTime;
|
|
48
|
-
// Set the qt param prior to applying hitFilter or parameterOverrides.
|
|
49
25
|
params.set("qt", String(queueTime));
|
|
50
|
-
// Apply `parameterOverrides`, if set.
|
|
51
26
|
if (config.parameterOverrides) {
|
|
52
27
|
for (const param of Object.keys(config.parameterOverrides)){
|
|
53
28
|
const value = config.parameterOverrides[param];
|
|
54
29
|
params.set(param, value);
|
|
55
30
|
}
|
|
56
31
|
}
|
|
57
|
-
// Apply `hitFilter`, if set.
|
|
58
32
|
if (typeof config.hitFilter === "function") {
|
|
59
33
|
config.hitFilter.call(null, params);
|
|
60
34
|
}
|
|
61
|
-
// Retry the fetch. Ignore URL search params from the URL as they're
|
|
62
|
-
// now in the post body.
|
|
63
35
|
await fetch(new Request(url.origin + url.pathname, {
|
|
64
36
|
body: params.toString(),
|
|
65
37
|
method: "POST",
|
|
@@ -85,13 +57,7 @@ const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
|
|
85
57
|
}
|
|
86
58
|
};
|
|
87
59
|
};
|
|
88
|
-
|
|
89
|
-
* Creates GET and POST routes to catch failed Measurement Protocol hits.
|
|
90
|
-
*
|
|
91
|
-
* @param bgSyncPlugin
|
|
92
|
-
* @returns The created routes.
|
|
93
|
-
* @private
|
|
94
|
-
*/ const createCollectRoutes = (bgSyncPlugin)=>{
|
|
60
|
+
const createCollectRoutes = (bgSyncPlugin)=>{
|
|
95
61
|
const match = ({ url })=>url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
|
96
62
|
const handler = new NetworkOnly({
|
|
97
63
|
plugins: [
|
|
@@ -103,48 +69,28 @@ const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
|
|
103
69
|
new Route(match, handler, "POST")
|
|
104
70
|
];
|
|
105
71
|
};
|
|
106
|
-
|
|
107
|
-
* Creates a route with a network first strategy for the analytics.js script.
|
|
108
|
-
*
|
|
109
|
-
* @param cacheName
|
|
110
|
-
* @returns The created route.
|
|
111
|
-
* @private
|
|
112
|
-
*/ const createAnalyticsJsRoute = (cacheName)=>{
|
|
72
|
+
const createAnalyticsJsRoute = (cacheName)=>{
|
|
113
73
|
const match = ({ url })=>url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
|
114
74
|
const handler = new NetworkFirst({
|
|
115
75
|
cacheName
|
|
116
76
|
});
|
|
117
77
|
return new Route(match, handler, "GET");
|
|
118
78
|
};
|
|
119
|
-
|
|
120
|
-
* Creates a route with a network first strategy for the gtag.js script.
|
|
121
|
-
*
|
|
122
|
-
* @param cacheName
|
|
123
|
-
* @returns The created route.
|
|
124
|
-
* @private
|
|
125
|
-
*/ const createGtagJsRoute = (cacheName)=>{
|
|
79
|
+
const createGtagJsRoute = (cacheName)=>{
|
|
126
80
|
const match = ({ url })=>url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
|
127
81
|
const handler = new NetworkFirst({
|
|
128
82
|
cacheName
|
|
129
83
|
});
|
|
130
84
|
return new Route(match, handler, "GET");
|
|
131
85
|
};
|
|
132
|
-
|
|
133
|
-
* Creates a route with a network first strategy for the gtm.js script.
|
|
134
|
-
*
|
|
135
|
-
* @param cacheName
|
|
136
|
-
* @returns The created route.
|
|
137
|
-
* @private
|
|
138
|
-
*/ const createGtmJsRoute = (cacheName)=>{
|
|
86
|
+
const createGtmJsRoute = (cacheName)=>{
|
|
139
87
|
const match = ({ url })=>url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
|
140
88
|
const handler = new NetworkFirst({
|
|
141
89
|
cacheName
|
|
142
90
|
});
|
|
143
91
|
return new Route(match, handler, "GET");
|
|
144
92
|
};
|
|
145
|
-
|
|
146
|
-
* @param options
|
|
147
|
-
*/ const initialize = (options = {})=>{
|
|
93
|
+
const initialize = (options = {})=>{
|
|
148
94
|
const cacheName = privateCacheNames.getGoogleAnalyticsName(options.cacheName);
|
|
149
95
|
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
|
|
150
96
|
maxRetentionTime: MAX_RETENTION_TIME,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,UAAU,6BAA6B,CAAC;AACrD,eAAO,MAAM,kBAAkB,QAAU,CAAC;AAC1C,eAAO,MAAM,qBAAqB,6BAA6B,CAAC;AAChE,eAAO,MAAM,QAAQ,6BAA6B,CAAC;AACnD,eAAO,MAAM,iBAAiB,kBAAkB,CAAC;AACjD,eAAO,MAAM,YAAY,aAAa,CAAC;AACvC,eAAO,MAAM,WAAW,YAAY,CAAC;AACrC,eAAO,MAAM,oBAAoB,aAAa,CAAC;AAM/C,eAAO,MAAM,mBAAmB,QAAuB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/google-analytics",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0-preview.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Queues failed requests and uses the Background Sync API to replay them when the network is available",
|
|
6
6
|
"files": [
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"src",
|
|
8
|
+
"dist"
|
|
9
9
|
],
|
|
10
10
|
"keywords": [
|
|
11
11
|
"serwist",
|
|
@@ -21,41 +21,44 @@
|
|
|
21
21
|
"repository": "serwist/serwist",
|
|
22
22
|
"bugs": "https://github.com/serwist/serwist/issues",
|
|
23
23
|
"homepage": "https://serwist.pages.dev",
|
|
24
|
-
"
|
|
25
|
-
"main": "./dist/index.cjs",
|
|
24
|
+
"main": "./dist/index.js",
|
|
26
25
|
"types": "./dist/index.d.ts",
|
|
26
|
+
"typesVersions": {
|
|
27
|
+
"*": {
|
|
28
|
+
"initialize": [
|
|
29
|
+
"./dist/initialize.d.ts"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
},
|
|
27
33
|
"exports": {
|
|
28
34
|
".": {
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
"default": "./dist/index.js"
|
|
32
|
-
},
|
|
33
|
-
"require": {
|
|
34
|
-
"types": "./dist/index.d.cts",
|
|
35
|
-
"default": "./dist/index.cjs"
|
|
36
|
-
}
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
37
|
},
|
|
38
38
|
"./initialize": {
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
"default": "./dist/initialize.js"
|
|
42
|
-
},
|
|
43
|
-
"require": {
|
|
44
|
-
"types": "./dist/initialize.d.cts",
|
|
45
|
-
"default": "./dist/initialize.cjs"
|
|
46
|
-
}
|
|
39
|
+
"types": "./dist/initialize.d.ts",
|
|
40
|
+
"default": "./dist/initialize.js"
|
|
47
41
|
},
|
|
48
42
|
"./package.json": "./package.json"
|
|
49
43
|
},
|
|
50
44
|
"dependencies": {
|
|
51
|
-
"@serwist/background-sync": "
|
|
52
|
-
"@serwist/core": "
|
|
53
|
-
"@serwist/routing": "
|
|
54
|
-
"@serwist/strategies": "
|
|
45
|
+
"@serwist/background-sync": "9.0.0-preview.1",
|
|
46
|
+
"@serwist/core": "9.0.0-preview.1",
|
|
47
|
+
"@serwist/routing": "9.0.0-preview.1",
|
|
48
|
+
"@serwist/strategies": "9.0.0-preview.1"
|
|
55
49
|
},
|
|
56
50
|
"devDependencies": {
|
|
57
|
-
"rollup": "4.9.
|
|
58
|
-
"
|
|
51
|
+
"rollup": "4.9.6",
|
|
52
|
+
"typescript": "5.4.0-dev.20240203",
|
|
53
|
+
"@serwist/constants": "9.0.0-preview.1"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"typescript": ">=5.0.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"typescript": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
59
62
|
},
|
|
60
63
|
"scripts": {
|
|
61
64
|
"build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
|
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
1
9
|
import type { GoogleAnalyticsInitializeOptions } from "./initialize.js";
|
|
2
10
|
import { initialize } from "./initialize.js";
|
|
11
|
+
|
|
3
12
|
export { initialize };
|
|
4
13
|
export type { GoogleAnalyticsInitializeOptions };
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Queue, QueueEntry } from "@serwist/background-sync";
|
|
10
|
+
import { BackgroundSyncPlugin } from "@serwist/background-sync";
|
|
11
|
+
import type { RouteMatchCallbackOptions } from "@serwist/core";
|
|
12
|
+
import { getFriendlyURL, logger, privateCacheNames } from "@serwist/core/internal";
|
|
13
|
+
import { Route, Router } from "@serwist/routing";
|
|
14
|
+
import { NetworkFirst, NetworkOnly } from "@serwist/strategies";
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
ANALYTICS_JS_PATH,
|
|
18
|
+
COLLECT_PATHS_REGEX,
|
|
19
|
+
GOOGLE_ANALYTICS_HOST,
|
|
20
|
+
GTAG_JS_PATH,
|
|
21
|
+
GTM_HOST,
|
|
22
|
+
GTM_JS_PATH,
|
|
23
|
+
MAX_RETENTION_TIME,
|
|
24
|
+
QUEUE_NAME,
|
|
25
|
+
} from "./utils/constants.js";
|
|
26
|
+
|
|
27
|
+
export interface GoogleAnalyticsInitializeOptions {
|
|
28
|
+
/**
|
|
29
|
+
* The cache name to store and retrieve analytics.js. Defaults to the cache names provided by `@serwist/core`.
|
|
30
|
+
*/
|
|
31
|
+
cacheName?: string;
|
|
32
|
+
/**
|
|
33
|
+
* [Measurement Protocol parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters),
|
|
34
|
+
* expressed as key/value pairs, to be added to replayed Google Analytics
|
|
35
|
+
* requests. This can be used to, e.g., set a custom dimension indicating
|
|
36
|
+
* that the request was replayed.
|
|
37
|
+
*/
|
|
38
|
+
parameterOverrides?: { [paramName: string]: string };
|
|
39
|
+
/**
|
|
40
|
+
* A function that allows you to modify the hit parameters prior to replaying
|
|
41
|
+
* the hit. The function is invoked with the original hit's URLSearchParams
|
|
42
|
+
* object as its only argument.
|
|
43
|
+
*/
|
|
44
|
+
hitFilter?: (params: URLSearchParams) => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Creates the requestWillDequeue callback to be used with the background
|
|
49
|
+
* sync plugin. The callback takes the failed request and adds the
|
|
50
|
+
* `qt` param based on the current time, as well as applies any other
|
|
51
|
+
* user-defined hit modifications.
|
|
52
|
+
*
|
|
53
|
+
* @param config
|
|
54
|
+
* @returns The requestWillDequeue callback function.
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
const createOnSyncCallback = (config: GoogleAnalyticsInitializeOptions) => {
|
|
58
|
+
return async ({ queue }: { queue: Queue }) => {
|
|
59
|
+
let entry: QueueEntry | undefined = undefined;
|
|
60
|
+
while ((entry = await queue.shiftRequest())) {
|
|
61
|
+
const { request, timestamp } = entry;
|
|
62
|
+
const url = new URL(request.url);
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
// Measurement protocol requests can set their payload parameters in
|
|
66
|
+
// either the URL query string (for GET requests) or the POST body.
|
|
67
|
+
const params = request.method === "POST" ? new URLSearchParams(await request.clone().text()) : url.searchParams;
|
|
68
|
+
|
|
69
|
+
// Calculate the qt param, accounting for the fact that an existing
|
|
70
|
+
// qt param may be present and should be updated rather than replaced.
|
|
71
|
+
const originalHitTime = timestamp! - (Number(params.get("qt")) || 0);
|
|
72
|
+
const queueTime = Date.now() - originalHitTime;
|
|
73
|
+
|
|
74
|
+
// Set the qt param prior to applying hitFilter or parameterOverrides.
|
|
75
|
+
params.set("qt", String(queueTime));
|
|
76
|
+
|
|
77
|
+
// Apply `parameterOverrides`, if set.
|
|
78
|
+
if (config.parameterOverrides) {
|
|
79
|
+
for (const param of Object.keys(config.parameterOverrides)) {
|
|
80
|
+
const value = config.parameterOverrides[param];
|
|
81
|
+
params.set(param, value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Apply `hitFilter`, if set.
|
|
86
|
+
if (typeof config.hitFilter === "function") {
|
|
87
|
+
config.hitFilter.call(null, params);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Retry the fetch. Ignore URL search params from the URL as they're
|
|
91
|
+
// now in the post body.
|
|
92
|
+
await fetch(
|
|
93
|
+
new Request(url.origin + url.pathname, {
|
|
94
|
+
body: params.toString(),
|
|
95
|
+
method: "POST",
|
|
96
|
+
mode: "cors",
|
|
97
|
+
credentials: "omit",
|
|
98
|
+
headers: { "Content-Type": "text/plain" },
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
if (process.env.NODE_ENV !== "production") {
|
|
103
|
+
logger.log(`Request for '${getFriendlyURL(url.href)}' has been replayed`);
|
|
104
|
+
}
|
|
105
|
+
} catch (err) {
|
|
106
|
+
await queue.unshiftRequest(entry);
|
|
107
|
+
|
|
108
|
+
if (process.env.NODE_ENV !== "production") {
|
|
109
|
+
logger.log(`Request for '${getFriendlyURL(url.href)}' failed to replay, putting it back in the queue.`);
|
|
110
|
+
}
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (process.env.NODE_ENV !== "production") {
|
|
115
|
+
logger.log("All Google Analytics request successfully replayed; " + "the queue is now empty!");
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Creates GET and POST routes to catch failed Measurement Protocol hits.
|
|
122
|
+
*
|
|
123
|
+
* @param bgSyncPlugin
|
|
124
|
+
* @returns The created routes.
|
|
125
|
+
* @private
|
|
126
|
+
*/
|
|
127
|
+
const createCollectRoutes = (bgSyncPlugin: BackgroundSyncPlugin) => {
|
|
128
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
|
129
|
+
|
|
130
|
+
const handler = new NetworkOnly({
|
|
131
|
+
plugins: [bgSyncPlugin],
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
return [new Route(match, handler, "GET"), new Route(match, handler, "POST")];
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Creates a route with a network first strategy for the analytics.js script.
|
|
139
|
+
*
|
|
140
|
+
* @param cacheName
|
|
141
|
+
* @returns The created route.
|
|
142
|
+
* @private
|
|
143
|
+
*/
|
|
144
|
+
const createAnalyticsJsRoute = (cacheName: string) => {
|
|
145
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
|
146
|
+
|
|
147
|
+
const handler = new NetworkFirst({ cacheName });
|
|
148
|
+
|
|
149
|
+
return new Route(match, handler, "GET");
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Creates a route with a network first strategy for the gtag.js script.
|
|
154
|
+
*
|
|
155
|
+
* @param cacheName
|
|
156
|
+
* @returns The created route.
|
|
157
|
+
* @private
|
|
158
|
+
*/
|
|
159
|
+
const createGtagJsRoute = (cacheName: string) => {
|
|
160
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
|
161
|
+
|
|
162
|
+
const handler = new NetworkFirst({ cacheName });
|
|
163
|
+
|
|
164
|
+
return new Route(match, handler, "GET");
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Creates a route with a network first strategy for the gtm.js script.
|
|
169
|
+
*
|
|
170
|
+
* @param cacheName
|
|
171
|
+
* @returns The created route.
|
|
172
|
+
* @private
|
|
173
|
+
*/
|
|
174
|
+
const createGtmJsRoute = (cacheName: string) => {
|
|
175
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
|
176
|
+
|
|
177
|
+
const handler = new NetworkFirst({ cacheName });
|
|
178
|
+
|
|
179
|
+
return new Route(match, handler, "GET");
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* @param options
|
|
184
|
+
*/
|
|
185
|
+
const initialize = (options: GoogleAnalyticsInitializeOptions = {}): void => {
|
|
186
|
+
const cacheName = privateCacheNames.getGoogleAnalyticsName(options.cacheName);
|
|
187
|
+
|
|
188
|
+
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
|
|
189
|
+
maxRetentionTime: MAX_RETENTION_TIME,
|
|
190
|
+
onSync: createOnSyncCallback(options),
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const routes = [createGtmJsRoute(cacheName), createAnalyticsJsRoute(cacheName), createGtagJsRoute(cacheName), ...createCollectRoutes(bgSyncPlugin)];
|
|
194
|
+
|
|
195
|
+
const router = new Router();
|
|
196
|
+
for (const route of routes) {
|
|
197
|
+
router.registerRoute(route);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
router.addFetchListener();
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
export { initialize };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const QUEUE_NAME = "serwist-google-analytics";
|
|
10
|
+
export const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
|
|
11
|
+
export const GOOGLE_ANALYTICS_HOST = "www.google-analytics.com";
|
|
12
|
+
export const GTM_HOST = "www.googletagmanager.com";
|
|
13
|
+
export const ANALYTICS_JS_PATH = "/analytics.js";
|
|
14
|
+
export const GTAG_JS_PATH = "/gtag/js";
|
|
15
|
+
export const GTM_JS_PATH = "/gtm.js";
|
|
16
|
+
export const COLLECT_DEFAULT_PATH = "/collect";
|
|
17
|
+
|
|
18
|
+
// This RegExp matches all known Measurement Protocol single-hit collect
|
|
19
|
+
// endpoints. Most of the time the default path (/collect) is used, but
|
|
20
|
+
// occasionally an experimental endpoint is used when testing new features,
|
|
21
|
+
// (e.g. /r/collect or /j/collect)
|
|
22
|
+
export const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
package/dist/index.cjs
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var backgroundSync = require('@serwist/background-sync');
|
|
4
|
-
var internal = require('@serwist/core/internal');
|
|
5
|
-
var routing = require('@serwist/routing');
|
|
6
|
-
var strategies = require('@serwist/strategies');
|
|
7
|
-
|
|
8
|
-
/*
|
|
9
|
-
Copyright 2018 Google LLC
|
|
10
|
-
|
|
11
|
-
Use of this source code is governed by an MIT-style
|
|
12
|
-
license that can be found in the LICENSE file or at
|
|
13
|
-
https://opensource.org/licenses/MIT.
|
|
14
|
-
*/ const QUEUE_NAME = "serwist-google-analytics";
|
|
15
|
-
const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
|
|
16
|
-
const GOOGLE_ANALYTICS_HOST = "www.google-analytics.com";
|
|
17
|
-
const GTM_HOST = "www.googletagmanager.com";
|
|
18
|
-
const ANALYTICS_JS_PATH = "/analytics.js";
|
|
19
|
-
const GTAG_JS_PATH = "/gtag/js";
|
|
20
|
-
const GTM_JS_PATH = "/gtm.js";
|
|
21
|
-
// This RegExp matches all known Measurement Protocol single-hit collect
|
|
22
|
-
// endpoints. Most of the time the default path (/collect) is used, but
|
|
23
|
-
// occasionally an experimental endpoint is used when testing new features,
|
|
24
|
-
// (e.g. /r/collect or /j/collect)
|
|
25
|
-
const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Creates the requestWillDequeue callback to be used with the background
|
|
29
|
-
* sync plugin. The callback takes the failed request and adds the
|
|
30
|
-
* `qt` param based on the current time, as well as applies any other
|
|
31
|
-
* user-defined hit modifications.
|
|
32
|
-
*
|
|
33
|
-
* @param config
|
|
34
|
-
* @returns The requestWillDequeue callback function.
|
|
35
|
-
* @private
|
|
36
|
-
*/ const createOnSyncCallback = (config)=>{
|
|
37
|
-
return async ({ queue })=>{
|
|
38
|
-
let entry = undefined;
|
|
39
|
-
while(entry = await queue.shiftRequest()){
|
|
40
|
-
const { request, timestamp } = entry;
|
|
41
|
-
const url = new URL(request.url);
|
|
42
|
-
try {
|
|
43
|
-
// Measurement protocol requests can set their payload parameters in
|
|
44
|
-
// either the URL query string (for GET requests) or the POST body.
|
|
45
|
-
const params = request.method === "POST" ? new URLSearchParams(await request.clone().text()) : url.searchParams;
|
|
46
|
-
// Calculate the qt param, accounting for the fact that an existing
|
|
47
|
-
// qt param may be present and should be updated rather than replaced.
|
|
48
|
-
const originalHitTime = timestamp - (Number(params.get("qt")) || 0);
|
|
49
|
-
const queueTime = Date.now() - originalHitTime;
|
|
50
|
-
// Set the qt param prior to applying hitFilter or parameterOverrides.
|
|
51
|
-
params.set("qt", String(queueTime));
|
|
52
|
-
// Apply `parameterOverrides`, if set.
|
|
53
|
-
if (config.parameterOverrides) {
|
|
54
|
-
for (const param of Object.keys(config.parameterOverrides)){
|
|
55
|
-
const value = config.parameterOverrides[param];
|
|
56
|
-
params.set(param, value);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
// Apply `hitFilter`, if set.
|
|
60
|
-
if (typeof config.hitFilter === "function") {
|
|
61
|
-
config.hitFilter.call(null, params);
|
|
62
|
-
}
|
|
63
|
-
// Retry the fetch. Ignore URL search params from the URL as they're
|
|
64
|
-
// now in the post body.
|
|
65
|
-
await fetch(new Request(url.origin + url.pathname, {
|
|
66
|
-
body: params.toString(),
|
|
67
|
-
method: "POST",
|
|
68
|
-
mode: "cors",
|
|
69
|
-
credentials: "omit",
|
|
70
|
-
headers: {
|
|
71
|
-
"Content-Type": "text/plain"
|
|
72
|
-
}
|
|
73
|
-
}));
|
|
74
|
-
if (process.env.NODE_ENV !== "production") {
|
|
75
|
-
internal.logger.log(`Request for '${internal.getFriendlyURL(url.href)}' has been replayed`);
|
|
76
|
-
}
|
|
77
|
-
} catch (err) {
|
|
78
|
-
await queue.unshiftRequest(entry);
|
|
79
|
-
if (process.env.NODE_ENV !== "production") {
|
|
80
|
-
internal.logger.log(`Request for '${internal.getFriendlyURL(url.href)}' failed to replay, putting it back in the queue.`);
|
|
81
|
-
}
|
|
82
|
-
throw err;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
if (process.env.NODE_ENV !== "production") {
|
|
86
|
-
internal.logger.log("All Google Analytics request successfully replayed; " + "the queue is now empty!");
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
/**
|
|
91
|
-
* Creates GET and POST routes to catch failed Measurement Protocol hits.
|
|
92
|
-
*
|
|
93
|
-
* @param bgSyncPlugin
|
|
94
|
-
* @returns The created routes.
|
|
95
|
-
* @private
|
|
96
|
-
*/ const createCollectRoutes = (bgSyncPlugin)=>{
|
|
97
|
-
const match = ({ url })=>url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
|
98
|
-
const handler = new strategies.NetworkOnly({
|
|
99
|
-
plugins: [
|
|
100
|
-
bgSyncPlugin
|
|
101
|
-
]
|
|
102
|
-
});
|
|
103
|
-
return [
|
|
104
|
-
new routing.Route(match, handler, "GET"),
|
|
105
|
-
new routing.Route(match, handler, "POST")
|
|
106
|
-
];
|
|
107
|
-
};
|
|
108
|
-
/**
|
|
109
|
-
* Creates a route with a network first strategy for the analytics.js script.
|
|
110
|
-
*
|
|
111
|
-
* @param cacheName
|
|
112
|
-
* @returns The created route.
|
|
113
|
-
* @private
|
|
114
|
-
*/ const createAnalyticsJsRoute = (cacheName)=>{
|
|
115
|
-
const match = ({ url })=>url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
|
116
|
-
const handler = new strategies.NetworkFirst({
|
|
117
|
-
cacheName
|
|
118
|
-
});
|
|
119
|
-
return new routing.Route(match, handler, "GET");
|
|
120
|
-
};
|
|
121
|
-
/**
|
|
122
|
-
* Creates a route with a network first strategy for the gtag.js script.
|
|
123
|
-
*
|
|
124
|
-
* @param cacheName
|
|
125
|
-
* @returns The created route.
|
|
126
|
-
* @private
|
|
127
|
-
*/ const createGtagJsRoute = (cacheName)=>{
|
|
128
|
-
const match = ({ url })=>url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
|
129
|
-
const handler = new strategies.NetworkFirst({
|
|
130
|
-
cacheName
|
|
131
|
-
});
|
|
132
|
-
return new routing.Route(match, handler, "GET");
|
|
133
|
-
};
|
|
134
|
-
/**
|
|
135
|
-
* Creates a route with a network first strategy for the gtm.js script.
|
|
136
|
-
*
|
|
137
|
-
* @param cacheName
|
|
138
|
-
* @returns The created route.
|
|
139
|
-
* @private
|
|
140
|
-
*/ const createGtmJsRoute = (cacheName)=>{
|
|
141
|
-
const match = ({ url })=>url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
|
142
|
-
const handler = new strategies.NetworkFirst({
|
|
143
|
-
cacheName
|
|
144
|
-
});
|
|
145
|
-
return new routing.Route(match, handler, "GET");
|
|
146
|
-
};
|
|
147
|
-
/**
|
|
148
|
-
* @param options
|
|
149
|
-
*/ const initialize = (options = {})=>{
|
|
150
|
-
const cacheName = internal.privateCacheNames.getGoogleAnalyticsName(options.cacheName);
|
|
151
|
-
const bgSyncPlugin = new backgroundSync.BackgroundSyncPlugin(QUEUE_NAME, {
|
|
152
|
-
maxRetentionTime: MAX_RETENTION_TIME,
|
|
153
|
-
onSync: createOnSyncCallback(options)
|
|
154
|
-
});
|
|
155
|
-
const routes = [
|
|
156
|
-
createGtmJsRoute(cacheName),
|
|
157
|
-
createAnalyticsJsRoute(cacheName),
|
|
158
|
-
createGtagJsRoute(cacheName),
|
|
159
|
-
...createCollectRoutes(bgSyncPlugin)
|
|
160
|
-
];
|
|
161
|
-
const router = new routing.Router();
|
|
162
|
-
for (const route of routes){
|
|
163
|
-
router.registerRoute(route);
|
|
164
|
-
}
|
|
165
|
-
router.addFetchListener();
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
exports.initialize = initialize;
|
package/dist/initialize.cjs
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var backgroundSync = require('@serwist/background-sync');
|
|
4
|
-
var internal = require('@serwist/core/internal');
|
|
5
|
-
var routing = require('@serwist/routing');
|
|
6
|
-
var strategies = require('@serwist/strategies');
|
|
7
|
-
|
|
8
|
-
/*
|
|
9
|
-
Copyright 2018 Google LLC
|
|
10
|
-
|
|
11
|
-
Use of this source code is governed by an MIT-style
|
|
12
|
-
license that can be found in the LICENSE file or at
|
|
13
|
-
https://opensource.org/licenses/MIT.
|
|
14
|
-
*/ const QUEUE_NAME = "serwist-google-analytics";
|
|
15
|
-
const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
|
|
16
|
-
const GOOGLE_ANALYTICS_HOST = "www.google-analytics.com";
|
|
17
|
-
const GTM_HOST = "www.googletagmanager.com";
|
|
18
|
-
const ANALYTICS_JS_PATH = "/analytics.js";
|
|
19
|
-
const GTAG_JS_PATH = "/gtag/js";
|
|
20
|
-
const GTM_JS_PATH = "/gtm.js";
|
|
21
|
-
// This RegExp matches all known Measurement Protocol single-hit collect
|
|
22
|
-
// endpoints. Most of the time the default path (/collect) is used, but
|
|
23
|
-
// occasionally an experimental endpoint is used when testing new features,
|
|
24
|
-
// (e.g. /r/collect or /j/collect)
|
|
25
|
-
const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Creates the requestWillDequeue callback to be used with the background
|
|
29
|
-
* sync plugin. The callback takes the failed request and adds the
|
|
30
|
-
* `qt` param based on the current time, as well as applies any other
|
|
31
|
-
* user-defined hit modifications.
|
|
32
|
-
*
|
|
33
|
-
* @param config
|
|
34
|
-
* @returns The requestWillDequeue callback function.
|
|
35
|
-
* @private
|
|
36
|
-
*/ const createOnSyncCallback = (config)=>{
|
|
37
|
-
return async ({ queue })=>{
|
|
38
|
-
let entry = undefined;
|
|
39
|
-
while(entry = await queue.shiftRequest()){
|
|
40
|
-
const { request, timestamp } = entry;
|
|
41
|
-
const url = new URL(request.url);
|
|
42
|
-
try {
|
|
43
|
-
// Measurement protocol requests can set their payload parameters in
|
|
44
|
-
// either the URL query string (for GET requests) or the POST body.
|
|
45
|
-
const params = request.method === "POST" ? new URLSearchParams(await request.clone().text()) : url.searchParams;
|
|
46
|
-
// Calculate the qt param, accounting for the fact that an existing
|
|
47
|
-
// qt param may be present and should be updated rather than replaced.
|
|
48
|
-
const originalHitTime = timestamp - (Number(params.get("qt")) || 0);
|
|
49
|
-
const queueTime = Date.now() - originalHitTime;
|
|
50
|
-
// Set the qt param prior to applying hitFilter or parameterOverrides.
|
|
51
|
-
params.set("qt", String(queueTime));
|
|
52
|
-
// Apply `parameterOverrides`, if set.
|
|
53
|
-
if (config.parameterOverrides) {
|
|
54
|
-
for (const param of Object.keys(config.parameterOverrides)){
|
|
55
|
-
const value = config.parameterOverrides[param];
|
|
56
|
-
params.set(param, value);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
// Apply `hitFilter`, if set.
|
|
60
|
-
if (typeof config.hitFilter === "function") {
|
|
61
|
-
config.hitFilter.call(null, params);
|
|
62
|
-
}
|
|
63
|
-
// Retry the fetch. Ignore URL search params from the URL as they're
|
|
64
|
-
// now in the post body.
|
|
65
|
-
await fetch(new Request(url.origin + url.pathname, {
|
|
66
|
-
body: params.toString(),
|
|
67
|
-
method: "POST",
|
|
68
|
-
mode: "cors",
|
|
69
|
-
credentials: "omit",
|
|
70
|
-
headers: {
|
|
71
|
-
"Content-Type": "text/plain"
|
|
72
|
-
}
|
|
73
|
-
}));
|
|
74
|
-
if (process.env.NODE_ENV !== "production") {
|
|
75
|
-
internal.logger.log(`Request for '${internal.getFriendlyURL(url.href)}' has been replayed`);
|
|
76
|
-
}
|
|
77
|
-
} catch (err) {
|
|
78
|
-
await queue.unshiftRequest(entry);
|
|
79
|
-
if (process.env.NODE_ENV !== "production") {
|
|
80
|
-
internal.logger.log(`Request for '${internal.getFriendlyURL(url.href)}' failed to replay, putting it back in the queue.`);
|
|
81
|
-
}
|
|
82
|
-
throw err;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
if (process.env.NODE_ENV !== "production") {
|
|
86
|
-
internal.logger.log("All Google Analytics request successfully replayed; " + "the queue is now empty!");
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
/**
|
|
91
|
-
* Creates GET and POST routes to catch failed Measurement Protocol hits.
|
|
92
|
-
*
|
|
93
|
-
* @param bgSyncPlugin
|
|
94
|
-
* @returns The created routes.
|
|
95
|
-
* @private
|
|
96
|
-
*/ const createCollectRoutes = (bgSyncPlugin)=>{
|
|
97
|
-
const match = ({ url })=>url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
|
98
|
-
const handler = new strategies.NetworkOnly({
|
|
99
|
-
plugins: [
|
|
100
|
-
bgSyncPlugin
|
|
101
|
-
]
|
|
102
|
-
});
|
|
103
|
-
return [
|
|
104
|
-
new routing.Route(match, handler, "GET"),
|
|
105
|
-
new routing.Route(match, handler, "POST")
|
|
106
|
-
];
|
|
107
|
-
};
|
|
108
|
-
/**
|
|
109
|
-
* Creates a route with a network first strategy for the analytics.js script.
|
|
110
|
-
*
|
|
111
|
-
* @param cacheName
|
|
112
|
-
* @returns The created route.
|
|
113
|
-
* @private
|
|
114
|
-
*/ const createAnalyticsJsRoute = (cacheName)=>{
|
|
115
|
-
const match = ({ url })=>url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
|
116
|
-
const handler = new strategies.NetworkFirst({
|
|
117
|
-
cacheName
|
|
118
|
-
});
|
|
119
|
-
return new routing.Route(match, handler, "GET");
|
|
120
|
-
};
|
|
121
|
-
/**
|
|
122
|
-
* Creates a route with a network first strategy for the gtag.js script.
|
|
123
|
-
*
|
|
124
|
-
* @param cacheName
|
|
125
|
-
* @returns The created route.
|
|
126
|
-
* @private
|
|
127
|
-
*/ const createGtagJsRoute = (cacheName)=>{
|
|
128
|
-
const match = ({ url })=>url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
|
129
|
-
const handler = new strategies.NetworkFirst({
|
|
130
|
-
cacheName
|
|
131
|
-
});
|
|
132
|
-
return new routing.Route(match, handler, "GET");
|
|
133
|
-
};
|
|
134
|
-
/**
|
|
135
|
-
* Creates a route with a network first strategy for the gtm.js script.
|
|
136
|
-
*
|
|
137
|
-
* @param cacheName
|
|
138
|
-
* @returns The created route.
|
|
139
|
-
* @private
|
|
140
|
-
*/ const createGtmJsRoute = (cacheName)=>{
|
|
141
|
-
const match = ({ url })=>url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
|
142
|
-
const handler = new strategies.NetworkFirst({
|
|
143
|
-
cacheName
|
|
144
|
-
});
|
|
145
|
-
return new routing.Route(match, handler, "GET");
|
|
146
|
-
};
|
|
147
|
-
/**
|
|
148
|
-
* @param options
|
|
149
|
-
*/ const initialize = (options = {})=>{
|
|
150
|
-
const cacheName = internal.privateCacheNames.getGoogleAnalyticsName(options.cacheName);
|
|
151
|
-
const bgSyncPlugin = new backgroundSync.BackgroundSyncPlugin(QUEUE_NAME, {
|
|
152
|
-
maxRetentionTime: MAX_RETENTION_TIME,
|
|
153
|
-
onSync: createOnSyncCallback(options)
|
|
154
|
-
});
|
|
155
|
-
const routes = [
|
|
156
|
-
createGtmJsRoute(cacheName),
|
|
157
|
-
createAnalyticsJsRoute(cacheName),
|
|
158
|
-
createGtagJsRoute(cacheName),
|
|
159
|
-
...createCollectRoutes(bgSyncPlugin)
|
|
160
|
-
];
|
|
161
|
-
const router = new routing.Router();
|
|
162
|
-
for (const route of routes){
|
|
163
|
-
router.registerRoute(route);
|
|
164
|
-
}
|
|
165
|
-
router.addFetchListener();
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
exports.initialize = initialize;
|
package/dist/initialize.d.cts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export interface GoogleAnalyticsInitializeOptions {
|
|
2
|
-
/**
|
|
3
|
-
* The cache name to store and retrieve analytics.js. Defaults to the cache names provided by `@serwist/core`.
|
|
4
|
-
*/
|
|
5
|
-
cacheName?: string;
|
|
6
|
-
/**
|
|
7
|
-
* [Measurement Protocol parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters),
|
|
8
|
-
* expressed as key/value pairs, to be added to replayed Google Analytics
|
|
9
|
-
* requests. This can be used to, e.g., set a custom dimension indicating
|
|
10
|
-
* that the request was replayed.
|
|
11
|
-
*/
|
|
12
|
-
parameterOverrides?: {
|
|
13
|
-
[paramName: string]: string;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* A function that allows you to modify the hit parameters prior to replaying
|
|
17
|
-
* the hit. The function is invoked with the original hit's URLSearchParams
|
|
18
|
-
* object as its only argument.
|
|
19
|
-
*/
|
|
20
|
-
hitFilter?: (params: URLSearchParams) => void;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* @param options
|
|
24
|
-
*/
|
|
25
|
-
declare const initialize: (options?: GoogleAnalyticsInitializeOptions) => void;
|
|
26
|
-
export { initialize };
|