From 8a67e690c482531bd36612f150cce26b7cb696d9 Mon Sep 17 00:00:00 2001 From: ekremney Date: Thu, 12 Dec 2024 19:13:44 +0100 Subject: [PATCH] feat(rum-api-client): campaign performance measurement --- .../src/functions/campaign-performance.js | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 packages/spacecat-shared-rum-api-client/src/functions/campaign-performance.js diff --git a/packages/spacecat-shared-rum-api-client/src/functions/campaign-performance.js b/packages/spacecat-shared-rum-api-client/src/functions/campaign-performance.js new file mode 100644 index 00000000..e528ff57 --- /dev/null +++ b/packages/spacecat-shared-rum-api-client/src/functions/campaign-performance.js @@ -0,0 +1,57 @@ +/* + * Copyright 2024 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +/* c8 ignore start */ + +import { DataChunks, series } from '@adobe/rum-distiller'; +import { loadBundles } from '../utils.js'; + +function handler(bundles) { + const dataChunks = new DataChunks(); + loadBundles(bundles, dataChunks); + + const campaignIdFacet = (bundle) => (bundle.events.some((e) => e.checkpoint === 'paid' && e.source === 'facebook') + ? bundle.events.find((e) => e.checkpoint === 'utm' && e.source === 'utm_campaign')?.target + : null); + dataChunks.addFacet('traffic', campaignIdFacet); + + dataChunks.addSeries('ctr', (bundle) => (bundle.events.some((e) => e.checkpoint === 'click') + ? bundle.weight + : 0)); + dataChunks.addSeries('lcp', series.lcp); + dataChunks.addSeries('views', series.pageViews); + + return dataChunks.facets.traffic.map((traffic) => { + const campaignId = traffic.value; + const { ctr, lcp, views } = traffic.metrics; + + return { + campaignId, + ctr: (ctr.sum / ctr.weight).toFixed(4), + lcp: lcp.percentile(75), + views: views.weight, + }; + }).sort((a, b) => b.views - a.views); +} + +export default { + handler, + checkpoints: [ + 'click', + 'cwv-lcp', + 'utm', + 'paid', + 'email', + 'enter', + ], +}; +/* c8 ignore end */