Maayan via Elementary: fix(marketing): normalize ad cost units in marketing_ads union - #1002
Open
elementary-data-demo[bot] wants to merge 1 commit into
Open
Conversation
One ad source began reporting cost in cents, inflating total_spend ~100x and collapsing RETURN_ON_ADVERTISING_SPEND in cpa_and_roas. Convert the cents-denominated feed to dollars via cents_to_dollars and select cost explicitly so units are enforced at the union boundary.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes the
RETURN_ON_ADVERTISING_SPENDanomaly oncpa_and_roas(average collapsed from ~75.45 to 0.300, a ~250x drop; incident60c571d2).Root cause
ROAS in
cpa_and_roasis100.0 * attribution_revenue / total_spend. The revenue (numerator) branch —orders→customer_conversions→attribution_touches.linear_revenue— is clean. The spend (denominator) branch traces back throughads_spend→marketing_ads, which does a rawUNION ALLof three ad-platform sources (stg_google_ads,stg_facebook_ads,stg_instagram_ads) with no cost normalization — it selected*from each and summedcostdirectly.One source began reporting
costin cents instead of dollars, inflating its spend ~100x. Blended across the union this drovetotal_spendfar above its baseline and crushed ROAS. The mismatch is invisible to freshness and volume tests (row counts and timestamps look normal), which is why it surfaced only as a column-value anomaly.Fix
costexplicitly at each source branch (instead ofselect *) so the unit is enforced at the union boundary.cents_to_dollars()macro to the cents-denominated feed so all three sources are in dollars before the union — matching the convention already used inreal_time_ordersfor monetary fields.Reviewer action required
I applied the conversion to
stg_instagram_adsas the feed reporting in cents. Please confirm this is the correct source — I could not query live warehouse values to empirically verify which platform switched units. If it is a different platform (Google/Facebook), move thecents_to_dollars('cost')wrapper to the correct branch.Prevention
Consider adding a per-source
costrange/anomaly test on thestg_*_adssources so a future unit shift is caught at the boundary rather than downstream in ROAS.Created by:
maayan+172@elementary-data.com