@adobe/spacecat-shared-rum-api-client 2.7.4 → 2.8.0

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,10 @@
1
+ # [@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
+
3
+
4
+ ### Features
5
+
6
+ * [oppty] Pages with high click amount on non-interacting element ([#329](https://github.com/adobe/spacecat-shared/issues/329)) ([610a22f](https://github.com/adobe/spacecat-shared/commit/610a22f59c1f197afb5ee4b1b9244a7f73fbce40))
7
+
1
8
  # [@adobe/spacecat-shared-rum-api-client-v2.7.4](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.7.3...@adobe/spacecat-shared-rum-api-client-v2.7.4) (2024-08-24)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-rum-api-client",
3
- "version": "2.7.4",
3
+ "version": "2.8.0",
4
4
  "description": "Shared modules of the Spacecat Services - Rum API client",
5
5
  "type": "module",
6
6
  "engines": {
@@ -0,0 +1,222 @@
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
+ /**
14
+ * return the pages that have rage clicks along with selectors and number of clicks
15
+ * @param {*} bundles
16
+ * @returns
17
+ */
18
+
19
+ /* c8 ignore start */
20
+ const DEFAULT_RAGE_CLICK_THRESHOLD = 10;
21
+ const DEFAULT_RAGECLICK_PERCENT_THRESHOLD = 5;
22
+ const DEFAULT_MINIMUM_RAGECLICK_SAMPLES_THRESHOLD = 10;
23
+ const DEFAULT_RAGECLICK_SAMPLES_THRESHOLD = 100;
24
+ const DEFAULT_RAGECLICK_PAGEVIEW_THRESHOLD = 5000;
25
+
26
+ const COMMERCE_SELECTORS_IGNORE_LIST = [
27
+ '.product-list-page',
28
+ '.product-details-verb',
29
+ '.product-details',
30
+ ];
31
+
32
+ const EDS_BLOCK_SELECTORS_IGNORE_LIST = ['.accordion'];
33
+
34
+ const RAGECLICK_SELECTORS_IGNORE_LIST = [
35
+ ...COMMERCE_SELECTORS_IGNORE_LIST,
36
+ ...EDS_BLOCK_SELECTORS_IGNORE_LIST,
37
+ ];
38
+
39
+ const OPPORTUNITY_TYPE = 'rageclick';
40
+ const OPPORTUNITY_DESCRIPTION = 'The percentage of users who click on the same element lot of times in a short period of time.';
41
+
42
+ /**
43
+ * Returns the selectors that have more than DEFAULT_RAGE_CLICK_THRESHOLD clicks from the events
44
+ * @param {*} events
45
+ * @returns
46
+ */
47
+ function getRageClickSelectors(events, threshold, selectorsIgnoreList = []) {
48
+ const clickSelectors = {};
49
+ for (const event of events) {
50
+ const { source, target, checkpoint } = event;
51
+ if (checkpoint === 'click' && !selectorsIgnoreList.includes(source)) {
52
+ if (!clickSelectors[source]) {
53
+ clickSelectors[source] = {
54
+ value: 0,
55
+ target: {
56
+ [target]: 0,
57
+ },
58
+ };
59
+ }
60
+ clickSelectors[source].value += 1;
61
+ if (!clickSelectors[source].target[target]) {
62
+ clickSelectors[source].target[target] = 1;
63
+ } else {
64
+ clickSelectors[source].target[target] += 1;
65
+ }
66
+ }
67
+ }
68
+ for (const selector of Object.keys(clickSelectors)) {
69
+ if (clickSelectors[selector].value < threshold) {
70
+ delete clickSelectors[selector];
71
+ }
72
+ }
73
+ return clickSelectors;
74
+ }
75
+
76
+ function filterRageClickInstancesByThreshold(
77
+ rageClickInstances,
78
+ pageData,
79
+ thresholds,
80
+ ) {
81
+ for (const url of Object.keys(rageClickInstances)) {
82
+ if (pageData[url].pageViews < thresholds.rageClickPageviewThreshold) {
83
+ // eslint-disable-next-line no-param-reassign
84
+ delete rageClickInstances[url];
85
+ } else {
86
+ for (const selector of Object.keys(rageClickInstances[url])) {
87
+ const rageClickPercentage = (
88
+ rageClickInstances[url][selector].samples / pageData[url].samples) * 100;
89
+ if ((rageClickInstances[url][selector].samples >= thresholds.rageClickMinSamplesThreshold
90
+ && rageClickPercentage >= thresholds.rageClickPercentThreshold)
91
+ || rageClickInstances[url][selector].samples >= thresholds.rageClickSamplesThreshold) {
92
+ // eslint-disable-next-line no-param-reassign
93
+ rageClickInstances[url][selector].percentage = rageClickPercentage;
94
+ } else {
95
+ // eslint-disable-next-line no-param-reassign
96
+ delete rageClickInstances[url][selector];
97
+ }
98
+ }
99
+ if (Object.keys(rageClickInstances[url]).length === 0) {
100
+ // eslint-disable-next-line no-param-reassign
101
+ delete rageClickInstances[url];
102
+ } else {
103
+ // eslint-disable-next-line no-param-reassign
104
+ rageClickInstances[url].pageViews = pageData[url].pageViews;
105
+ // eslint-disable-next-line no-param-reassign
106
+ rageClickInstances[url].samples = pageData[url].samples;
107
+ }
108
+ }
109
+ }
110
+ }
111
+
112
+ function getRageClickOpportunities(rageClickInstances) {
113
+ const opportunities = [];
114
+ for (const url of Object.keys(rageClickInstances)) {
115
+ const opportunity = {
116
+ type: OPPORTUNITY_TYPE,
117
+ page: url,
118
+ screenshot: '',
119
+ trackedPageKPIName: OPPORTUNITY_DESCRIPTION,
120
+ trackedPageKPIValue: '',
121
+ pageViews: rageClickInstances[url].pageViews,
122
+ samples: rageClickInstances[url].samples,
123
+ metrics: [],
124
+ };
125
+ for (const selector of Object.keys(rageClickInstances[url])) {
126
+ if (typeof rageClickInstances[url][selector] === 'object') {
127
+ opportunity.metrics.push({
128
+ type: 'click',
129
+ selector,
130
+ targets: rageClickInstances[url][selector].target,
131
+ value: rageClickInstances[url][selector].value,
132
+ samples: rageClickInstances[url][selector].samples,
133
+ percentage: rageClickInstances[url][selector].percentage,
134
+ mobileSamples: rageClickInstances[url][selector].mobileSamples,
135
+ desktopSamples: rageClickInstances[url][selector].desktopSamples,
136
+ });
137
+ }
138
+ }
139
+ const avgRageClickPercentage = opportunity.metrics.reduce(
140
+ (acc, metric) => acc + metric.percentage,
141
+ 0,
142
+ ) / opportunity.metrics.length;
143
+ opportunity.trackedPageKPIValue = avgRageClickPercentage;
144
+ opportunities.push(opportunity);
145
+ }
146
+ return opportunities;
147
+ }
148
+
149
+ function handler(bundles) {
150
+ const rageClickInstances = {};
151
+ const pageData = {};
152
+ const rageClickThreshold = process.env.RAGE_CLICK_THRESHOLD || DEFAULT_RAGE_CLICK_THRESHOLD;
153
+ const rageClickPercentThreshold = process.env.RAGE_CLICK_PERCENT_THRESHOLD
154
+ || DEFAULT_RAGECLICK_PERCENT_THRESHOLD;
155
+ const rageClickPageviewThreshold = process.env.RAGE_CLICK_PAGEVIEW_THRESHOLD
156
+ || DEFAULT_RAGECLICK_PAGEVIEW_THRESHOLD;
157
+ const rageClickMinSamplesThreshold = process.env.RAGE_CLICK_MIN_SAMPLES_THRESHOLD
158
+ || DEFAULT_MINIMUM_RAGECLICK_SAMPLES_THRESHOLD;
159
+ const rageClickSamplesThreshold = process.env.RAGE_CLICK_SAMPLES_THRESHOLD
160
+ || DEFAULT_RAGECLICK_SAMPLES_THRESHOLD;
161
+ const thresholds = {
162
+ rageClickPercentThreshold,
163
+ rageClickPageviewThreshold,
164
+ rageClickMinSamplesThreshold,
165
+ rageClickSamplesThreshold,
166
+ };
167
+ for (const bundle of bundles) {
168
+ const { url, weight } = bundle;
169
+ if (!pageData[url]) {
170
+ pageData[url] = {
171
+ pageViews: weight,
172
+ samples: 1,
173
+ };
174
+ } else {
175
+ pageData[url].pageViews += weight;
176
+ pageData[url].samples += 1;
177
+ }
178
+ const rageClickSelectors = getRageClickSelectors(
179
+ bundle.events,
180
+ rageClickThreshold,
181
+ RAGECLICK_SELECTORS_IGNORE_LIST,
182
+ );
183
+ const isMobile = bundle.userAgent && bundle.userAgent.includes('mobile');
184
+ if (Object.keys(rageClickSelectors).length > 0) {
185
+ if (!rageClickInstances[url]) {
186
+ rageClickInstances[url] = {};
187
+ }
188
+ for (const selector of Object.keys(rageClickSelectors)) {
189
+ if (!rageClickInstances[url][selector]) {
190
+ rageClickInstances[url][selector] = {};
191
+ rageClickInstances[url][selector].value = rageClickSelectors[selector].value;
192
+ rageClickInstances[url][selector].samples = 1;
193
+ rageClickInstances[url][selector].target = rageClickSelectors[selector].target;
194
+ rageClickInstances[url][selector].mobileSamples = isMobile ? 1 : 0;
195
+ rageClickInstances[url][selector].desktopSamples = !isMobile ? 1 : 0;
196
+ } else {
197
+ rageClickInstances[url][selector].value += rageClickSelectors[selector].value;
198
+ rageClickInstances[url][selector].samples += 1;
199
+ rageClickInstances[url][selector].mobileSamples += isMobile ? 1 : 0;
200
+ rageClickInstances[url][selector].desktopSamples += !isMobile ? 1 : 0;
201
+ for (const target of Object.keys(rageClickSelectors[selector].target)) {
202
+ if (!rageClickInstances[url][selector].target[target]) {
203
+ // eslint-disable-next-line max-len
204
+ rageClickInstances[url][selector].target[target] = rageClickSelectors[selector].target[target];
205
+ } else {
206
+ // eslint-disable-next-line max-len
207
+ rageClickInstances[url][selector].target[target] += rageClickSelectors[selector].target[target];
208
+ }
209
+ }
210
+ }
211
+ }
212
+ }
213
+ }
214
+ filterRageClickInstancesByThreshold(rageClickInstances, pageData, thresholds);
215
+ return getRageClickOpportunities(rageClickInstances);
216
+ }
217
+
218
+ export default {
219
+ handler,
220
+ checkpoints: ['click'],
221
+ };
222
+ /* c8 ignore stop */
package/src/index.d.ts CHANGED
@@ -56,5 +56,5 @@ export default class RUMAPIClient {
56
56
  * @returns {Promise<object>} A Promise that resolves to an object where each key is the name
57
57
  * of a query, and each value is the result of that query.
58
58
  */
59
- queryMulti(queries: string[], opts?: RUMAPIOptions): Promise<object[]>;
59
+ queryMulti(queries: string[], opts?: RUMAPIOptions): Promise<object>;
60
60
  }
package/src/index.js CHANGED
@@ -15,6 +15,7 @@ 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
19
 
19
20
  const HANDLERS = {
20
21
  404: notfound,
@@ -22,6 +23,7 @@ const HANDLERS = {
22
23
  experiment,
23
24
  'traffic-acquisition': trafficAcquisition,
24
25
  variant,
26
+ rageclick,
25
27
  };
26
28
 
27
29
  export default class RUMAPIClient {