I started using Google Analytics 4 Data API and downloaded the PHP library to create the requests. I have been playing a little and my request is working good so far, but when I need to sort it I don't know exactly how to pass that data, I have been trying many ways but no luck.
Check "orderBys" data, there I supposed to pass orderType and dimensionName to filter by dimensions date so it should be something like "ordertype" => ALPHANUMERIC and "dimensionName => "date"
Any tip would be much appreciated :)
$response = $client->runReport([
'property' => 'properties/' . $property_id,
'dateRanges' => [
new DateRange([
'start_date' => '7daysAgo',
'end_date' => 'yesterday',
]),
],
'dimensions' => [new Dimension(
['name' => 'day']
),
],
'metrics' => [
new Metric(['name' => 'newUsers']),
new Metric(['name' => 'active7DayUsers']),
],
'orderBys' => [],
]);
This works for me:
Note that is used the V1beta package
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
$response = $client->runReport([
// ...
'orderBys' => [
new OrderBy([
'dimension' => new OrderBy\DimensionOrderBy([
'dimension_name' => 'month', // your dimension here
'order_type' => OrderBy\DimensionOrderBy\OrderType::ALPHANUMERIC
]),
'desc' => false,
]),
],
]);
I had similar struggle, the documentation is really bad.