Skip to content

Commit

Permalink
Merge pull request #5 from akki-io/feature/add-predefined-methods
Browse files Browse the repository at this point in the history
Add custom methods
  • Loading branch information
akki-io authored Feb 13, 2022
2 parents 41bdb6b + ce0e24d commit 47b9806
Show file tree
Hide file tree
Showing 7 changed files with 737 additions and 18 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
# Laravel Google Analytics

[![Latest Version](https://img.shields.io/github/release/akki-io/laravel-google-analytics.svg?style=flat-square)](https://github.com/akki-io/laravel-google-analytics/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/akki-io/laravel-google-analytics/Test/master?style=flat-square)
[![StyleCI](https://styleci.io/repos/441735142/shield?branch=master)](https://styleci.io/repos/441735142)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Total Downloads](https://img.shields.io/packagist/dt/akki-io/laravel-google-analytics.svg?style=flat-square)](https://packagist.org/packages/akki-io/laravel-google-analytics)


A Laravel package to retrieve data from Google Analytics 4 using the GA4 Query Explorer

## TL;DR
Expand All @@ -25,8 +27,9 @@ use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType;
use Google\Analytics\Data\V1beta\MetricAggregation;
use Google\Analytics\Data\V1beta\Filter\NumericFilter\Operation;

// get the top 20 viewed pages for last 30 days
LaravelGoogleAnalytics::getTopViewedPages(Period::days(30), $count = 20);
// get the top 20 most viewed pages for last 30 days
LaravelGoogleAnalytics::getMostViewsByPage(Period::days(30), $count = 20);
LaravelGoogleAnalytics::getMostUsersByDate(Period::days(30), $count = 20);

// build a query using the `get()` method
LaravelGoogleAnalytics::dateRanges(Period::days(30), Period::days(60))
Expand All @@ -40,7 +43,7 @@ LaravelGoogleAnalytics::dateRanges(Period::days(30), Period::days(60))
```


Please refer to the [wiki]((https://github.com/akki-io/laravel-google-analytics/wiki)) for more details.
Please refer to the [wiki](https://github.com/akki-io/laravel-google-analytics/wiki) for more details.


## Contributing
Expand All @@ -55,6 +58,7 @@ If you discover any security related issues, please email [email protected] instead

- [Akki Khare](https://github.com/akki-io)
- [All Contributors](../../contributors)
- [spatie/laravel-analytics](https://github.com/spatie/laravel-analytics)

## License

Expand Down
68 changes: 54 additions & 14 deletions src/LaravelGoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace AkkiIo\LaravelGoogleAnalytics;

use AkkiIo\LaravelGoogleAnalytics\Traits\CustomAcquisitionTrait;
use AkkiIo\LaravelGoogleAnalytics\Traits\CustomDemographicsTrait;
use AkkiIo\LaravelGoogleAnalytics\Traits\CustomEngagementTrait;
use AkkiIo\LaravelGoogleAnalytics\Traits\CustomRetentionTrait;
use AkkiIo\LaravelGoogleAnalytics\Traits\CustomTechTrait;
use AkkiIo\LaravelGoogleAnalytics\Traits\DateRangeTrait;
use AkkiIo\LaravelGoogleAnalytics\Traits\DimensionTrait;
use AkkiIo\LaravelGoogleAnalytics\Traits\FilterByDimensionTrait;
Expand All @@ -25,6 +30,11 @@ class LaravelGoogleAnalytics
use FilterByDimensionTrait;
use FilterByMetricTrait;
use RowOperationTrait;
use CustomAcquisitionTrait;
use CustomEngagementTrait;
use CustomRetentionTrait;
use CustomDemographicsTrait;
use CustomTechTrait;
use ResponseTrait;

public ?int $propertyId = null;
Expand All @@ -45,7 +55,21 @@ public function setPropertyId($propertyId = null): self
}

/**
* Set the client.
* Get the property id.
*
* @return int|null
*/
public function getPropertyId(): ?int
{
if (! $this->propertyId) {
$this->setPropertyId();
}

return $this->propertyId;
}

/**
* Set the credentials.
*
* @param null $credentials
* @return $this
Expand All @@ -58,28 +82,44 @@ public function setCredentials($credentials = null): self
}

/**
* Get the result from the GA4 query explorer.
* Get the credentials.
*
* @return LaravelGoogleAnalyticsResponse
*
* @throws \Google\ApiCore\ApiException|\Google\ApiCore\ValidationException
* @return mixed
*/
public function get(): LaravelGoogleAnalyticsResponse
public function getCredentials()
{
if (! $this->propertyId) {
$this->setPropertyId();
}

if (! $this->credentials) {
$this->setCredentials();
}

$client = new BetaAnalyticsDataClient([
'credentials' => $this->credentials,
return $this->credentials;
}

/**
* Get the client.
*
* @return BetaAnalyticsDataClient
*
* @throws \Google\ApiCore\ValidationException
*/
public function getClient(): BetaAnalyticsDataClient
{
return new BetaAnalyticsDataClient([
'credentials' => $this->getCredentials(),
]);
}

$response = $client->runReport([
'property' => "properties/{$this->propertyId}",
/**
* Get the result from the GA4 query explorer.
*
* @return LaravelGoogleAnalyticsResponse
*
* @throws \Google\ApiCore\ApiException|\Google\ApiCore\ValidationException
*/
public function get(): LaravelGoogleAnalyticsResponse
{
$response = $this->getClient()->runReport([
'property' => "properties/{$this->getPropertyId()}",
'dateRanges' => $this->dateRanges,
'metrics' => $this->metrics,
'dimensions' => $this->dimensions,
Expand Down
109 changes: 109 additions & 0 deletions src/Traits/CustomAcquisitionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace AkkiIo\LaravelGoogleAnalytics\Traits;

use AkkiIo\LaravelGoogleAnalytics\Period;
use Illuminate\Support\Arr;

trait CustomAcquisitionTrait
{
/**
* Get total users.
*
* @param Period $period
* @return int
*
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function getTotalUsers(Period $period): int
{
$result = $this->dateRange($period)
->metrics('totalUsers')
->get()
->table;

return (int) Arr::first(Arr::flatten($result));
}

/**
* Get total users by date.
*
* @param Period $period
* @return array
*
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function getTotalUsersByDate(Period $period): array
{
return $this->dateRange($period)
->metrics('totalUsers')
->dimensions('date')
->orderByDimension('date')
->keepEmptyRows(true)
->get()
->table;
}

/**
* Get total users by session source.
*
* @param Period $period
* @return array
*
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function getTotalUsersBySessionSource(Period $period): array
{
return $this->dateRange($period)
->metrics('totalUsers')
->dimensions('sessionSource')
->orderByMetricDesc('totalUsers')
->get()
->table;
}

/**
* Get total users by date.
*
* @param Period $period
* @param int $count
* @return array
*
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function getMostUsersByDate(Period $period, int $count = 20): array
{
return $this->dateRange($period)
->metrics('totalUsers')
->dimensions('date')
->orderByMetricDesc('totalUsers')
->limit($count)
->get()
->table;
}

/**
* Get total users by session source.
*
* @param Period $period
* @param int $count
* @return array
*
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function getMostUsersBySessionSource(Period $period, int $count = 20): array
{
return $this->dateRange($period)
->metrics('totalUsers')
->dimensions('sessionSource')
->orderByMetricDesc('totalUsers')
->limit($count)
->get()
->table;
}
}
Loading

0 comments on commit 47b9806

Please sign in to comment.