Asfandyar Khan
Asfandyar Khan

Reputation: 1738

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 dimension.

Below is my code, Im fetching activeUsers based on current date. I want to fetch multiple metrics like activeUsers, newUsers, sessions using one single API report, I had to call below API 3 times by passing different metrics to fetch data. Any other solution?

$property_id = 'PROPERTY-ID';
$client = new BetaAnalyticsDataClient();

$response = $client->runReport([
    'property' => 'properties/' . $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => '2021-06-01',
            'end_date' => '2021-06-01',
        ]),
    ],
    'dimensions' => [new Dimension(
        [
            'name' => 'date',
        ]
    ),
    ],
    'metrics' => [new Metric(
        [
            'name' => 'activeUsers',
        ]
    )
    ]
]);


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

I have tried sending two metrics using below code:

'metrics' => [new Metric([
                'name' => 'activeUsers',
            ],[
                'name' => 'newUsers',
            ])]

But how will I fetch from response? Currently Im using below to fetch

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

Using $row->getMetricValues()[0]->getValue() returns value of activeUsers but how can I get value of newUsers Because I have used two metrics. I have tried to use $row->getMetricValues()[1]->getValue() this but not working.

Upvotes: 6

Views: 1492

Answers (1)

Vlad Dobrescu
Vlad Dobrescu

Reputation: 21

Here is an example of an API call with 2 dimensions and 2 metrics:

// Make an API call.
$response = $client->runReport([
    'property' => 'properties/' . $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => '2022-06-16',
            'end_date' => 'today',
        ]),
    ],

    'metrics' => [
        new Metric([
            'name' => 'activeUsers',
        ]),
        new Metric([
            "name" => "sessions"
        ])
    ],
    'dimensions' => [
        new Dimension([
            "name" => "city"
        ]),
        new Dimension([
            'name' => 'firstUserSource',
        ])
    ],
]);

Now, in your foreach loop do this:

    foreach ($response->getRows() as $row) {

    $return[] = [
        'city' => $row->getDimensionValues()[0]->getValue(),
        'source' => $row->getDimensionValues()[1]->getValue(),
        'users' => $row->getMetricValues()[0]->getValue(),
        'sessions' => $row->getMetricValues()[1]->getValue(),
    ];
}

Then just json_encode the $return and that's about it.

Upvotes: 2

Related Questions