ozdemir/analytics

Analytics Package for Google Analytics Data API V4

dev-main 2021-05-23 19:59 UTC

This package is auto-updated.

Last update: 2024-04-24 02:10:49 UTC


README

Analytics Package for Google Analytics Data API V4

Installation

Run

$ composer require ozdemir/analytics

Usage

# Main Library
use Ozdemir\Analytics\Analytics;
use Ozdemir\Analytics\Period;

# Optional reports, you can use these or create your own custom requests.
use Ozdemir\Analytics\Requests\PageViewsByCountry;
use Ozdemir\Analytics\Requests\PageViewsByReferer;

use Ozdemir\Analytics\Requests\PageViews;
use Ozdemir\Analytics\Requests\PageViewsAndUsers;
use Ozdemir\Analytics\Requests\TopBrowsers;
use Ozdemir\Analytics\Requests\TotalUsersByChannels;
use Ozdemir\Analytics\Requests\MostVisitedPages;


$config = [
    'property' => '11111111', // Google Analytics Property ID
    'credentials' => __DIR__ . '/credentials.json'  // credentials.json file path or config array
];

# we load the library
$analytics = new Analytics($config);

# Requests are defined in an array
# Up to 5 requests can be executed in one batch request.
# It is set by Google, so we can't increase this at the moment.
$response = $analytics->fetch([
    new PageViewsByReferer(
        Period::days(7)
    ),

    new PageViewsByCountry(
        Period::days(7),
        5   # row limit, only returns 5 rows
    ),
]);

# We can use the response value as we want.this is quite configurable.
# get($index): index number is the same order of the defined requests above.
# we can also cache the output as we want. It is very easy with the laravel.
return [
    'pageViews' => $response->get(0)->toChartJs(),  // ->toJson() is also available.
    'pageViewsByCountry' => $response->get(1)->toChartJs(),
];

todo:

  • make a better readme / tutorial
  • add unit tests