Skip to content

Commit

Permalink
Add more per page methods (#20)
Browse files Browse the repository at this point in the history
* Fix styling

* wip

* Fix styling

* wip

* Fix styling

* wip

* Fix styling

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Fix styling

* wip

* wip

* wip

* wip

* Update README

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa authored Aug 3, 2023
1 parent 286cf91 commit 3764271
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ $data = Analytics::totalUsersByGender(Period::weeks(7));

// Get the total users by age group for the last 7 weeks
$data = Analytics::totalUsersByAgeGroup(Period::weeks(7));

// Get the total users by date per page for the last 7 weeks:
$data = Analytics::totalUsersByDatePerPage(Period::weeks(7));
```

### Device and OS Analytics
Expand Down Expand Up @@ -278,6 +281,9 @@ use Vormkracht10\Analytics\Period;
// Get the top 10 referrals for the last 14 days:
$data = Analytics::getTopReferrers(period: Period::days(14), limit: 10);

// Get the top 10 referrals for the last 14 days, by page path:
$data = Analytics::getTopReferrersByPagePath(period: Period::days(14), limit: 10);

// Get the top 10 referrals for the last 14 days, grouped by page title:
$data = Analytics::getTopReferrersByPageTitle(period: Period::days(14), limit: 10);

Expand Down Expand Up @@ -310,6 +316,9 @@ use Vormkracht10\Analytics\Period;
// Get total sessions for the last 7 days:
$data = Analytics::sessions(Period::days(7));

// Get total sessions for the last 7 days per page:
$data = Analytics::sessionsPerPage(Period::days(7));

// Get the average session duration for the last 7 days:
$data = Analytics::averageSessionDuration(Period::days(7));

Expand Down
6 changes: 5 additions & 1 deletion src/Traits/Analytics/RealtimeAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait RealtimeAnalytics
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function activeUsers(Period|null $period = null): int
public function activeUsers(Period $period = null, string $path = null): int
{
if (is_null($period)) {
$period = Period::minutes(30);
Expand All @@ -21,6 +21,10 @@ public function activeUsers(Period|null $period = null): int
->setDateRange($period)
->addMetrics('activeUsers');

if ($path) {
$googleAnalytics->addDimension('pagePath');
}

$result = $this->getReport($googleAnalytics)
->dataTable;

Expand Down
37 changes: 25 additions & 12 deletions src/Traits/Analytics/ResourceAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public function getTopReferrersByPageTitle(Period $period, int $limit): array
->dataTable;
}

public function getTopReferrersByPagePath(Period $period, int $limit): array
{
$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('sessions')
->addDimensions('pageReferrer', 'pagePath')
->orderByMetric('sessions', Direction::DESC)
->limit($limit);

return $this->getReport($googleAnalytics)
->dataTable;
}

public function getLandingPagesByPageTitle(Period $period, int $limit): array
{
$googleAnalytics = $this->googleAnalytics
Expand All @@ -59,18 +72,18 @@ public function getLandingPagesPlusQueryStringByPageTitle(Period $period, int $l
->dataTable;
}

public function getLandingPages(Period $period, int $limit): array
{
$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('sessions')
->addDimensions('landingPage')
->orderByMetric('sessions', Direction::DESC)
->limit($limit);

return $this->getReport($googleAnalytics)
->dataTable;
}
public function getLandingPages(Period $period, int $limit): array
{
$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('sessions')
->addDimensions('landingPage')
->orderByMetric('sessions', Direction::DESC)
->limit($limit);

return $this->getReport($googleAnalytics)
->dataTable;
}

public function getTopTrafficSources(Period $period, int $limit): array
{
Expand Down
12 changes: 12 additions & 0 deletions src/Traits/Analytics/SessionsAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public function sessions(Period $period): int
return (int) Arr::first(Arr::flatten($result));
}

public function sessionsPerPage(Period $period): array
{
$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('sessions')
->addDimensions('pagePath')
->keepEmptyRows(true);

return $this->getReport($googleAnalytics)
->dataTable;
}

/**
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
Expand Down
16 changes: 16 additions & 0 deletions src/Traits/Analytics/UsersAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public function totalUsersByDate(Period $period): array
->dataTable;
}

/**
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function totalUsersByDatePerPage(Period $period): array
{
$googleAnalytics = $this->googleAnalytics
->setDateRange($period)
->addMetrics('totalUsers')
->addDimensions('date')
->addDimensions('pagePath');

return $this->getReport($googleAnalytics)
->dataTable;
}

/**
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
Expand Down

0 comments on commit 3764271

Please sign in to comment.