
如何使用 DeepSeek 構建 AI Agent:終極指南
核心關鍵詞:Analytics?SEO?API、網站流量分析、轉化率分析、GA4 Data API、Search Console API、自動化報告、Conversion Rate、關鍵詞分析、AIOps SEO
環境變量:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json"
安裝 SDK:
pip install google-analytics-data google-api-python-client
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import RunReportRequest, DateRange, Dimension, Metric
client = BetaAnalyticsDataClient()
request = RunReportRequest(
property="properties/123456789",
date_ranges=[DateRange(start_date="2025-06-01", end_date="2025-06-30")],
dimensions=[Dimension(name="sessionDefaultChannelGroup")],
metrics=[Metric(name="sessions"), Metric(name="pageViews"), Metric(name="bounceRate")],
)
response = client.run_report(request)
for row in response.rows:
channel = row.dimension_values[0].value
sessions = row.metric_values[0].value
views = row.metric_values[1].value
bounce = row.metric_values[2].value
print(f"{channel}: Sessions={sessions}, PageViews={views}, BounceRate={bounce}%")
sessionDefaultChannelGroup
、pagePath
、country
sessions
、pageViews
、bounceRate
dimensions=[Dimension("pagePath"), Dimension("queryString")]
,精確拆分 SEO 關鍵詞帶來流量。metricFilter
或在代碼中過濾出 sessions > 1000 的頁面列表,定位潛力頁面。在 GA4 中預先配置 purchase
、lead_form
等 轉化事件,并在 API 請求中添加:
from google.analytics.data_v1beta.types import FilterExpression, Filter
request.dimension_filter = FilterExpression(
filter=Filter(field_name="eventName", string_filter={"value": "purchase"})
)
request.metrics.append(Metric(name="eventCount"))
CR 計算:
cr = int(conversions) / int(sessions) * 100 if int(sessions) else 0
結合渠道維度與頁面維度,輸出如下格式:
Organic Search /product/abc: Sessions=2000, Conversions=50, CR=2.50%
Paid Search /landing/xyz: Sessions=500, Conversions=10, CR=2.00%
from googleapiclient.discovery import build
sc_client = build('searchconsole', 'v1', credentials=creds)
response = sc_client.searchanalytics().query(
siteUrl='https://www.example.com',
body={
"startDate":"2025-06-01","endDate":"2025-06-30",
"dimensions":["query"],
"rowLimit":100
}
).execute()
for row in response['rows']:
print(f"{row['keys'][0]}: Clicks={row['clicks']}, Impressions={row['impressions']}, CTR={row['ctr']:.2%}, Position={row['position']:.2f}")
query
維度,實現 流量轉化率 全鏈路分析。Pipeline 設計:
報告生成:
AIOps 異常檢測:
優化方案:
問題 | 建議方案 |
---|---|
API 配額限制(429 錯誤) | 實現指數退避重試、分批拉取并啟用緩存(Redis) |
數據延遲與實時性 | GA4 數據通常延遲數小時,實時需求可使用 runRealtimeReport |
關鍵詞維度不匹配 | 確保 GA4 與 GSC 中 Query 名稱一致,必要時統一小寫處理 |
數據安全與權限管理 | 服務賬號僅授予最小權限,敏感報告輸出加密存儲 |
本文全面演示了如何利用 Analytics?SEO?API:
下一步,可結合 Google Ads API 與 Third?Party SEO API,構建全渠道 ROI 分析 平臺,實現 SEO 投入產出閉環。
原文引自YouTube視頻:https://www.youtube.com/watch?v=orytoGXgoGQ