这是我的代码,错误在最后。
我正在尝试按国家/地区获取 ga 跟踪器代码的分析数据
在网站上
<script async src="https://www.googletagmanager.com/gtag/js?id=G-2YFZD9DYTB"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-2YFZD9DYTB');
</script>
代码(已更新)
// Set up Google Client
$client = new Google_Client();
// Disable SSL verification for the Guzzle HTTP client
$httpClientConfig = [
'verify' => false, // This disables SSL verification
];
$httpClient = new Client($httpClientConfig);
$client->setHttpClient($httpClient);
$client->setApplicationName('lookn-387014');
$client->setAuthConfig(__DIR__ . '/account-key.json');
$client->setScopes([
AnalyticsReporting::ANALYTICS_READONLY
]);
$client->setAccessType('offline');
// Analytics Data
$analyticsData = new AnalyticsData($client);
$propertyId = '404314478'; // GA4 property ID
$dateRanges = new DateRange();
$dateRanges->setStartDate(Carbon::now()->subYear()->format('Y-m-d')); // Replace with your desired start date
$dateRanges->setEndDate(Carbon::now()->format('Y-m-d')); // Replace with your desired end date
// Metric
$metric = new Metric();
$metric->setExpression('metrics/sessions'); // Replace with your desired metric
// Dimension
$dimension = new Dimension();
$dimension->setName('ga:country');
// Request
$request = new RunReportRequest();
$request->setProperty('property', 'properties/' . $propertyId);
$request->setDateRanges([$dateRanges]);
$request->setDimensions([$dimension]); // Set dimensions
$request->setMetrics([$metric]);
$response = $analyticsData->properties->runReport($propertyId, $request);
错误
{"error":"invalid_grant","error_description":"Invalid JWT Signature."}
服务帐户密钥
我的钥匙是全新且有效的
更多信息
在:属性访问管理我的服务帐户选择了“管理员”标准角色
您应该使用的 API 称为
Google Analytics Data API (GA4)
来源:GA 数据 API