6

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.

2
  • you can put more then one metric in a single report Jun 4, 2021 at 8:02
  • 1
    How? I have tried but it doesn't work. and how will i get using below foreach loop? I have updated questions, check now Jun 4, 2021 at 8:14

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.