Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more per page methods #20

Merged
merged 24 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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 @@ -279,6 +282,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 @@ -311,6 +317,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