-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add query for fetching RUM engagement metrics #985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
74645e6
fix: add engagement query
dipratap a9db6ef
Merge branch 'main' of github.com:adobe/spacecat-shared into perf_dash
dipratap 508557a
Merge branch 'main' of github.com:adobe/spacecat-shared into perf_dash
dipratap 63e82fd
fix: add comment
dipratap 0fa62e2
Merge branch 'main' of github.com:adobe/spacecat-shared into perf_dash
dipratap 44e011c
fix: merge main
dipratap 381f78e
fix: refactor
dipratap 589506d
fix: add user-engagement import type
dipratap f1e7fe5
fix: add user-engagement import type
dipratap 0fb3601
fix: add new import to default configs
dipratap 80164aa
Merge branch 'main' of github.com:adobe/spacecat-shared into perf_dash
dipratap 95547ae
fix: address review comments
dipratap cdff554
fix: merge main
dipratap caf5ad9
fix: merge main
dipratap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/spacecat-shared-rum-api-client/src/functions/user-engagement.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
|
|
||
| const evaluateEngagement = (bundle) => { | ||
| const clickEngagement = bundle.events.filter((evt) => evt.checkpoint === 'click').length > 0 | ||
| ? bundle.weight | ||
| : 0; | ||
| const contentEngagement = bundle.events | ||
| .filter((evt) => evt.checkpoint === 'viewmedia' || evt.checkpoint === 'viewblock') | ||
| .length > 3 | ||
| ? bundle.weight | ||
| : 0; | ||
| return clickEngagement || contentEngagement; | ||
| }; | ||
|
|
||
| /** | ||
| * Calculates engagement metrics for all pages from RUM data. | ||
| * A page view is considered engaged if there has been at least some user interaction (click events) | ||
| * or significant content has been viewed (4 or more viewmedia or viewblock events). | ||
| * Ref. - https://github.com/adobe/rum-distiller/blob/22f8b3caa6d700f4d1cbe29a94b7da34b9d50764/series.js#L89 | ||
| * | ||
| * @param {Array} bundles - The RUM bundles to calculate engagement metrics for. | ||
| * @returns {Array} An array of engagement metrics for each page. | ||
| */ | ||
| function handler(bundles) { | ||
| const urlsData = {}; | ||
| bundles.forEach((bundle) => { | ||
| const engagementTraffic = evaluateEngagement(bundle); | ||
| if (!urlsData[bundle.url]) { | ||
| urlsData[bundle.url] = { | ||
| url: bundle.url, | ||
| totalTraffic: bundle.weight, | ||
| engagementTraffic, | ||
| engagementPercentage: (engagementTraffic / bundle.weight) * 100, | ||
| }; | ||
| } else { | ||
| urlsData[bundle.url].totalTraffic += bundle.weight; | ||
| urlsData[bundle.url].engagementTraffic += engagementTraffic; | ||
| urlsData[bundle.url].engagementPercentage = Math.round( | ||
| (urlsData[bundle.url].engagementTraffic / urlsData[bundle.url].totalTraffic) * 100, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| return Object.values(urlsData); | ||
| } | ||
|
|
||
| export default { | ||
| handler, | ||
| checkpoints: ['click', 'viewmedia', 'viewblock'], | ||
prithipalpatwal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/spacecat-shared-rum-api-client/test/user-engagement.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
| /* eslint-env mocha */ | ||
|
|
||
| import { expect } from 'chai'; | ||
| import userEngagement from '../src/functions/user-engagement.js'; | ||
|
|
||
| describe('userEngagement', () => { | ||
| it('calculates user engagement metrics correctly', () => { | ||
| const mockBundles = [ | ||
| { | ||
| id: 'bundle1', | ||
| url: 'https://example.com/page1', | ||
| weight: 100, | ||
| events: [ | ||
| { checkpoint: 'click', timeDelta: 2000 }, | ||
| ], | ||
| }, | ||
| { | ||
| id: 'bundle2', | ||
| url: 'https://example.com/page1', | ||
| weight: 50, | ||
| events: [ | ||
| // No click event | ||
| ], | ||
| }, | ||
| { | ||
| id: 'bundle3', | ||
| url: 'https://example.com/page2', | ||
| weight: 200, | ||
| events: [ | ||
| { checkpoint: 'click', timeDelta: 1200 }, | ||
| { checkpoint: 'click', timeDelta: 1500 }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = userEngagement.handler(mockBundles); | ||
|
|
||
| expect(result).to.be.an('array'); | ||
| expect(result).to.have.length(2); | ||
|
|
||
| // Check page2 (higher totalTraffic, should be first) | ||
| const page2 = result[1]; | ||
| expect(page2.url).to.equal('https://example.com/page2'); | ||
| expect(page2.totalTraffic).to.equal(200); | ||
| expect(page2.engagementPercentage).to.equal(100); | ||
|
|
||
| // Check page1 | ||
| const page1 = result[0]; | ||
| expect(page1.url).to.equal('https://example.com/page1'); | ||
| expect(page1.totalTraffic).to.equal(150); | ||
| expect(page1.engagementPercentage).to.equal(67); | ||
| }); | ||
|
|
||
| it('handles empty bundles array', () => { | ||
| const result = userEngagement.handler([]); | ||
| expect(result).to.be.an('array'); | ||
| expect(result).to.have.length(0); | ||
| }); | ||
|
|
||
| it('handles bundles with no click events', () => { | ||
| const mockBundles = [ | ||
| { | ||
| id: 'bundle1', | ||
| url: 'https://example.com/page1', | ||
| weight: 100, | ||
| events: [], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = userEngagement.handler(mockBundles); | ||
|
|
||
| expect(result).to.be.an('array'); | ||
| expect(result).to.have.length(1); | ||
|
|
||
| const page = result[0]; | ||
| expect(page.url).to.equal('https://example.com/page1'); | ||
| expect(page.totalTraffic).to.equal(100); | ||
| expect(page.engagementPercentage).to.equal(0); | ||
| }); | ||
|
|
||
| it('handles content engagement (viewmedia/viewblock)', () => { | ||
| const mockBundles = [ | ||
| { | ||
| id: 'bundle1', | ||
| url: 'https://example.com/page1', | ||
| weight: 100, | ||
| events: [ | ||
| { checkpoint: 'viewmedia', timeDelta: 1000 }, | ||
| { checkpoint: 'viewmedia', timeDelta: 2000 }, | ||
| { checkpoint: 'viewmedia', timeDelta: 3000 }, | ||
| { checkpoint: 'viewmedia', timeDelta: 4000 }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = userEngagement.handler(mockBundles); | ||
|
|
||
| expect(result).to.be.an('array'); | ||
| expect(result).to.have.length(1); | ||
|
|
||
| const page = result[0]; | ||
| expect(page.url).to.equal('https://example.com/page1'); | ||
| expect(page.totalTraffic).to.equal(100); | ||
| expect(page.engagementPercentage).to.equal(100); | ||
| }); | ||
|
|
||
| it('handles mixed click and content engagement', () => { | ||
| const mockBundles = [ | ||
| { | ||
| id: 'bundle1', | ||
| url: 'https://example.com/page1', | ||
| weight: 100, | ||
| events: [ | ||
| { checkpoint: 'click', timeDelta: 1000 }, | ||
| ], | ||
| }, | ||
| { | ||
| id: 'bundle2', | ||
| url: 'https://example.com/page1', | ||
| weight: 50, | ||
| events: [ | ||
| { checkpoint: 'viewmedia', timeDelta: 1000 }, | ||
| { checkpoint: 'viewmedia', timeDelta: 2000 }, | ||
| { checkpoint: 'viewmedia', timeDelta: 3000 }, | ||
| { checkpoint: 'viewmedia', timeDelta: 4000 }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = userEngagement.handler(mockBundles); | ||
|
|
||
| expect(result).to.be.an('array'); | ||
| expect(result).to.have.length(1); | ||
|
|
||
| const page = result[0]; | ||
| expect(page.url).to.equal('https://example.com/page1'); | ||
| expect(page.totalTraffic).to.equal(150); | ||
| expect(page.engagementPercentage).to.equal(100); | ||
| }); | ||
|
|
||
| it('has correct checkpoints', () => { | ||
| expect(userEngagement.checkpoints).to.deep.equal(['click', 'viewmedia', 'viewblock']); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.