@adobe/spacecat-shared-rum-api-client 2.8.0 → 2.9.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-rum-api-client-v2.9.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.9.0...@adobe/spacecat-shared-rum-api-client-v2.9.1) (2024-09-14)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **deps:** update external fixes ([#369](https://github.com/adobe/spacecat-shared/issues/369)) ([5412d7b](https://github.com/adobe/spacecat-shared/commit/5412d7be554b9940d43b39b18f2913146e866846))
7
+
8
+ # [@adobe/spacecat-shared-rum-api-client-v2.9.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.8.0...@adobe/spacecat-shared-rum-api-client-v2.9.0) (2024-09-03)
9
+
10
+
11
+ ### Features
12
+
13
+ * experimentation opportunities ([#351](https://github.com/adobe/spacecat-shared/issues/351)) ([f21a765](https://github.com/adobe/spacecat-shared/commit/f21a765996990b1748c11b0cd8089e87ec91d9ad))
14
+
1
15
  # [@adobe/spacecat-shared-rum-api-client-v2.8.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.7.4...@adobe/spacecat-shared-rum-api-client-v2.8.0) (2024-08-26)
2
16
 
3
17
 
package/README.md CHANGED
@@ -191,6 +191,92 @@ An example response:
191
191
 
192
192
  ```
193
193
 
194
+ ### high-inorganic-high-bounce-rate (Experimentation Opportunity)
195
+
196
+ Calculates the amount of inorganic traffic and the bounce rate for each page. Identifies pages with both high inorganic traffic and high bounce rates, which can be targeted for future experimentation opportunities. An example payload is provided below:
197
+
198
+ ```json
199
+ [
200
+ {
201
+ "type": "high-inorganic-high-bounce-rate",
202
+ "page": "https://www.spacecat.com/",
203
+ "screenshot": "",
204
+ "trackedPageKPIName": "Bounce Rate",
205
+ "trackedPageKPIValue": 0.6507592190889371,
206
+ "pageViews": 46100,
207
+ "samples": 46100,
208
+ "metrics": [
209
+ {
210
+ "type": "traffic",
211
+ "value": {
212
+ "total": 46100,
213
+ "paid": 40700,
214
+ "owned": 5400,
215
+ "earned": 0
216
+ }
217
+ }
218
+ ]
219
+ },
220
+ {
221
+ "type": "high-inorganic-high-bounce-rate",
222
+ "page": "https://www.spacecat.com/pricing",
223
+ "screenshot": "",
224
+ "trackedPageKPIName": "Bounce Rate",
225
+ "trackedPageKPIValue": 0.8723897911832946,
226
+ "pageViews": 43100,
227
+ "samples": 43100,
228
+ "metrics": [
229
+ {
230
+ "type": "traffic",
231
+ "value": {
232
+ "total": 43100,
233
+ "paid": 24100,
234
+ "owned": 19000,
235
+ "earned": 0
236
+ }
237
+ }
238
+ ]
239
+ }
240
+ ]
241
+ ```
242
+
243
+ ### high-organic-low-ctr (Experimentation Opportunity)
244
+
245
+ Calculates the amount of non-inorganic (earned and owned) traffic and the click-through rate for each page. Identifies pages with high non-inorganic traffic and low click-through rates, which can be targeted for future experimentation opportunities. An example payload is provided below:
246
+
247
+ ```json
248
+ [
249
+ {
250
+ "type": "high-organic-low-ctr",
251
+ "page": "https://www.spacecat.com/about-us",
252
+ "screenshot": "",
253
+ "trackedPageKPIName": "Click Through Rate",
254
+ "trackedPageKPIValue": 0.14099783080260303,
255
+ "pageViews": 46100,
256
+ "samples": 46100,
257
+ "metrics": [
258
+ {
259
+ "type": "traffic",
260
+ "value": {
261
+ "total": 46100,
262
+ "paid": 0,
263
+ "owned": 46100,
264
+ "earned": 0
265
+ }
266
+ },
267
+ {
268
+ "type": "ctr",
269
+ "value": {
270
+ "page": 0.14099783080260303,
271
+ "siteAverage": 0.4077909270216962
272
+ }
273
+ }
274
+ ]
275
+ }
276
+ ]
277
+
278
+ ```
279
+
194
280
  ## Linting
195
281
  Lint the codebase using:
196
282
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-rum-api-client",
3
- "version": "2.8.0",
3
+ "version": "2.9.1",
4
4
  "description": "Shared modules of the Spacecat Services - Rum API client",
5
5
  "type": "module",
6
6
  "engines": {
@@ -39,7 +39,7 @@
39
39
  "@adobe/helix-shared-wrap": "2.0.2",
40
40
  "@adobe/helix-universal": "5.0.5",
41
41
  "@adobe/spacecat-shared-utils": "1.4.0",
42
- "aws4": "1.13.1",
42
+ "aws4": "1.13.2",
43
43
  "d3-array": "3.2.4",
44
44
  "urijs": "^1.19.11"
45
45
  },
@@ -47,8 +47,8 @@
47
47
  "chai": "5.1.1",
48
48
  "chai-as-promised": "8.0.0",
49
49
  "nock": "13.5.5",
50
- "sinon": "18.0.0",
50
+ "sinon": "18.0.1",
51
51
  "sinon-chai": "4.0.0",
52
- "typescript": "5.5.4"
52
+ "typescript": "5.6.2"
53
53
  }
54
54
  }
@@ -23,6 +23,60 @@ function pageviewsByUrl(bundles) {
23
23
  }, {});
24
24
  }
25
25
 
26
+ /**
27
+ * Calculates the Click-Through Rate (CTR) by URL.
28
+ * CTR is defined as the total number of sessions with at least one click event
29
+ * divided by the total number of pageviews for each URL.
30
+ *
31
+ * @param {Array<Object>} bundles - An array of RUM bundles (NOT Flat bundles).
32
+ * @returns {Object} - An object where the key is the URL and the value is the CTR value.
33
+ */
34
+ function getCTRByUrl(bundles) {
35
+ const aggregated = bundles.reduce((acc, bundle) => {
36
+ const { url } = bundle;
37
+ if (!acc[url]) {
38
+ acc[url] = { sessionsWithClick: 0, totalPageviews: 0 };
39
+ }
40
+ const hasClick = bundle.events.some((event) => event.checkpoint === 'click');
41
+
42
+ acc[url].totalPageviews += bundle.weight;
43
+ if (hasClick) {
44
+ acc[url].sessionsWithClick += bundle.weight;
45
+ }
46
+ return acc;
47
+ }, {});
48
+ return Object.entries(aggregated)
49
+ .reduce((acc, [url, { sessionsWithClick, totalPageviews }]) => {
50
+ acc[url] = (sessionsWithClick / totalPageviews);
51
+ return acc;
52
+ }, {});
53
+ }
54
+
55
+ /**
56
+ * Calculates the Click-Through Rate (CTR) average for the entire site.
57
+ * CTR is defined as the total number of sessions with at least one click event
58
+ * divided by the total number of pageviews for the entire site.
59
+ *
60
+ * @param {Array<Object>} bundles - An array of RUM bundles (NOT Flat bundles).
61
+ * @returns {number} - Average CTR for the site.
62
+ */
63
+ function getSiteAvgCTR(bundles) {
64
+ const aggregated = bundles.reduce((acc, bundle) => {
65
+ const hasClick = bundle.events.some((event) => event.checkpoint === 'click');
66
+ acc.totalPageviews += bundle.weight;
67
+ if (hasClick) {
68
+ acc.sessionsWithClick += bundle.weight;
69
+ }
70
+ return acc;
71
+ }, { sessionsWithClick: 0, totalPageviews: 0 });
72
+
73
+ return aggregated.totalPageviews === 0
74
+ ? 0
75
+ : aggregated.sessionsWithClick / aggregated.totalPageviews;
76
+ }
77
+
26
78
  export {
79
+ getSiteAvgCTR,
80
+ getCTRByUrl,
27
81
  pageviewsByUrl,
28
82
  };
@@ -0,0 +1,74 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import trafficAcquisition from '../traffic-acquisition.js';
14
+ import { getCTRByUrl } from '../../common/aggregateFns.js';
15
+
16
+ const HOMEPAGE_PAID_TRAFFIC_THRESHOLD = 0.8;
17
+ const NON_HOMEPAGE_PAID_TRAFFIC_THRESHOLD = 0.5;
18
+ const BOUNCE_RATE_THRESHOLD = 0.5;
19
+ const DAILY_PAGEVIEW_THRESHOLD = 1000;
20
+
21
+ function convertToOpportunity(traffic) {
22
+ const {
23
+ url, total, bounceRate, paid, earned, owned,
24
+ } = traffic;
25
+
26
+ return {
27
+ type: 'high-inorganic-high-bounce-rate',
28
+ page: url,
29
+ screenshot: '',
30
+ trackedPageKPIName: 'Bounce Rate',
31
+ trackedPageKPIValue: bounceRate,
32
+ pageViews: total,
33
+ samples: total, // todo: get the actual number of samples
34
+ metrics: [{
35
+ type: 'traffic',
36
+ value: {
37
+ total,
38
+ paid,
39
+ owned,
40
+ earned,
41
+ },
42
+ }],
43
+ };
44
+ }
45
+
46
+ function hasHighInorganicTraffic(traffic) {
47
+ const { url, paid, total } = traffic;
48
+ const isHomepage = new URL(url).pathname === '/';
49
+ const threshold = isHomepage
50
+ ? HOMEPAGE_PAID_TRAFFIC_THRESHOLD
51
+ : NON_HOMEPAGE_PAID_TRAFFIC_THRESHOLD;
52
+ return paid / total > threshold;
53
+ }
54
+
55
+ function hasHighBounceRate(ctr) {
56
+ return ctr < BOUNCE_RATE_THRESHOLD;
57
+ }
58
+
59
+ function handler(bundles, opts = {}) {
60
+ const { interval = 7 } = opts;
61
+ const trafficByUrl = trafficAcquisition.handler(bundles);
62
+ const ctrByUrl = getCTRByUrl(bundles);
63
+
64
+ return trafficByUrl.filter((traffic) => traffic.total > interval * DAILY_PAGEVIEW_THRESHOLD)
65
+ .filter(hasHighInorganicTraffic)
66
+ .filter((traffic) => hasHighBounceRate(ctrByUrl[traffic.url]))
67
+ .map((traffic) => ({ ...traffic, bounceRate: 1 - ctrByUrl[traffic.url] }))
68
+ .map(convertToOpportunity);
69
+ }
70
+
71
+ export default {
72
+ handler,
73
+ checkpoints: ['email', 'enter', 'paid', 'utm', 'click', 'experiment'],
74
+ };
@@ -0,0 +1,77 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import trafficAcquisition from '../traffic-acquisition.js';
14
+ import { getCTRByUrl, getSiteAvgCTR } from '../../common/aggregateFns.js';
15
+
16
+ const DAILY_EARNED_THRESHOLD = 5000;
17
+ const CTR_THRESHOLD_RATIO = 0.95;
18
+ const DAILY_PAGEVIEW_THRESHOLD = 1000;
19
+
20
+ function convertToOpportunity(traffic) {
21
+ const {
22
+ url, total, ctr, paid, owned, earned, siteAvgCTR,
23
+ } = traffic;
24
+
25
+ return {
26
+ type: 'high-organic-low-ctr',
27
+ page: url,
28
+ screenshot: '',
29
+ trackedPageKPIName: 'Click Through Rate',
30
+ trackedPageKPIValue: ctr,
31
+ pageViews: total,
32
+ samples: total, // todo: get the actual number of samples
33
+ metrics: [{
34
+ type: 'traffic',
35
+ value: {
36
+ total,
37
+ paid,
38
+ owned,
39
+ earned,
40
+ },
41
+ }, {
42
+ type: 'ctr',
43
+ value: {
44
+ page: ctr,
45
+ siteAverage: siteAvgCTR,
46
+ },
47
+ }],
48
+ };
49
+ }
50
+
51
+ function hasHighOrganicTraffic(interval, traffic) {
52
+ const { earned, owned } = traffic;
53
+ return earned + owned > DAILY_EARNED_THRESHOLD * interval;
54
+ }
55
+
56
+ function hasLowerCTR(ctr, siteAvgCTR) {
57
+ return ctr < CTR_THRESHOLD_RATIO * siteAvgCTR;
58
+ }
59
+
60
+ function handler(bundles, opts = {}) {
61
+ const { interval = 7 } = opts;
62
+
63
+ const trafficByUrl = trafficAcquisition.handler(bundles);
64
+ const ctrByUrl = getCTRByUrl(bundles);
65
+ const siteAvgCTR = getSiteAvgCTR(bundles);
66
+
67
+ return trafficByUrl.filter((traffic) => traffic.total > interval * DAILY_PAGEVIEW_THRESHOLD)
68
+ .filter(hasHighOrganicTraffic.bind(null, interval))
69
+ .filter((traffic) => hasLowerCTR(ctrByUrl[traffic.url], siteAvgCTR))
70
+ .map((traffic) => ({ ...traffic, ctr: ctrByUrl[traffic.url], siteAvgCTR }))
71
+ .map(convertToOpportunity);
72
+ }
73
+
74
+ export default {
75
+ handler,
76
+ checkpoints: ['email', 'enter', 'paid', 'utm', 'click', 'experiment'],
77
+ };
@@ -12,6 +12,8 @@
12
12
 
13
13
  import { classifyTrafficSource } from '../common/traffic.js';
14
14
 
15
+ const MAIN_TYPES = ['total', 'paid', 'earned', 'owned'];
16
+
15
17
  function extractHints(bundle) {
16
18
  const findEvent = (checkpoint, source = '') => bundle.events.find((e) => e.checkpoint === checkpoint && (!source || e.source === source)) || {};
17
19
 
@@ -31,9 +33,12 @@ function extractHints(bundle) {
31
33
  }
32
34
 
33
35
  function collectByUrlAndTrafficSource(acc, { url, weight, trafficSource }) {
34
- acc[url] = acc[url] || { total: 0 };
36
+ acc[url] = acc[url] || {
37
+ total: 0, owned: 0, earned: 0, paid: 0,
38
+ };
35
39
  acc[url][trafficSource] = (acc[url][trafficSource] || 0) + weight;
36
40
  acc[url].total += weight;
41
+ acc[url][trafficSource.split(':')[0]] += weight;
37
42
  return acc;
38
43
  }
39
44
 
@@ -41,13 +46,16 @@ function transformFormat(trafficSources) {
41
46
  return Object.entries(trafficSources).map(([url, value]) => ({
42
47
  url,
43
48
  total: value.total,
49
+ earned: value.earned,
50
+ owned: value.owned,
51
+ paid: value.paid,
44
52
  sources: Object.entries(value)
45
- .filter(([source]) => source !== 'total')
53
+ .filter(([source]) => !MAIN_TYPES.includes(source))
46
54
  .map(([source, views]) => ({ type: source, views })),
47
55
  }));
48
56
  }
49
57
 
50
- async function handler(bundles) {
58
+ function handler(bundles) {
51
59
  const trafficSources = bundles
52
60
  .map(extractHints)
53
61
  .map((row) => {
package/src/index.js CHANGED
@@ -15,7 +15,9 @@ import cwv from './functions/cwv.js';
15
15
  import experiment from './functions/experiment.js';
16
16
  import trafficAcquisition from './functions/traffic-acquisition.js';
17
17
  import variant from './functions/variant.js';
18
- import rageclick from './functions/rageclick.js';
18
+ import rageclick from './functions/opportunities/rageclick.js';
19
+ import highInorganicHighBounceRate from './functions/opportunities/high-inorganic-high-bounce-rate.js';
20
+ import highOrganicLowCtr from './functions/opportunities/high-organic-low-ctr.js';
19
21
 
20
22
  const HANDLERS = {
21
23
  404: notfound,
@@ -24,6 +26,8 @@ const HANDLERS = {
24
26
  'traffic-acquisition': trafficAcquisition,
25
27
  variant,
26
28
  rageclick,
29
+ 'high-inorganic-high-bounce-rate': highInorganicHighBounceRate,
30
+ 'high-organic-low-ctr': highOrganicLowCtr,
27
31
  };
28
32
 
29
33
  export default class RUMAPIClient {
@@ -80,7 +84,7 @@ export default class RUMAPIClient {
80
84
  // Execute each query handler sequentially
81
85
  for (const { query, handler } of queryHandlers) {
82
86
  // eslint-disable-next-line no-await-in-loop
83
- results[query] = await handler(bundles);
87
+ results[query] = await handler(bundles, opts);
84
88
  }
85
89
 
86
90
  return results;