'How to get analytics report to my web through api in laravel with google analytics 4 property

I created a CMS website and I create google analytics account for get the hits and other data from my website, I am not used the universal analytics , I used the google analytics 4 property , but how to get the analytics data to my website through API in Laravel?



Solution 1:[1]

Have a look at the documentation for the google analytics data api. There is a php client library for it you can use.

require 'vendor/autoload.php';

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;

$client = new BetaAnalyticsDataClient();

$response = $client->runReport([
    'property' => 'properties/[YOUR_PROPERTY_ID]'
]);

foreach ($response->getRows() as $row) {
    foreach ($row->getDimensionValues() as $dimensionValue) {
        print 'Dimension Value: ' . $dimensionValue->getValue() . PHP_EOL;
    }
}

A few things to note.

  1. use a service account this will give you access to your personal account.
  2. Remember this api is in beta and it can change.
  3. Data is returned as json you will need to format it yourself.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DaImTo