-
Notifications
You must be signed in to change notification settings - Fork 15
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
Open
ssilare-adobe
wants to merge
3
commits into
main
Choose a base branch
from
SITES-36715
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ | |
| * governing permissions and limitations under the License. | ||
| */ | ||
| import { GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3'; | ||
| import { DEFAULT_CPC_VALUE } from './constants.js'; | ||
| import { getObjectFromKey } from './s3.js'; | ||
|
|
||
| function createFilePath({ siteId, source, metric }) { | ||
| if (!siteId) { | ||
|
|
@@ -80,3 +82,45 @@ export async function storeMetrics(content, config, context) { | |
| throw new Error(`Failed to upload metrics to ${filePath}, error: ${e.message}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Fetches the organic traffic data for a site from S3 and calculate the CPC value as per | ||
| * https://wiki.corp.adobe.com/pages/viewpage.action?spaceKey=AEMSites&title=Success+Studio+Projected+Business+Impact+Metrics#SuccessStudioProjectedBusinessImpactMetrics-IdentifyingCPCvalueforadomain | ||
| * @param context | ||
| * @param siteId | ||
| * @returns {number} CPC value | ||
| */ | ||
| export async function calculateCPCValue(context, siteId) { | ||
| if (!context?.env?.S3_IMPORTER_BUCKET_NAME) { | ||
| throw new Error('S3 importer bucket name is required'); | ||
| } | ||
| if (!context.s3Client) { | ||
| throw new Error('S3 client is required'); | ||
| } | ||
| if (!context.log) { | ||
| throw new Error('Logger is required'); | ||
| } | ||
| if (!siteId) { | ||
| throw new Error('SiteId is required'); | ||
| } | ||
| const { s3Client, log } = context; | ||
| const bucketName = context.env.S3_IMPORTER_BUCKET_NAME; | ||
| const key = `metrics/${siteId}/ahrefs/organic-traffic.json`; | ||
| try { | ||
| 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; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an object return. Success Failed : |
||
| } | ||
| const lastTraffic = organicTrafficData.at(-1); | ||
| if (!lastTraffic.cost || !lastTraffic.value) { | ||
ssilare-adobe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| log.warn(`Invalid organic traffic data present for ${siteId} - cost:${lastTraffic.cost} value:${lastTraffic.value}, Using Default CPC value.`); | ||
| return DEFAULT_CPC_VALUE; | ||
| } | ||
| // dividing by 100 for cents to dollar conversion | ||
| return lastTraffic.cost / lastTraffic.value / 100; | ||
| } catch (err) { | ||
| log.error(`Error fetching organic traffic data for site ${siteId}. Using Default CPC value.`, err); | ||
| return DEFAULT_CPC_VALUE; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
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
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.
There's another issue to this, what if the consumer doesn't define the CPC value or misses it? I mean not just for the reports, but for the audits like meta-tags or cwv, which will eventually use this?
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.
I mean not just for the reports, but for the audits like meta-tags or cwv, which will eventually use this?
We will add that in documentation, whenever null or any case will occur they have to handle themselves.
We will provide actual value if null they have to handle like what they have to do that time
Default or don't have to add
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.
Okay got it.