Skip to content

feat(integrations): sync AI-sourced traffic, conversions and revenue from GA4 - #718

Merged
gkhngyk merged 2 commits into
mainfrom
feature/704-ga-traffic-sync
Aug 13, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
feature/704-ga-traffic-sync

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Phase 2 (#694) mapped each brand to a GA4 property; nothing read it, so the connection produced no data. This adds the pipeline — a daily per-brand sync mirroring the Search Console one (#644), with no UI.

Three tables, because they answer different questions and must never be summed:

table question
ga_ai_traffic_stats what AI sources brought, per landing page
ga_page_stats what a landing page is worth across every source
ga_item_stats what actually sold

ga_page_stats is an addition to the issue as filed, agreed in the thread above: the AI slice can say what AI brought but not what a page is worth, so every downstream idea that ranks pages by commercial value needs the totals. userEngagementDuration was added for the same reason. Both are schema decisions, which is why they land now rather than after a 90-day backfill exists.

ga_item_stats carries no page dimension on purpose — a GA4 purchase fires on checkout, so pairing item metrics with a page would attribute every sale to /checkout.

Source classification is an explicit list, not a pattern. Both halves of that matter in real data: one engine arrives as several source strings (chatgpt.com, chatgpt, openai), and a *.workers.dev subdomain containing "claude" is an ordinary referrer that substring matching would have booked as AI traffic. The raw string is stored beside the normalised platform, and one cheap daily call logs sources the list does not recognise so it can grow from observed data instead of guesswork.

Volume is bounded where it can run away. A large shop with tens of thousands of pages taking traffic daily would otherwise write millions of rows per brand:

  • The page table keys on the path without its query string. On a live property that alone was 8.8× fewer distinct pages (2118 → 241); a shop with faceted filters is far worse. The AI table keeps the full entry URL — it is small enough to afford it — plus the path, so the two join.
  • Each day is fetched separately with a ceiling on rows kept (GA_PAGE_DAILY_LIMIT, default 5000). Per day rather than per window, because a ceiling applied to a 90-day range means "the top pages of the quarter" — on a large property less than one day of data, and it silently biases everything computed from it. A day that hits the ceiling is logged, never silently truncated.
  • Rows are ordered transactions → keyEvents → sessions, so a page with three sessions and two purchases outranks five thousand busier pages instead of being cut. That page is exactly the one later phases are looking for.

Related issue

Closes #704

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Automated — cd server && yarn test (227 pass; 22 new in src/lib/ga-sync.test.js covering source classification, date conversion, header-name reading, the three row mappers, the day range and unknown-source reporting).

Against a connected property:

  1. Map a brand to a GA4 property in Settings → Integrations.
  2. POST /api/integrations/google-analytics/sync as an admin or manager. The first call backfills 90 days.
  3. Call it again — first flips to false, only the last few days are rewritten, and row counts stay equal to distinct natural keys.
  4. A property without ecommerce returns no item rows; that path logs and continues rather than failing the brand.

Verified against a live connected property before opening this:

  • First sync stored 96 AI rows and 1570 page rows across 91 days in 70s; the refresh run rewrote 4 days in 6s.
  • After both runs, count(*) equals count(DISTINCT natural key) in every table — re-running updates rather than duplicates.
  • Joining the two traffic tables on the path resolves each AI landing page to its full session count (/ → 68 of 3409; a blog post → 10 of 32).
  • The item report returned nothing, which is the no-ecommerce path.
  • GA4 quota is not a constraint: the heaviest report cost 3 tokens of a 200,000 daily allowance, and the allowance is per property.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from server/)
  • yarn format passes (run from server/)
  • supabase/schema.sql regenerated via bash supabase/build-schema.sh
  • Changes are focused — one concern per PR

…from GA4

Phase 2 mapped each brand to a GA4 property but nothing read it, so the
connection produced no data. This is the pipeline: a daily per-brand sync
mirroring the Search Console one, with no UI.

Three tables, because they answer different questions and must never be
added together. ga_ai_traffic_stats is what AI sources brought, per landing
page. ga_page_stats is what a landing page is worth across every source —
"high business value, weak AI visibility" cannot be computed from the AI
slice alone. ga_item_stats is what actually sold, kept page-free on purpose:
a GA4 purchase fires on checkout, so pairing item metrics with a page
dimension would book every sale to /checkout.

Sources are classified from an explicit list rather than a pattern. Real
data shows both halves matter: one engine arrives as several source strings,
and a workers.dev subdomain containing "claude" is an ordinary referrer that
substring matching would have counted as AI traffic. The raw string is stored
next to the normalised platform, and a cheap daily call logs sources the list
does not know so it can grow from observed data.

Volume is bounded where it can run away. The page table keys on the path
without its query string — on a live property that alone was 8.8x fewer
distinct pages, and a shop with faceted filters is far worse — and each day
is fetched separately with a ceiling on rows kept. Per day rather than per
window, because a ceiling applied to a 90-day range means "the top pages of
the quarter", which on a large property is less than one day of data and
silently biases everything computed from it. Rows are ordered transactions
first, then key events, then sessions, so a page with three sessions and two
purchases outranks five thousand busier pages instead of being cut.

Verified against a live connected property: first sync stored 96 AI rows and
1570 page rows across 91 days in 70s; the refresh run detected it was not the
first, rewrote 4 days in 6s, and left row counts equal to distinct natural
keys — re-running updates rather than duplicates. The item report returned
nothing, which is the no-ecommerce path and not an error. Joining the two
traffic tables on the path resolves each AI landing page to its full session
count. GA4 quota is not a constraint: the heaviest report cost 3 tokens of a
200,000 daily allowance, per property.

Closes #704
…vars

They are product constants, not deployment configuration: there is no
environment where a different value is correct, and nothing about the value
depends on where the server runs. Exposing them added two more entries to an
env surface that already carries five Cloro knobs, each one a thing to
document and to drift out of date.

If a property ever fills the ceiling — which is logged, not silent — the
useful response is to look at what is being stored rather than to raise the
number, and that was always going to be a code change.
@gkhngyk
gkhngyk merged commit 4a5fc81 into main Aug 13, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the feature/704-ga-traffic-sync branch August 13, 2026 18:14
@gkhngyk gkhngyk mentioned this pull request Aug 24, 2026
7 of 12 tasks
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.

Integrations: sync AI-sourced traffic, conversions and revenue from GA4 — Google Analytics Phase 3

1 participant