工作
Configuring GSC, GA4, PageSpeed & CrUX API Access for Coding Agents
Complete configuration protocol for integrating Google Search Console, GA4, PageSpeed Insights, and CrUX APIs with AI coding agents.
使用这个提示词
# Complete Guide: GSC, GA4, PageSpeed Insights & CrUX API Integration for AI Agents
## Overview
This protocol sets up automated, read-only analytics data pipelines allowing AI coding assistants to fetch search query impressions, page performance metrics, Core Web Vitals (CrUX), and traffic analytics directly via CLI scripts.
---
## 1. Google Cloud Project & Service Account Setup
1. Create or select a Google Cloud Project in Google Cloud Console.
2. Enable the following APIs:
- **Google Search Console API** (`searchconsole.googleapis.com`)
- **Google Analytics Data API v1** (`analyticsdata.googleapis.com`)
- **PageSpeed Insights API** (`pagespeedonline.googleapis.com`)
- **Chrome UX Report API** (`chromeuxreport.googleapis.com`)
3. Create a Service Account (e.g. `agent-analytics-reader@project.iam.gserviceaccount.com`).
4. Generate and download a JSON Private Key. Place it in a secure location (e.g., `~/.credentials/google-service-account.json`, permissions `0600`).
---
## 2. Property Permission Delegations
- **Search Console:** Go to GSC -> Settings -> Users and Permissions -> Add User (`agent-analytics-reader@...`) with **Restricted (Read-Only)** access.
- **Google Analytics 4:** Go to GA4 Admin -> Property Access Management -> Add User with **Viewer** role.
---
## 3. Node.js Script Template for Multi-Source Fetching
```javascript
import { google } from 'googleapis';
import fs from 'node:fs';
const auth = new google.auth.GoogleAuth({
keyFile: process.env.GOOGLE_APPLICATION_CREDENTIALS,
scopes: [
'https://www.googleapis.com/auth/webmasters.readonly',
'https://www.googleapis.com/auth/analytics.readonly',
],
});
// Fetch Search Console Top Queries
export async function getTopQueries(siteUrl, days = 28) {
const searchconsole = google.searchconsole({ version: 'v1', auth });
const startDate = new Date(Date.now() - days * 86400000).toISOString().slice(0, 10);
const endDate = new Date().toISOString().slice(0, 10);
const res = await searchconsole.searchanalytics.query({
siteUrl,
requestBody: {
startDate,
endDate,
dimensions: ['query'],
rowLimit: 25,
},
});
return res.data.rows || [];
}
```
---
## 4. PageSpeed Insights & CrUX Direct REST Invocations
PageSpeed and CrUX can be queried directly with an API key:
```bash
# PageSpeed Insights
curl -s "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://yepsoso.com&strategy=mobile&key=$PSI_API_KEY" | jq '.lighthouseResult.categories'
# Chrome UX Report (CrUX)
curl -s -X POST "https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=$PSI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"origin": "https://yepsoso.com", "formFactor": "PHONE"}' | jq '.record.metrics'
```
还没有评论,可以先留下一条有用的补充。