Google Analytics Reporting api v3 使用 PHP 获取数据

Google Analytics Reporting api v3 to get data using PHP

我有一个服务帐户可以获取数据,例如 pageviewsusersbounce rate等来自 Google Analytics。我已经在我的开发者电子邮件地址上设置了访问我的 7 属性 数据的权限。

Out of 7, I am able to grab data from 5 properties/sites but other 2 are not working

尽管两个帐户都具有 Google 分析管理 › 用户管理 的访问权限。我正在使用 PHP 中的 Google Analytics V3。以下是我使用的部分代码。

$account_id = 'ga:343434334'; // my view id goes here

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");

$key = file_get_contents($key_file_location);

// seproate additional scopes with a comma
$scopes ="https://www.googleapis.com/auth/analytics.readonly";

$cred = new Google_Auth_AssertionCredentials(
    $Email_address,
    array($scopes),
    $key
);

$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}

$service = new Google_Service_Analytics($client);


 // get page threshold
        $optParams1 = array(
            'dimensions' => 'ga:pageDepth',
            'sort' => '-ga:users',
            'filters' => 'ga:pagePath=@inquiry',
            'max-results' => '1'
        );
$results1 = $service->data_ga->get($account_id, $start_date, $end_date, 'ga:users', $optParams1);
print '<pre>';
print_r(results1);
printt '</pre>';

您似乎正在使用服务帐户进行身份验证。在您缺少访问权限的帐户上,仔细检查您是否在 ACCOUNT 级别授予服务帐户访问权限。这意味着您在 ACCOUNT 级别添加了用户,如果您仅在 Web 属性 或视图级别添加了服务帐户,它将无法工作。