简体   繁体   English

通过 oauth2(同意屏幕)从 Google Analytics Data API (Ga4) 中提取数据

[英]Extract data from Google Analytics Data API (Ga4) via oauth2 (consent screen)

Is it possible to extract data from Google Analytics Data API (GA4 accounts) not via service account?是否可以不通过服务帐户从Google Analytics Data API (GA4 帐户)中提取数据? I can extract normally using service accounts (example below), but I needed authorization via oauth (consent screen) and I found absolutely nothing related.我可以使用服务帐户正常提取(下面的示例),但我需要通过 oauth(同意屏幕)授权,我发现完全没有相关。

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

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;

$client = new BetaAnalyticsDataClient(['credentials' => 'MY-CREDENTIALS.json']);

$response = $client->runReport([
    'property' => 'properties/MY-ID',
    'dateRanges' => [
        new DateRange([
            'start_date' => '2020-03-31',
            'end_date' => 'today',
        ]),
    ],
    'dimensions' => [new Dimension(
        [
            'name' => 'city',
        ]
    ),
    ],
    'metrics' => [new Metric(
        [
            'name' => 'activeUsers',
        ]
    )
    ]
]);

print 'Report result: ' . PHP_EOL;

foreach ($response->getRows() as $row) {
    print $row->getDimensionValues()[0]->getValue()
        . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
}

I got it as follows:我得到它如下:

$client = new BetaAnalyticsDataClient( [
    'credentials' => Google\ApiCore\CredentialsWrapper::build( [
        'scopes'  => [
            'https://www.googleapis.com/auth/analytics',
            'openid',
            'https://www.googleapis.com/auth/analytics.readonly',
        ],
        'keyFile' => [
            'type'          => 'authorized_user',
            'client_id'     => 'MY-CLIENT-ID',
            'client_secret' => 'MY-CLIENT-SECRET',
            'refresh_token' => 'REFRESH-TOKEN' // https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token
        ],
    ] ),
] );

for nodejs and as per sample on github of the library对于 nodejs 并根据库的 github 上的示例

const oAuth2Client = await getOAuth2Client();
const analyticsDataClient = getAnalyticsDataClient(oAuth2Client);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ARRAY BigQuery 中更新 ARRAY(GA4 数据) - Update ARRAY IN ARRAY BigQuery (GA4 Data) 并非所有 [GA4] BigQuery Export 架构都无法通过 GA4 API 获得? - Not all [GA4] BigQuery Export schema not available via GA4 API? CWV 数据的问题 - GA4 + BigQuery + Data Studio - Problems with CWV data - GA4 + BigQuery + Data Studio GA4流量源数据与bigquery不匹配 - GA4 traffic source data do not match with bigquery BigQuery 中的 GA4 数据:如何复制参与率? - GA4 data in BigQuery: How to replicate the engagement rate? GA4 BigQuery 屏幕分辨率 - GA4 BigQuery screen resolution 如何从 google Analytics Api 获取记录的每个事件的数据? - how to get from google Analytics Api data for each event recorded? 将自定义文本添加到 Google Cloud 中的 oAuth 同意屏幕 - Adding custom text to oAuth consent screen in Google Cloud 有谁有将 GA4 日志数据加载到 Supabase 数据库的经验吗? - Does anyone who has experience with loading GA4 log data to Supabase database? ga4 使用什么方法将数据流式传输到 bigquery? 在SQL条款中,它只是插入还是更新? - What method ga4 use for streaming data to bigquery? In SQL terms, is it just insert or update too?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM