You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过跟踪ID而非账号ID调用Google Analytics API v4获取报告?

Can I use a UA tracking ID with Google Analytics API v4 in PHP?

Absolutely, you can use a UA tracking ID (like UA-xxxxxx-2) to pull analytics reports via the Google Analytics API v4 in PHP—though there are a few key details to get right, since the API doesn’t directly use the UA ID as the primary identifier for report requests. Here’s how to make it work:

Step 1: Map your UA tracking ID to a View ID

The Analytics Reporting API v4 relies on a View ID (a numeric string) to target data, rather than the UA tracking ID itself. The UA ID is a property-level identifier, while the View ID corresponds to a specific data view under that property. To get the View ID:

  • Head to your old Universal Analytics dashboard, navigate to Admin > View > View Settings, and you’ll find the View ID listed there (it’ll look like 12345678).
  • If you want to automate this, use the Google Analytics Management API: query the accounts/{accountId}/webproperties/{webPropertyId}/profiles endpoint, where webPropertyId is your full UA tracking ID (e.g., UA-xxxxxx-2). This will return all views associated with that UA property, and you can grab the numeric View ID from the results.

Step 2: Update your PHP API request

Swap out the account ID you were using before with the numeric View ID in your report request. Here’s a quick snippet example to illustrate:

use Google_Service_AnalyticsReporting_ReportRequest;

// Initialize your Google client (same as before)
$client = /* your existing client setup */;
$analytics = new Google_Service_AnalyticsReporting($client);

// Build the report request with the View ID
$request = new ReportRequest();
$request->setViewId('12345678'); // Replace with your View ID from Step 1
$request->setDateRanges([/* your date range setup */]);
$request->setDimensions([/* your dimensions */]);
$request->setMetrics([/* your metrics */]);

// Execute the request (same as your existing code)
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests([$request]);
$response = $analytics->reports->batchGet($body);

Important Notes

  • UA data is historical only: Universal Analytics stopped collecting new data in July 2023, so this setup will only let you pull past data. If you need to access ongoing analytics data, you’ll need to migrate to a GA4 property and use the Google Analytics Data API v1 instead.
  • Permissions matter: Make sure the service account or OAuth credentials you’re using have Read & Analyze permissions for the UA property’s view you’re targeting—otherwise, you’ll get authorization errors.
  • Avoid hardcoding: If you manage multiple UA properties, consider building a small helper function that fetches the View ID automatically using the Management API instead of hardcoding values.

内容的提问来源于stack exchange,提问作者Abduhafiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:33:33