1. Go to this page and download the library: Download gtmassey/laravel-analytics library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gtmassey / laravel-analytics example snippets
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Metrics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\Period\Period;
use Carbon\Carbon;
$report = Analytics::query()
->setMetrics(fn(Metrics $metrics) => $metrics
->active1DayUsers()
->active7DayUsers()
->active28DayUsers()
)
->forPeriod(Period::defaultPeriod())
->run();
$report2 = Analytics::query()
->setMetrics(fn(Metrics $metrics) => $metrics->sessions())
->setDimensions(fn(Dimensions $dimensions) => $dimensions->pageTitle())
->forPeriod(Period::create(Carbon::now()->subDays(30), Carbon::now()))
->run();
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Filters\Filter;
use Gtmassey\LaravelAnalytics\Request\Filters\FilterExpression;
use Gtmassey\LaravelAnalytics\Request\Metrics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\Period\Period;
$report = Analytics::query()
->setMetrics(fn(Metrics $metrics) => $metrics->sessions())
->setDimensions(fn(Dimensions $dimensions) => $dimensions->pageTitle())
->forPeriod(Period::defaultPeriod())
->dimensionFilter(fn(FilterExpression $filterExpression) => $filterExpression
->filter('pageTitle', fn(Filter $filter) => $filter->exact('Home'))
)
->run();
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Filters\Filter;
use Gtmassey\LaravelAnalytics\Request\Filters\FilterExpression;
use Gtmassey\LaravelAnalytics\Request\Metrics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\Period\Period;
$report = Analytics::query()
->setMetrics(fn(Metrics $metrics) => $metrics->sessions())
->setDimensions(fn(Dimensions $dimensions) => $dimensions->pageTitle())
->forPeriod(Period::defaultPeriod())
->dimensionFilter(fn(FilterExpression $filterExpression) => $filterExpression
->filterDimension(
dimensionsCallback: fn(Dimensions $dimensions) => $dimensions->pageTitle(),
filter: fn(Filter $filter) => $filter->exact('Home')
)
)
->run();
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Filters\Filter;
use Gtmassey\LaravelAnalytics\Request\Filters\FilterExpression;
use Gtmassey\LaravelAnalytics\Request\Metrics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\Period\Period;
$report = Analytics::query()
->setMetrics(fn(Metrics $metrics) => $metrics->sessions())
->setDimensions(fn(Dimensions $dimensions) => $dimensions->pageTitle())
->forPeriod(Period::defaultPeriod())
->metricFilter(fn(FilterExpression $filterExpression) => $filterExpression
->filterMetric(
metricsCallback: fn(Metrics $metrics) => $metrics->sessions(),
filter: fn(Filter $filter) => $filter->greaterThanInt(100)
)
)
->run();
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Filters\Filter;
use Gtmassey\LaravelAnalytics\Request\Filters\FilterExpression;
use Gtmassey\LaravelAnalytics\Request\Metrics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\Period\Period;
$report = Analytics::query()
->setMetrics(fn(Metrics $metrics) => $metrics->sessions())
->setDimensions(fn(Dimensions $dimensions) => $dimensions->pageTitle())
->forPeriod(Period::defaultPeriod())
->dimensionFilter(fn(FilterExpression $filterExpression) => $filterExpression
->not(fn(FilterExpression $filterExpression) => $filterExpression
->filter('pageTitle', fn(Filter $filter) => $filter
->exact('Home')
)
)
)
->run();
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Filters\Filter;
use Gtmassey\LaravelAnalytics\Request\Filters\FilterExpression;
use Gtmassey\LaravelAnalytics\Request\Filters\FilterExpressionList;
use Gtmassey\LaravelAnalytics\Request\Metrics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\Period\Period;
$report = Analytics::query()
->setMetrics(fn(Metrics $metrics) => $metrics->sessions())
->setDimensions(fn(Dimensions $dimensions) => $dimensions->deviceCategory()->browser())
->forPeriod(Period::defaultPeriod())
->dimensionFilter(fn(FilterExpression $filterExpression) => $filterExpression
->andGroup(fn(FilterExpressionList $filterExpressionList) => $filterExpressionList
->filter('deviceCategory', fn(Filter $filter) => $filter
->exact('Mobile')
)
->filter('browser', fn(Filter $filter) => $filter
->exact('Chrome')
)
)
)
->run();
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Filters\Filter;
use Gtmassey\LaravelAnalytics\Request\Filters\FilterExpression;
use Gtmassey\LaravelAnalytics\Request\Filters\FilterExpressionList;
use Gtmassey\LaravelAnalytics\Request\Metrics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\Period\Period;
$report = Analytics::query()
->setMetrics(fn(Metrics $metrics) => $metrics->sessions())
->setDimensions(fn(Dimensions $dimensions) => $dimensions->browser())
->forPeriod(Period::defaultPeriod())
->dimensionFilter(fn(FilterExpression $filterExpression) => $filterExpression
->orGroup(fn(FilterExpressionList $filterExpressionList) => $filterExpressionList
->filter('browser', fn(Filter $filter) => $filter
->exact('Firefox')
)
->filter('browser', fn(Filter $filter) => $filter
->exact('Chrome')
)
)
)
->run();
namespace App\Analytics;
use Google\Analytics\Data\V1beta\Metric;
use Gtmassey\LaravelAnalytics\Request\Metrics;
class CustomMetrics extends Metrics
{
public function customMetric(): self
{
$this->metrics->push(new Metric(['name' => 'customEvent:parameter_name']));
return $this;
}
}
use Gtmassey\LaravelAnalytics\Request\Metrics;
use App\Analytics\CustomMetrics;
//use Gtmassey\LaravelAnalytics\Request\Dimensions;
//use App\Analytics\CustomDimensions;
public function boot()
{
$this->app->bind(Metrics::class, CustomMetrics::class);
//$this->app->bind(Dimensions::class, CustomDimensions::class);
}
use App\Analytics\CustomMetrics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\LaravelAnalytics\Period;
$report = Analytics::query()
->setMetrics(fn(CustomMetrics $metrics) => $metrics
->customMetric()
->sessions()
)
->forPeriod(Period::defaultPeriod())
->run();
namespace App\Analytics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\LaravelAnalytics\Request\Filters\Filter;
use Gtmassey\LaravelAnalytics\Request\Filters\FilterExpression;
use Gtmassey\LaravelAnalytics\Request\Metrics;
class CustomAnalytics extends Analytics
{
public function onlySessionsAbove(int $count): static
{
$this->metricFilter(fn(FilterExpression $filterExpression) => $filterExpression
->filterMetric(
metricsCallback: fn(Metrics $metrics) => $metrics->sessions(),
filter: fn(Filter $filter) => $filter->greaterThanInt($count),
)
);
return $this;
}
}
use Gtmassey\LaravelAnalytics\Analytics;
use App\Analytics\CustomAnalytics;
public function boot()
{
$this->app->bind(Analytics::class, CustomAnalytics::class);
}
use App\Analytics\CustomAnalytics;
use Gtmassey\LaravelAnalytics\Period;
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Metrics;
$report = CustomAnalytics::query()
->setMetrics(fn(Metrics $metrics) => $metrics->sessions())
->setDimensions(fn(Dimensions $dimensions) => $dimensions->browser())
->forPeriod(Period::defaultPeriod())
->onlySessionsAbove(100)
->run();