Skip to content

fix(search-console): count clicks from anonymized searches - #388

Merged
PascaleBeier merged 2 commits into
HitKeep:mainfrom
joshpurvis:fix/search-console-anonymized-clicks
Oct 5, 2026
Merged

PascaleBeier merged 2 commits into
HitKeep:mainfrom
joshpurvis:fix/search-console-anonymized-clicks

Conversation

@joshpurvis

@joshpurvis joshpurvis commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Search Console reports in HitKeep undercount clicks, and on a small site they can show zero. Google leaves anonymized searches out of any Search Analytics response grouped by query, and every HitKeep import was grouped by query.

The sync now imports totals that keep those clicks, and the reports read them. A manual "Sync now" also repairs days imported before this change.

No search text is collected. The new rows are the per-page, per-country and per-device totals Search Console already shows the site owner, and they match its chart. Google still hides the anonymized searches themselves.

What I found

I queried the live API for one property, one day at a time, with each dimension set:

  • Keep every click: date, date,page, date,country, date,device, date,country,device
  • Lose the anonymized clicks: anything with query, and page combined with country or device

A single request that drops query but keeps page, country and device returns the same zeros, so page totals and audience totals need separate requests.

What changed

  • worker/search_console.go: each day makes three requests concurrently, and the day is replaced only after all three succeed:
    • query rows (date,query,page,country,device, as before)
    • page totals (date,page)
    • audience totals (date,country,device)
  • database/store_search_console.go: each report reads one shape of row, so nothing is counted twice:
    • overview and trend: audience totals, or page totals under a page filter
    • pages report: page totals
    • countries and devices reports: audience totals
    • queries report: query rows
    • Days imported before this change fall back to their query rows.
  • The first sync, and a manual sync on an existing install, import the recent seven days plus every older day in the 90-day range that still lacks audience totals, newest first. Each day commits on its own, so a sync cut short (a timeout, or the user leaving the page) skips the finished days next time. This also lets a first import that keeps timing out make progress instead of starting over.
  • New migration 2026_10_04_000000_add_search_console_totals_backfilled_property.sql adds totals_backfilled_property_uri to google_search_console_sync_state. When that import completes, the worker records the property, and later manual syncs go back to the seven-day window. It names the property, so a site remapped to another property gets the same import again. The upsert only ever sets it, so the handler and failure paths that rebuild the row can't clear it.
  • recordSyncFailure now writes with an uncancelled context, with a 10s limit. Before, a sync that hit RunDue's deadline or lost its HTTP caller couldn't record its failure, so the site stayed pending with the manual flag set.
  • A failed sync still clears the manual flag, so background retries keep the normal seven-day window.

Limits

  • A page filter combined with a country or device filter still uses query rows and undercounts. Google won't return that breakdown with anonymized clicks included.
  • Existing installs only catch up on a manual sync, which their users may never click. The next daily sync corrects their last seven days, and every later day is imported with totals. Catching up in the background as well is possible, but it would add Google requests to every install and has to fit RunDue's one-minute budget, so I've left that up to you.
  • Each day now makes three API requests instead of one.

Testing

  • Tests cover each report's row shape, the legacy fallback, the filters, the catch-up, resuming after a failed first import, skipping a completed catch-up, and keeping the recorded property across upserts.
  • The fake Search Console source now blanks the dimensions a request doesn't group by, the way Google does. It can't model Google's privacy filtering, which is why I checked the request shapes against the live API.
  • go test -race ./database ./worker ./server/user passes.
  • Maintainer validation: complete QA passed on the final source snapshot (run 20261005T184731-bae2b14f): go-format, go-fix, go-lint, go-vet, go-staticcheck, go-race-database, go-race-server, go-race-rest, govulncheck.
  • I'm running this on my own instance, on top of 2.14.0.

Contributor Checklist

  • I updated docs or explained why docs are not needed.
  • I updated OpenAPI sources if public API shape changed.
  • I checked MCP read-only/privacy boundaries if MCP behavior changed.
  • I updated Agent Skills if MCP tools, filters, metrics, or guidance changed.
  • I checked GoAI structured-output validation if AI provider or Opportunity behavior changed.
  • I checked the diff for secrets, private deployment details, local-only paths, and customer data.

No docs, OpenAPI or Agent Skills changes. Reports keep the same fields and now show the numbers Search Console shows. MCP, Ask AI and Opportunities read Search Console through the same store functions, so their numbers now include anonymized clicks. The tools stay read-only and aggregate-only, and no search text is added.

Maintainer follow-up

Completed totals imports now checkpoint empty days as well as days with rows. Checkpoints are scoped to site/property/data state and committed with fact replacement, so interrupted initial backfills can progress past empty dates without fabricated analytics rows. Shared and tenant migrations, analytics reset, and tenant-split fixtures include the checkpoint table.

TDD reproduced the empty-day retry failure before the fix. Regression coverage verifies restart/resume, scoped and idempotent replacement, rollback, reset, and tenant transfer. Gortex coverage and guards completed; its contract audit timed out.

The dedicated Search Console workspace and versioned datasets remain staged follow-ups in #261.

Search Console reports undercounted clicks, and on a small site showed zero,
because Google leaves anonymized searches out of any Search Analytics
response grouped by query and every import was grouped by query. The sync
now also imports totals that keep those clicks, the reports read them, and a
manual sync repairs days imported before this change. No search text is
collected.

Checked against the live API: date, date+page, date+country, date+device and
date+country+device keep every click; any set with query, or page combined
with country or device, loses the anonymized ones.

- Worker: each day makes three concurrent requests (query rows, page totals
  by date+page, audience totals by date+country+device) and replaces the day
  only after all succeed.
- Store: each report reads one row shape. Overview and series use audience
  totals (page totals under a page filter), the page report page totals,
  country and device reports audience totals, the query report and
  page+country/device filters query rows. Days without totals fall back to
  query rows.
- Catch-up: the first sync and a manual sync import the recent seven days
  plus every older day in the 90-day range still lacking audience totals,
  newest first, so a sync cut short resumes where it stopped.
- Migration adds google_search_console_sync_state.totals_backfilled_property_uri.
  A completed catch-up records the property; later manual syncs skip it, and
  remapping to another property catches up again. The upsert only sets it.
- recordSyncFailure writes with an uncancelled context (10s limit), so a sync
  that hit its deadline or lost its caller still records the failure and
  clears the manual flag.

Limits: a page filter combined with a country or device filter still
undercounts; existing installs only catch up on a manual sync.
@PascaleBeier
PascaleBeier self-requested a review October 4, 2026 17:18
@PascaleBeier PascaleBeier self-assigned this Oct 4, 2026
@PascaleBeier
PascaleBeier merged commit 15cd7cc into HitKeep:main Oct 5, 2026
17 checks passed
@PascaleBeier

Copy link
Copy Markdown
Collaborator

Thanks @joshpurvis , merged! I only tested everything and added some checkpoints, this is a great piece of work towards #261 <3

@joshpurvis

Copy link
Copy Markdown
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants