Google Analytics

[PHP] Fetch Multiple Metrics Based FROM Google Analytics Data API (GA4)

mysop 2022. 8. 2. 17:24
반응형

애널리틱스 데이터를 가져오는 방법중

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;

/**
 * TODO(developer): Replace this variable with your Google Analytics 4
 *   property ID before running the sample.
 */
$property_id = 'YOUR-GA4-PROPERTY-ID';

// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();

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

// Print results of an API call.
print 'Report result: ' . PHP_EOL;

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

출처 : https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries

 

그런데 디멘션에서 도시와 페이지제목 메트릭에서는 조회수만 가져오려고 한다.

 

디멘션에서 두가지를 동시에 가져오는 방법은 아래와 같다

 

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;

/**
 * TODO(developer): Replace this variable with your Google Analytics 4
 *   property ID before running the sample.
 */
$property_id = 'YOUR-GA4-PROPERTY-ID';

// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();

// Make an API call.
$response = $client->runReport([
    'property' => 'properties/' . $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => '2020-03-31',
            'end_date' => 'today',
        ]),
    ],
    'dimensions' => [
        new Dimension([
            "name" => "unifiedScreenName"
        ]),
        new Dimension([
            'name' => 'city',
        ])
    ],
    'metrics' => [new Metric(
        [
            'name' => 'activeUsers',
        ]
    )
    ]
]);

// Print results of an API call.
print 'Report result: ' . PHP_EOL;

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

중간 dimensions 부분이 추가된 부분이다.

 

출처 : https://stackoverflow.com/questions/67833346/fetch-multiple-metrics-based-from-google-analytics-data-api-ga4

 

Fetch Multiple Metrics Based FROM Google Analytics Data API (GA4)

I'm using Google's Data API for fetching different types of data from metrics and dimensions. But for some cases my dimension is same as date and want to fetch multiple metrics based on same dimens...

stackoverflow.com

 

간단한 방법인데 잘 모르면 고생한다... 

 

 

반응형