Claude Code 如何在本機免費連接 Google Analytics 4 與 Search Console(約需 15 分鐘)
How to connect Google Analytics 4 and Search Console to Claude Code (free, local, 15 minutes)
KKV Labs 開源 ga4-gsc-mcp,讓 Claude Code 在本機以唯讀方式查閲 Google Analytics 4(GA4)與 Google Search Console(GSC)資料。
I run a few websites. Every week I opened Google Analytics 4 (GA4) in one tab and Google Search Console (GSC) in another, then tried to match the numbers by hand. Which page lost clicks? Did that page also lose sign-ups? It was slow and I made mistakes.
So we built an open-source MCP server that lets Claude Code read both. It is called ga4-gsc-mcp. It is free, MIT licensed, read-only, and it runs on your own machine with your own Google credentials. Disclosure up front: I'm Karthik Varma, founder of KKV Labs, and KKV Labs made this. I tested it live on my own sites before release.
This guide takes you from zero to asking Claude Code "why did my Google traffic drop?" The one slow part is creating a Google Cloud OAuth client. That takes about 10 to 15 minutes the first time and you only do it once.
What you get
The server adds 36 read-only tools to Claude Code:
- Google Search Console (GSC): top queries, top pages, quick wins, traffic drops, why traffic dropped (with Google ranking updates in the window), keyword cannibalization, content decay, top movers, URL inspection and sitemaps.
- Google Analytics 4 (GA4): traffic sources, which platform sent fewer visitors, attribution, AI assistant traffic (ChatGPT, Perplexity, Claude, Gemini, Copilot), funnels, realtime, and raw GA4 reports.
- PageSpeed Insights: Core Web Vitals and a Lighthouse score.
- Bing Webmaster Tools: clicks, queries, pages, crawl issues and backlinks, if you add your own Bing API key.
Some tools join the sources. top_pages puts GSC clicks next to GA4 landing page data, so you can see search clicks and on-site results in one row.
It works in Claude Code, Claude Desktop and Cursor, plus any MCP client that runs a local stdio server. It does not work in claude.ai or ChatGPT on the web, because those cannot start a process on your computer.
Step 0: try it with fake data first
You need Node.js 20 or newer. Before you touch Google Cloud, check that the server runs. Demo mode uses built-in fake data for example.com and makes no network calls:
GA4_GSC_MCP_DEMO=1 npx -y ga4-gsc-mcp check
If that prints a list of properties and sites, Node and npx are fine.
Shortcut: npx ga4-gsc-mcp setup walks you through steps 1 to 4 in the terminal. It opens each Google Cloud page in order, signs you in, checks access and offers to write the config for Claude Desktop or Cursor (it backs up your config first and asks before writing). The manual steps below show what it does.
Step 1: enable three Google APIs
Pick a Google Cloud project you own, or create a new one. Then enable the Google Analytics Admin API, the Google Analytics Data API and the Google Search Console API. You can click them on in the API library, or use gcloud:
gcloud services enable analyticsadmin.googleapis.com analyticsdata.googleapis.com searchconsole.googleapis.com --project=YOUR_PROJECT_ID
The Admin API lists your properties. The Data API runs reports. The Search Console API covers search analytics and URL inspection.
Step 2: create a desktop OAuth client
The README offers three ways to sign in. I recommend the one-time browser login, because the saved token holds nothing but two read-only scopes.
- In Google Cloud, open Google Auth Platform (APIs & Services > OAuth consent screen). Set it up as External and add your own Google account under Test users. On Google Workspace you can pick Internal instead.
- Go to Clients (APIs & Services > Credentials) > Create client > Desktop app. Download the JSON file.
Step 3: sign in once
npx -y ga4-gsc-mcp auth --client-secret ~/Downloads/client_secret_XXXX.json
Your browser opens. You approve read-only access. A refresh token is saved to ~/.config/ga4-gsc-mcp/oauth-token.json (%APPDATA%\ga4-gsc-mcp on Windows) with file mode 600.
One catch. While your consent screen is in Testing, Google expires refresh tokens after 7 days. To keep the login, switch the publishing status to In production. For an app only you use, Google shows a warning screen you can click through. Or just run auth again each week.
Now check your real credentials:
npx -y ga4-gsc-mcp check
It prints which credentials it used and every GA4 property and GSC site they can see. If your site is missing here, it will be missing in Claude too.
Step 4: add it to Claude Code
claude mcp add ga4-gsc --scope user -- npx -y ga4-gsc-mcp
Start Claude Code and run /mcp. You should see ga4-gsc connected.
Want Bing too? Generate a key in Bing Webmaster Tools under Settings > API Access, then add the server with the key in its environment:
claude mcp add ga4-gsc --scope user -e BING_WEBMASTER_API_KEY=your-key -- npx -y ga4-gsc-mcp
The bing_ tools only appear when that key is set.
Step 5: ask questions
Here are five questions from the README and what comes back. Every answer starts with a receipt: the GA4 property, the GSC site, the exact dates, the comparison range and any caveats. So you always know what the numbers cover.
1. "How is example.com doing compared with last month?"
This runs site_overview. You get sessions, users, key events and engagement from GA4, plus clicks, impressions, CTR and average position from GSC. Each comes with its change. It also flags possible bot or spam traffic.
2. "Why did my Google traffic drop last month? Was there a Google update?"
This runs why_did_it_drop. It splits the clicks change into impressions and CTR, finds the start date, lists the cause per page and query, checks index status, and shows Google ranking updates from the Search Status Dashboard that fall in the window.
3. "Give me 10 SEO quick wins for example.com with the page to fix."
This runs quick_wins. It finds queries at positions 4 to 20 with real impressions, ranks them by the extra clicks you could gain, and shows your CTR by position against a benchmark.
4. "Which of my pages compete for the same keywords?"
This runs find_cannibalization. It lists queries split between two or more of your pages and suggests which page to keep.
5. "How many visits came from ChatGPT, Perplexity, Claude, Gemini and Copilot in the last 90 days, and where did they land?"
This runs ai_traffic on GA4 and returns visits by AI assistant with their landing pages.
When the ready-made tools don't fit, ask for something specific, like "Sessions by country and device for October." Claude will use run_ga4_report or search_console_query, which expose the full GA4 Data API and Search Analytics API. Tables can come back as CSV.
Troubleshooting
Run npx -y ga4-gsc-mcp check first. Most problems show up there. The common ones:
- "API has not been used in project ... or it is disabled": enable the three APIs from Step 1 in the project your credentials belong to. Wait a minute and retry.
- "Access blocked: ... has not completed the Google verification process": add your account under Test users on the consent screen.
-
invalid_grantafter about a week: the consent screen is in Testing, so the token expired. Publish it to In production, or runauthagain. - 403 "User does not have sufficient permissions": the signed-in account is not a user on that GA4 property or GSC site.
-
"No Search Console site matches ...": use the exact site from
list_properties. Domain properties look likesc-domain:example.com. URL-prefix properties look likehttps://www.example.com/. -
"No GA4 property's web stream matches ...": pass
ga4_property(the numeric ID from GA4 Admin > Property details), or setGA4_PROPERTY_ID. -
Numbers differ from the Search Console UI: the UI includes the last 2 to 3 unsettled days. This server ends on settled days by default. Pass
end_dateto match the UI exactly.
Honest limits
- Local only. It runs as a stdio process on your computer. It does not work in claude.ai or ChatGPT on the web.
- You need a Google Cloud project. That OAuth client setup is the slow part, and Testing mode tokens expire after 7 days unless you publish the consent screen.
- Search Console lags. GSC data settles 2 to 3 days late, and GA4 about 1 day late. Default ranges end on settled days, so "last 7 days" will not include today.
- Query tables don't add up to the total. Search Console leaves anonymized queries out of query and page tables.
- Quotas apply. GA4 and Search Console have per-project quotas. URL Inspection allows about 2,000 a day and 600 a minute per site.
- Bing keeps about 6 months of history and reports queries and pages by week.
-
Funnels are beta.
ga4_funneluses Google's v1alpha funnel API, which can change. - Tool output is untrusted text. Search queries and backlink anchor text are written by other people. Keep Claude Code's confirmation prompts on for tools that reach other hosts.
On security
Calls go only to Google's and Bing's APIs over HTTPS, through one guard with a fixed host allow list. There is no telemetry. Tokens and API keys are redacted from errors and logs. The full list is in the README.
Links
The code, issues and changelog are on GitHub: https://github.com/kkvlabs/ga4-gsc-mcp. Version 0.3.0 adds a npx ga4-gsc-mcp setup wizard and a one-click Claude Desktop install (download ga4-gsc-mcp.mcpb from the latest GitHub release). Issues and pull requests are welcome.
If you'd rather skip the Google Cloud setup, or you want this in claude.ai or ChatGPT on the web, the same team runs a hosted version: Site Insights by KKV Labs. You sign in with Google. The free plan covers 1 site and 20 lookups a day.
來源:DEV Community · dev.to