Reputation: 3794
I am trying to get the statistics from my Google Analytics account (GA4) related to Traffic acquisition. Like from where user came to my website, organically or direct or threw social media.
I have added the google/analytics-data
package to this Google document:
https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries
composer require google/analytics-data
I have all the required detail like property id and service account json file.
This is the code i have mange:
putenv("GOOGLE_APPLICATION_CREDENTIALS=$this->config_file_path");
$client = new BetaAnalyticsDataClient();
$credentials = ApplicationDefaultCredentials::getCredentials();
$analyticsData = new AlphaAnalyticsDataClient(['credentials' => $credentials]);
// Create a date range for the report.
$dateRanges = [
new DateRange([
'start_date' => '2023-07-21',
'end_date' => 'today',
]),
];
$dimensions = [new Dimension(
[
'name' => 'source',
],
),
new Dimension(
[
'name' => 'medium',
]
),
new Dimension(
[
'name' => 'country',
]
),
];
$metrics = [new Metric(
[
'name' => 'sessions',
]
),
];
$response = $client->runReport([
'property' => 'properties/' . $this->property_id,
'dateRanges' => $dateRanges,
'dimensions' => $dimensions,
'metrics' => $metrics,
]);
foreach ($response->getRows() as $row) {
echo "<pre>";
print $row->getDimensionValues()[0]->getValue()
. ' ' . $row->getDimensionValues()[1]->getValue()
. ' ' . $row->getDimensionValues()[2]->getValue()
. ' ' . $row->getMetricValues()[0]->getValue(). PHP_EOL;
echo "</pre>";
}
But getting empty result.
Please help me with this or point out where i have to improve my code to get the result, as i can see the detail in my account but not getting threw Google Analytics Data API (GA4)
Upvotes: 0
Views: 957
Reputation: 1
new Dimension(
[
'name' => 'sessionDefaultChannelGroup',
]
),
new Dimension(
[
'name' => 'country',
]
),
];
$metrics = [new Metric(
[
'name' => 'sessions',
]
),
];
$dimensionFilter = new DimensionFilter([
'dimension_name' => 'sessionDefaultChannelGroup',
'operator' => 'EXACT',
'expressions' => ['Organic Search'],
]);
$dimensionFilters = [$dimensionFilter];
$response = $client->runReport([
'property' => 'properties/' . $this->property_id,
'dateRanges' => $dateRanges,
'dimensions' => $dimensions,
'metrics' => $metrics,
'dimensionFilter' => $dimensionFilters,
]);
foreach ($response->getRows() as $row) {
echo "<pre>";
print $row->getDimensionValues()[0]->getValue()
. ' ' . $row->getDimensionValues()[1]->getValue()
. ' ' . $row->getDimensionValues()[2]->getValue()
. ' ' . $row->getMetricValues()[0]->getValue(). PHP_EOL;
echo "</pre>";
}
Explanation : if you want to find Referral Traffic from GA4, you need to add sessionDefaultChannelGroup to your dimensionFilter like below
$dimensionFilter = new DimensionFilter([
'dimension_name' => 'sessionDefaultChannelGroup',
'operator' => 'EXACT',
'expressions' => ['Organic Search'],
]);
Upvotes: -1
Reputation: 117301
Because traffic source uses session source and session medium not session and medium dimensions
Upvotes: -1