KatInterview

Last-Touch Ad Attribution: Daily Campaign Conversion Report

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionsattribution

Last updated

Write a SQL query that credits each conversion to the latest eligible ad impression within a 7-day lookback, then reports daily campaign metrics for January 2024.

Daily campaign performance with last-touch attribution

An advertising platform logs ad exposures and downstream purchases in two tables.

ad_impressions

ColumnTypeMeaning
impression_idSTRINGPrimary key for the exposure
user_idSTRINGViewer
ad_idSTRINGAd that was displayed
campaign_idSTRINGCampaign owning the ad
impression_tsTIMESTAMPServe time (UTC)
cost_usdNUMERICCost charged to the advertiser for this exposure

ad_conversions

ColumnTypeMeaning
conversion_idSTRINGPrimary key for the conversion
user_idSTRINGConverting user
ad_idSTRINGAd linked to the conversion
conversion_tsTIMESTAMPConversion time (UTC)
revenue_usdNUMERICValue of the conversion

Attribution rules

  1. A conversion may only be credited to an impression with the same user_id and the same ad_id.
  2. The impression must happen strictly before the conversion.
  3. The conversion must happen no later than 7 days after the impression.
  4. When several impressions qualify, credit the one closest in time before the conversion (the latest qualifying one).
  5. A conversion is credited at most once, and may end up credited to nothing.

Task

Consider only impressions served from 2024-01-01 00:00:00 UTC to 2024-01-31 23:59:59 UTC. For each campaign_id and UTC calendar date impression_date, return:

  • campaign_id
  • impression_date
  • impressions: count of impressions
  • unique_impression_users: count of distinct users who saw an impression
  • attributed_conversions: conversions credited to those impressions
  • conversion_rate: attributed_conversions / impressions
  • total_spend_usd: sum of cost_usd
  • total_conversion_revenue_usd: sum of revenue from the credited conversions
  • avg_hours_to_conversion: mean number of hours from impression to credited conversion, computed over credited conversions only

Campaign-days that have impressions but no credited conversions must still appear, with 0 conversions and 0 revenue.

Explain your approach, state any assumptions about boundaries and NULLs, and give the query.

Hints

Hint 1

Split the work in two: first decide which impression (if any) each conversion belongs to, then roll up January impressions by campaign and day.

Hint 2

Do not restrict impressions to January while choosing the winning impression. A December or February impression can be closer to the conversion and must be allowed to win; filter to January only after the winner is chosen.

Hint 3

Rank the eligible (conversion, impression) pairs per conversion by impression time descending and keep rank 1. Then collapse to one row per impression before joining back, so cost and impression counts are not multiplied by the number of conversions.

Hint 4

For the average, divide total hours by total credited conversions rather than averaging per-impression averages, and return NULL when there are no conversions.