diff --git a/packages/spacecat-shared-rum-api-client/src/functions/total-metrics.js b/packages/spacecat-shared-rum-api-client/src/functions/total-metrics.js index 0a932c15..04428313 100644 --- a/packages/spacecat-shared-rum-api-client/src/functions/total-metrics.js +++ b/packages/spacecat-shared-rum-api-client/src/functions/total-metrics.js @@ -21,7 +21,9 @@ function handler(bundles) { ? bundle.weight : 0)); const totalPageViews = dataChunks?.totals?.traffic_domain?.weight; - const totalCTR = dataChunks?.totals?.ctr?.sum / dataChunks?.totals?.ctr?.weight; + const sum = dataChunks?.totals?.ctr?.sum ?? 0; + const weight = dataChunks?.totals?.ctr?.weight ?? 0; + const totalCTR = weight !== 0 ? sum / weight : 0; return { totalPageViews, totalCTR, diff --git a/packages/spacecat-shared-rum-api-client/test/total-metrics.test.js b/packages/spacecat-shared-rum-api-client/test/total-metrics.test.js index 30857b8a..35e69e38 100644 --- a/packages/spacecat-shared-rum-api-client/test/total-metrics.test.js +++ b/packages/spacecat-shared-rum-api-client/test/total-metrics.test.js @@ -18,7 +18,7 @@ describe('Total Metrics Queries', () => { it('crunches CWV data', async () => { const result = totalMetrics.handler(bundlesForUrls.rumBundles); expect(result).to.deep.equal({ - totalCTR: 7303, + totalCTR: 0.2027468663384768, totalPageViews: 24173, }); });