GoogleAnalytics BetaAnalyticsDataClient 获取 INVALID_ARGUMENT:需要日期范围

问题描述 投票:0回答:1

我尝试从 Google api 获取数据并收到错误

{ "message": "A dateRange is required.", "code": 3, "status": "INVALID_ARGUMENT", "details": [] }

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Metric;
use Google\ApiCore\ValidationException;

try {
    $client = new BetaAnalyticsDataClient([
        'credentials' => __DIR__ . '/client_secrets.json',
    ]);
} catch (ValidationException $e) {
    echo $e->getMessage();
    exit;
}

$dateRange = new DateRange([
    'start_date' => '2024-04-01',
    'end_date' => '2024-04-12',
]);

$metricSessions = new Metric([
    'name' => 'sessions',
]);

$metricPageViews = new Metric([
    'name' => 'screenPageViews',
]);

try {
    $response = $client->runReport([
        'property' => 'properties/********',
        'date_ranges' => [$dateRange],
        'metrics' => [$metricSessions, $metricPageViews],
    ]);

    foreach ($response->getRows() as $row) {
        foreach ($row->getDimensionValues() as $dimensionValue) {
            print 'Dimension Value: ' . $dimensionValue->getValue() . PHP_EOL;
        }
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

这不是一个小错误,因为谷歌不支持它,并且已经关闭了 V4 的文档,并且没有明显的解决方案。也许有人有新的解决方案?

php google-analytics-api google-api-php-client
1个回答
0
投票

我想知道您的 DateRange 对象类型是否是正确的对象类型。试试这个。

<?php
require 'vendor/autoload.php';


$service_account_key_file_path = "C:\Development\FreeLance\GoogleSamples\Credentials/ServiceAccountCred.json";

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;

try {

    // Authenticate using a keyfile path
    $client = new BetaAnalyticsDataClient([
        'credentials' => $service_account_key_file_path
    ]);

    // CHANGE THIS
    $ga4_property = '250796939';

    // Set the date range for the last 90 days.
    $start_date = date('Y-m-d', strtotime('-90 days'));
    $end_date = date('Y-m-d');
    $date_range = new \Google\Analytics\Data\V1beta\DateRange(['start_date' => $start_date, 'end_date' => $end_date]);
    $date_ranges = [$date_range];


    // Set the dimensions and metrics for the request.
    $dimensions = [new \Google\Analytics\Data\V1beta\Dimension(['name' => 'pagePath'])];
    $metrics = [
        new \Google\Analytics\Data\V1beta\Metric(['name' => 'activeUsers']),
    ];

    $response = $client->runReport([
        'property' => 'properties/' . $ga4_property,
        'dateRanges' => $date_ranges,
        'dimensions' => $dimensions,
        'metrics' => $metrics,
    ]);

    foreach ($response->getRows() as $row) {
        foreach ($row->getDimensionValues() as $dimensionValue) {
            print 'Dimension Value: ' . $dimensionValue->getValue() . PHP_EOL;
        }
        foreach ($row->getMetricValues() as $metricValue) {
            print 'Metric Value: ' . $metricValue->getValue() . PHP_EOL;
        }
    }

} catch (\Google\ApiCore\ValidationException $e) {
    printf($e);
}
© www.soinside.com 2019 - 2024. All rights reserved.