Skip to content

Commit

Permalink
v2.0.0 (#41)
Browse files Browse the repository at this point in the history
* Extracted Period class (#39)

* Extracted Period class

Extracted the Period class to a separate package for future development in other projects.

* Updated README.md references

* Implemented a way to use custom metrics and dimensions (#40)

* Implemented a way to use custom metrics and dimensions

* Fix styling

* Change to trigger actions

* Removed typehints

Co-authored-by: Plytas <[email protected]>
Co-authored-by: Garrett Massey <[email protected]>

Co-authored-by: Vytautas Smilingis <[email protected]>
Co-authored-by: Plytas <[email protected]>
  • Loading branch information
3 people authored Dec 8, 2022
1 parent 16bda58 commit 9be5e66
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 567 deletions.
65 changes: 60 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Methods currently return an instance of `Gtmassey\LaravelAnalytics\ResponseData`
* [Separate Values](#vals)
* [Usage](#usage)
* [Query Builder](#querybuilder)
* [Custom Metrics And Dimensions](#custommetrics)
* [Default Reports](#defaultreports)
* [Changelog](#changelog)
* [Testing](#testing)
Expand Down Expand Up @@ -145,7 +146,7 @@ All Google Analytics Data API queries require a date range to be run. Use the `P
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Metrics;
use Gtmassey\LaravelAnalytics\Analytics;
use Gtmassey\LaravelAnalytics\Period;
use Gtmassey\Period\Period;
use Carbon\Carbon;

$report = Analytics::query()
Expand All @@ -165,16 +166,70 @@ $report = Analytics::query()
->run();
```

### <a name="custommetrics">Custom metrics and dimensions:</a>

You are not limited to the metrics and dimensions provided by this package. You can use any custom metrics and dimensions you have created in Google Analytics.

Create a new class that extends `Gtmassey\LaravelAnalytics\Request\CustomMetric` or `Gtmassey\LaravelAnalytics\Request\CustomDimension` and implement methods following this format:.

```php
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;
}
}
```

Bind the class in your `AppServiceProvider`:

```php
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);
}
```

Now you can use the custom metric in your query:

```php
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();
```

### <a name="defaultreports">Default Reports:</a>

#### getTopEvents()
```php
$report = Analytics::getTopEvents();
```

This method returns the top events for the given period. It accepts a `Period` object as an optional parameter.
This method returns the top events for the given period. It accepts a `Gtmassey\Period\Period` object as an optional parameter.

If a `Period` object is not passed, it will use the default period set in `Period::defaultPeriod()`.
If a `Gtmassey\Period\Period` object is not passed, it will use the default period set in `Gtmassey\Period\Period::defaultPeriod()`.

The method will return an instance of `Gtmassey\LaravelAnalytics\Response\ResponseData`, which contains `DimensionHeaders`, `MetricHeaders`, `Rows`, and additional metadata.

Expand Down Expand Up @@ -235,7 +290,7 @@ Gtmassey\LaravelAnalytics\Response\ResponseData {
$report = Analytics::getTopPages();
```

This method returns the top pages for the given period. It accepts a `Period` object as an optional parameter.
This method returns the top pages for the given period. It accepts a `Gtmassey\Period\Period` object as an optional parameter.

The pages along with the sessions for that page are listed in the `Rows` property of the response.

Expand All @@ -245,7 +300,7 @@ The pages along with the sessions for that page are listed in the `Rows` propert
$report = Analytics::getUserAcquisitionOverview();
```

This method returns the user acquisition overview for the given period. It accepts a `Period` object as an optional parameter.
This method returns the user acquisition overview for the given period. It accepts a `Gtmassey\Period\Period` object as an optional parameter.

The method will return a `ResponseData` object with the number of sessions by the session's primary acquisition source. Primary acquisition sources are either "direct", "Referral", "Organic Search", and "Organic Social".

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"Google Analytics"
],
"require": {
"php": "^8.1",
"php": "^8.1",
"google/analytics-data": "^0.9.0",
"gtmassey/period": "^1.0",
"illuminate/support": "^9.0",
"nesbot/carbon": "^2.63",
"spatie/laravel-data": "^2.0",
Expand Down
28 changes: 15 additions & 13 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Closure;
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\ApiCore\ApiException;
use Gtmassey\LaravelAnalytics\Exceptions\InvalidPropertyIdException;
use Gtmassey\LaravelAnalytics\Reports\Reports;
use Gtmassey\LaravelAnalytics\Request\Dimensions;
use Gtmassey\LaravelAnalytics\Request\Metrics;
use Gtmassey\LaravelAnalytics\Request\RequestData;
use Gtmassey\LaravelAnalytics\Response\ResponseData;
use Gtmassey\Period\Period;

class Analytics
{
Expand Down Expand Up @@ -40,21 +42,19 @@ public static function query(): self
return resolve(Analytics::class);
}

/****************************************
/***************************************
* Query Builders
****************************************/
***************************************/

/**
* Ability to add metrics to the query using a callback method
* for example:
* $query->setMetrics(function (Metrics $metrics) { $metrics->sessions()->bounceRate(); });
*
* @param Closure(Metrics): Metrics $callback
* @return static
*/
public function setMetrics(Closure $callback): static
{
$metrics = $callback(new Metrics());
/** @var Metrics $metrics */
$metrics = $callback(resolve(Metrics::class));
$this->requestData->metrics->push(...$metrics->getMetrics());

return $this;
Expand All @@ -64,21 +64,23 @@ public function setMetrics(Closure $callback): static
* Ability to add dimensions to the query using a callback method
* for example:
* $query->setDimensions(function (Dimensions $dimensions) { $dimensions->pageTitle()->pagePath(); });
*
* @param Closure(Dimensions): Dimensions $callback
* @return $this
*/
public function setDimensions(Closure $callback): static
{
$dimensions = $callback(new Dimensions());
/** @var Dimensions $dimensions */
$dimensions = $callback(resolve(Dimensions::class));
$this->requestData->dimensions->push(...$dimensions->getDimensions());

return $this;
}

public function forPeriod(Period $period): static
{
$this->requestData->dateRanges->push($period->getDateRange());
$dateRange = new DateRange([
'start_date' => $period->startDate->toDateString(),
'end_date' => $period->endDate->toDateString(),
]);
$this->requestData->dateRanges->push($dateRange);

return $this;
}
Expand All @@ -90,9 +92,9 @@ public function withTotals(bool $useTotals = true): static
return $this;
}

/****************************************
/***************************************
* Process and Run Query
****************************************/
***************************************/

/**
* @throws ApiException
Expand Down
Loading

0 comments on commit 9be5e66

Please sign in to comment.