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

Viewed 529

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?

1 Answers

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