-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from akki-io/feature/add-predefined-methods
Add custom methods
- Loading branch information
Showing
7 changed files
with
737 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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)) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.