Repository navigation
feat(integrations): sync AI-sourced traffic, conversions and revenue from GA4 - #718
Merged
Merged
Conversation
…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.
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
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:
ga_ai_traffic_statsga_page_statsga_item_statsga_page_statsis 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.userEngagementDurationwas 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_statscarries 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.devsubdomain 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:
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.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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Automated —
cd server && yarn test(227 pass; 22 new insrc/lib/ga-sync.test.jscovering source classification, date conversion, header-name reading, the three row mappers, the day range and unknown-source reporting).Against a connected property:
POST /api/integrations/google-analytics/syncas an admin or manager. The first call backfills 90 days.firstflips to false, only the last few days are rewritten, and row counts stay equal to distinct natural keys.Verified against a live connected property before opening this:
count(*)equalscount(DISTINCT natural key)in every table — re-running updates rather than duplicates./→ 68 of 3409; a blog post → 10 of 32).Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromserver/)yarn formatpasses (run fromserver/)supabase/schema.sqlregenerated viabash supabase/build-schema.sh