From 37642715101bed915ea710b3d8a00c39fd637751 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Thu, 3 Aug 2023 13:15:01 +0200 Subject: [PATCH] Add more per page methods (#20) * 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 --- README.md | 9 ++++++ src/Traits/Analytics/RealtimeAnalytics.php | 6 +++- src/Traits/Analytics/ResourceAnalytics.php | 37 +++++++++++++++------- src/Traits/Analytics/SessionsAnalytics.php | 12 +++++++ src/Traits/Analytics/UsersAnalytics.php | 16 ++++++++++ 5 files changed, 67 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f66a5bf..3c8c77a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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); @@ -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)); diff --git a/src/Traits/Analytics/RealtimeAnalytics.php b/src/Traits/Analytics/RealtimeAnalytics.php index 54bbbda..5265657 100644 --- a/src/Traits/Analytics/RealtimeAnalytics.php +++ b/src/Traits/Analytics/RealtimeAnalytics.php @@ -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); @@ -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; diff --git a/src/Traits/Analytics/ResourceAnalytics.php b/src/Traits/Analytics/ResourceAnalytics.php index 66b0a62..f786f8c 100644 --- a/src/Traits/Analytics/ResourceAnalytics.php +++ b/src/Traits/Analytics/ResourceAnalytics.php @@ -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 @@ -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 { diff --git a/src/Traits/Analytics/SessionsAnalytics.php b/src/Traits/Analytics/SessionsAnalytics.php index 361adbd..8f7cb78 100644 --- a/src/Traits/Analytics/SessionsAnalytics.php +++ b/src/Traits/Analytics/SessionsAnalytics.php @@ -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 diff --git a/src/Traits/Analytics/UsersAnalytics.php b/src/Traits/Analytics/UsersAnalytics.php index 118a5e1..ffca111 100644 --- a/src/Traits/Analytics/UsersAnalytics.php +++ b/src/Traits/Analytics/UsersAnalytics.php @@ -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