How to Measure 30-Day Booking Lag for Functional Medicine Ads in PostgreSQL
A functional medicine clinic spends $600 on a search campaign in October. By November 1, the clinic has recorded two confirmed discovery calls from those clicks. Two weeks later, the same clicks have produced four. The October spend stayed fixed. The people who saw the ads needed more time to book.
If your report pairs October spend with bookings confirmed in October, it can miss those late bookings and pick up bookings from September clicks. That matters when a clinic is deciding whether to keep funding a campaign for an initial consultation that patients may research across several visits.
This tutorial builds a PostgreSQL report that groups spend and bookings by the week of the original attributed click. You will calculate 7-, 14-, and 30-day booking counts, hide the cost-per-booking figure for incomplete cohorts, and test the result with synthetic data.
1. Decide what each row measures
The output has one row per campaign per click week. Weeks start on Monday in the clinic’s reporting timezone. The input spend is already aggregated by campaign and local report date. Align that date with the reporting timezone before loading it. If the ad account uses another timezone, align the reporting setup or source data before running the query. A daily total cannot be precisely shifted across timezones after aggregation.
A booking counts once, at its first confirmed timestamp. The report uses a single attributed click per booking, assigned upstream. The sample assumes that the attribution decision has already been made and that the click and booking records are available in the same internal system. It does not attempt to identify a website visitor or reconstruct a missing source. Phone bookings will only appear if the clinic has a reliable way to associate them with an eligible click in the same system.
The windows use elapsed time. The 7-day window is the first 168 hours after a click. A booking exactly 168 hours after the click enters the 14-day count and falls outside the 7-day count. Bookings exactly 720 hours after the click fall outside all three windows. This boundary makes the report consistent across daylight-saving changes.
2. Create the fixture
Run the following in a fresh PostgreSQL session. The table names, IDs, campaigns and spend are all synthetic. The booking table contains a reference to a click, a booking key and a timestamp. Keep free-text intake answers, contact details and clinical records out of this reporting dataset.
CREATE TEMP TABLE campaign_spend ( report_date date NOT NULL, campaign_id text NOT NULL, spend_usd numeric(12,2) NOT NULL CHECK (spend_usd >= 0), PRIMARY KEY (report_date, campaign_id) ); CREATE TEMP TABLE ad_click ( click_key text PRIMARY KEY, campaign_id text NOT NULL, clicked_at timestamptz NOT NULL ); CREATE TEMP TABLE confirmed_booking ( booking_key text PRIMARY KEY, click_key text NOT NULL REFERENCES ad_click (click_key), confirmed_at timestamptz NOT NULL ); INSERT INTO campaign_spend VALUES ('2026-10-05', 'search-a', 120), ('2026-10-08', 'search-a', 180), ('2026-10-05', 'search-b', 90), ('2026-10-12', 'search-a', 110); INSERT INTO ad_click VALUES ('c1', 'search-a', '2026-10-05 16:00+00'), ('c2', 'search-a', '2026-10-05 16:00+00'), ('c3', 'search-a', '2026-10-05 16:00+00'), ('c4', 'search-a', '2026-10-05 16:00+00'), ('c5', 'search-a', '2026-10-12 16:00+00'); INSERT INTO confirmed_booking VALUES ('b1', 'c1', '2026-10-07 16:00+00'), ('b2', 'c2', '2026-10-12 16:00+00'), ('b3', 'c3', '2026-10-25 16:00+00'), ('b4', 'c4', '2026-11-04 16:00+00'), ('b5', 'c5', '2026-10-13 16:00+00'); Campaign A has two spend rows and four booking rows in its first week. Joining raw spend to bookings on just campaign and week would produce eight rows and can inflate both measures. Aggregate spend and bookings independently before joining. Campaign B has spend and no matched bookings, which lets you verify that the report retains zero-booking campaigns.
3. Build the report from weekly aggregates
The date series lists the Monday boundaries in the report. PostgreSQL’s documentation for set-returning functions includes the integer form of generate_series used below. Adding multiples of seven to a date avoids using a timestamp series for local calendar boundaries.
The cutoff is exclusive. Set it to the latest instant through which your source data is complete, accounting for an overnight import or reconciliation delay. A full week is eligible for a 30-day cost figure once the end of that local week plus 720 hours is at or before the cutoff. This conservative rule gives even the last possible click in the week its full observation window.
WITH params AS ( SELECT DATE '2026-10-05' AS first_week, DATE '2026-10-12' AS last_week, 'America/New_York'::text AS report_tz, TIMESTAMPTZ '2026-11-13 06:00+00' AS cutoff ), weeks AS ( SELECT p.first_week + 7 * g.n AS click_week, ((p.first_week + 7 * (g.n + 1))::timestamp AT TIME ZONE p.report_tz) AS week_ends_at, p.cutoff FROM params p CROSS JOIN LATERAL generate_series(0, (p.last_week - p.first_week) / 7) AS g(n) ), weekly_spend AS ( SELECT date_trunc('week', s.report_date::timestamp)::date AS click_week, s.campaign_id, sum(s.spend_usd) AS spend_usd FROM campaign_spend s CROSS JOIN params p WHERE s.report_date >= p.first_week AND s.report_date < p.last_week + 7 GROUP BY 1, 2 ), eligible_bookings AS ( SELECT date_trunc('week', c.clicked_at AT TIME ZONE p.report_tz)::date AS click_week, c.campaign_id, b.confirmed_at - c.clicked_at AS booking_lag FROM ad_click c JOIN confirmed_booking b USING (click_key) CROSS JOIN params p WHERE c.clicked_at >= (p.first_week::timestamp AT TIME ZONE p.report_tz) AND c.clicked_at < ((p.last_week + 7)::timestamp AT TIME ZONE p.report_tz) AND b.confirmed_at >= c.clicked_at AND b.confirmed_at < c.clicked_at + INTERVAL '720 hours' AND b.confirmed_at < p.cutoff ), weekly_bookings AS ( SELECT click_week, campaign_id, count(*) FILTER (WHERE booking_lag < INTERVAL '168 hours') AS b7, count(*) FILTER (WHERE booking_lag < INTERVAL '336 hours') AS b14, count(*) AS b30 FROM eligible_bookings GROUP BY 1, 2 ), cohort_keys AS ( SELECT click_week, campaign_id FROM weekly_spend UNION SELECT click_week, campaign_id FROM weekly_bookings ) SELECT k.click_week, k.campaign_id, s.spend_usd, coalesce(b.b7, 0) AS bookings_7d_observed, coalesce(b.b14, 0) AS bookings_14d_observed, coalesce(b.b30, 0) AS bookings_30d_observed, w.week_ends_at + INTERVAL '720 hours' <= w.cutoff AS mature_30d, CASE WHEN w.week_ends_at + INTERVAL '720 hours' <= w.cutoff THEN round(s.spend_usd / nullif(b.b30, 0), 2) END AS cost_per_booking_30d FROM cohort_keys k JOIN weeks w USING (click_week) LEFT JOIN weekly_spend s USING (click_week, campaign_id) LEFT JOIN weekly_bookings b USING (click_week, campaign_id) ORDER BY k.click_week, k.campaign_id; The UNION keeps a campaign-week if either source has data. A booking with no matching spend appears with a null spend value. That can expose discrepancies such as a campaign remap or a missing date in the spend export. A cohort with no activity in either source does not appear. If your dashboard requires those rows, generate the expected campaign-week combinations from a campaign calendar and use them as the starting point.
4. Check the boundary cases against the fixture
| Click week | Campaign | Spend | Bookings in 7d | Bookings in 14d | Bookings in 30d | Mature at 30d? | 30d cost per booking |
|---|---|---|---|---|---|---|---|
| Oct 5 | search-a | $300 | 1 | 2 | 3 | Yes | $100 |
| Oct 5 | search-b | $90 | 0 | 0 | 0 | Yes | NULL |
| Oct 12 | search-a | $110 | 1 | 1 | 1 | No | NULL |
Booking b2 lands on the exact 7-day boundary, so it only appears in the 14- and 30-day counts. Booking b3 takes 20 days and increases only the 30-day count. Booking b4 lands on the exact 30-day boundary and is excluded. The October 12 cohort has already recorded a booking, but more bookings could arrive inside its window. Its current $110 spend divided by one booking would be premature as a comparison with the mature October 5 cohort.
Try changing the cutoff to 2026-11-20 06:00+00. The October 12 cohort now qualifies for a cost-per-booking figure of $110 in this fixture. Then change the cutoff back and remove the October 12 spend row in your test session. Its booking row should survive with null spend. These two changes test independent failure modes, incomplete observation and a missing input.
5. Turn the output into a weekly marketing report
Keep the observed counts visible on recent weeks so the clinic can see early activity. Use the mature flag for budget comparisons, and leave the nulls intact. A campaign that spent $90 with zero bookings has a real cost and an undefined cost per booking. A zero in that last column would tell a very different story.
When a campaign only books a few consultations a week, combine several mature cohorts. Sum spend across those cohorts and divide by their total bookings. Include mature weeks with spend and zero bookings in the numerator. Averaging weekly cost-per-booking values gives a small week the same weight as a busy one and can hide spending from weeks with zero bookings.
Check spend totals against the export before presenting the numbers. Verify that every aggregated spend row came from a complete import, because a single partial day can make a campaign look cheaper. Keep campaigns with unattributed bookings outside campaign-level allocation, and reconcile them separately against the clinic’s confirmed-booking total. A lower attributed cost per booking can reflect better source matching rather than any change in campaign performance.
This report measures confirmed bookings. If you also care about attended consultations or new paying patients, build those as distinct outcomes and pick a window appropriate to each one. An initial booking can help explain how quickly a campaign generates interest. Attendance and patient starts answer different questions about what that spend produced.
At Wellness Practice Marketing, we work with functional and integrative medicine clinics on Google Ads and the path from search to booking. A cohort report like this gives a clinic a way to judge campaign spend after prospective patients have had time to book.