-
Notifications
You must be signed in to change notification settings - Fork 14
feat: moved calculateCpcValue function to spacecat-shared-utils
#1048
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Sahil Silare <[email protected]>
Signed-off-by: Sahil Silare <[email protected]>
|
This PR will trigger a minor release when merged. |
| return DEFAULT_CPC_VALUE; | ||
| } | ||
| const lastTraffic = organicTrafficData.at(-1); | ||
| if (!lastTraffic.cost || !lastTraffic.value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current validation catches 0 and falsy values but misses:
Negative numbers: cost: -100 or value: -50 would pass validation but produce incorrect CPC calculations
Non-numeric types: value: "hello" or value: NaN would pass through and result in NaN on line 121
Can do something like this
// Validate both values are positive numbers
if (typeof lastTraffic.cost !== 'number' ||
typeof lastTraffic.value !== 'number' ||
!Number.isFinite(lastTraffic.cost) ||
!Number.isFinite(lastTraffic.value) ||
lastTraffic.cost <= 0 ||
lastTraffic.value <= 0) {
log.warn(Invalid organic traffic data for ${siteId} - cost:${lastTraffic.cost} value:${lastTraffic.value}. Using Default CPC value.);
return DEFAULT_CPC_VALUE;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add null check aswell
| } | ||
| } | ||
| // Always return body for non-JSON content types | ||
| return body; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent return types,
Can we make it to string always or json always.
| const organicTrafficData = await getObjectFromKey(s3Client, bucketName, key, log); | ||
| if (!Array.isArray(organicTrafficData) || organicTrafficData.length === 0) { | ||
| log.warn(`Organic traffic data not available for ${siteId}. Using Default CPC value.`); | ||
| return DEFAULT_CPC_VALUE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead setting default and return
how about we let downstream decide default.
As there will be no identifier if cpc is 1.5 its because of default or its actual.
Like upstream should get something which shows we are unable to fetch with reason
SITES-36715
Add CPC Calculation Utility
Summary
Adds a new utility function to calculate Cost Per Click (CPC) values from Ahrefs organic traffic data stored in S3.
Changes
Added
calculateCPCValuefunction inmetrics-store.jsAdded
getObjectFromKeyhelper ins3.jsAdded
DEFAULT_CPC_VALUEconstant (1.5)Updated exports in
index.jsandindex.d.tsPlease ensure your pull request adheres to the following guidelines:
Related Issues
Thanks for contributing!