@serwist/google-analytics 9.0.0-preview.0 → 9.0.0-preview.2

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.
Files changed (2) hide show
  1. package/dist/initialize.js +8 -62
  2. package/package.json +7 -7
@@ -3,63 +3,35 @@ import { privateCacheNames, logger, getFriendlyURL } from '@serwist/core/interna
3
3
  import { Router, Route } from '@serwist/routing';
4
4
  import { NetworkOnly, NetworkFirst } from '@serwist/strategies';
5
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
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/google-analytics",
3
- "version": "9.0.0-preview.0",
3
+ "version": "9.0.0-preview.2",
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": [
@@ -42,15 +42,15 @@
42
42
  "./package.json": "./package.json"
43
43
  },
44
44
  "dependencies": {
45
- "@serwist/background-sync": "9.0.0-preview.0",
46
- "@serwist/core": "9.0.0-preview.0",
47
- "@serwist/routing": "9.0.0-preview.0",
48
- "@serwist/strategies": "9.0.0-preview.0"
45
+ "@serwist/background-sync": "9.0.0-preview.2",
46
+ "@serwist/core": "9.0.0-preview.2",
47
+ "@serwist/routing": "9.0.0-preview.2",
48
+ "@serwist/strategies": "9.0.0-preview.2"
49
49
  },
50
50
  "devDependencies": {
51
51
  "rollup": "4.9.6",
52
- "typescript": "5.4.0-dev.20240203",
53
- "@serwist/constants": "9.0.0-preview.0"
52
+ "typescript": "5.4.0-dev.20240206",
53
+ "@serwist/constants": "9.0.0-preview.2"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "typescript": ">=5.0.0"