X7ROOT File Manager
Current Path:
/home/gfecatvj/sites/realesbar/platform/plugins/analytics/src
home
/
gfecatvj
/
sites
/
realesbar
/
platform
/
plugins
/
analytics
/
src
/
📁
..
📄
Analytics.php
(6.02 KB)
📄
AnalyticsClient.php
(2.43 KB)
📄
AnalyticsClientFactory.php
(2.06 KB)
📁
Cache
📁
Exceptions
📁
Facades
📄
GoogleClient.php
(1.84 KB)
📁
Http
📄
Period.php
(2.05 KB)
📄
Plugin.php
(850 B)
📁
Providers
Editing: Analytics.php
<?php namespace Botble\Analytics; use Carbon\Carbon; use Google_Service_Analytics; use Illuminate\Support\Collection; use Illuminate\Support\Traits\Macroable; use stdClass; class Analytics { use Macroable; /** * @var AnalyticsClient */ protected $client; /** * @var string */ protected $viewId; /** * @param AnalyticsClient $client * @param string $viewId */ public function __construct(AnalyticsClient $client, string $viewId) { $this->client = $client; $this->viewId = $viewId; } /** * @param string $viewId * @return $this */ public function setViewId(string $viewId) { $this->viewId = $viewId; return $this; } /** * @param Period $period * @return Collection */ public function fetchVisitorsAndPageViews(Period $period): Collection { $response = $this->performQuery( $period, 'ga:users,ga:pageviews', ['dimensions' => 'ga:date,ga:pageTitle'] ); return collect($response['rows'] ?? [])->map(function (array $dateRow) { return [ 'date' => Carbon::createFromFormat('Ymd', $dateRow[0]), 'pageTitle' => $dateRow[1], 'visitors' => (int)$dateRow[2], 'pageViews' => (int)$dateRow[3], ]; }); } /** * @param Period $period * @return Collection */ public function fetchTotalVisitorsAndPageViews(Period $period): Collection { $response = $this->performQuery( $period, 'ga:users,ga:pageviews', ['dimensions' => 'ga:date'] ); return collect($response['rows'] ?? [])->map(function (array $dateRow) { return [ 'date' => Carbon::createFromFormat('Ymd', $dateRow[0]), 'visitors' => (int)$dateRow[1], 'pageViews' => (int)$dateRow[2], ]; }); } /** * @param Period $period * @param int $maxResults * @return Collection */ public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection { $response = $this->performQuery( $period, 'ga:pageviews', [ 'dimensions' => 'ga:pagePath,ga:pageTitle', 'sort' => '-ga:pageviews', 'max-results' => $maxResults, ] ); return collect($response['rows'] ?? []) ->map(function (array $pageRow) { return [ 'url' => $pageRow[0], 'pageTitle' => $pageRow[1], 'pageViews' => (int)$pageRow[2], ]; }); } /** * @param Period $period * @param int $maxResults * @return Collection */ public function fetchTopReferrers(Period $period, int $maxResults = 20): Collection { $response = $this->performQuery($period, 'ga:pageviews', [ 'dimensions' => 'ga:fullReferrer', 'sort' => '-ga:pageviews', 'max-results' => $maxResults, ] ); return collect($response['rows'] ?? [])->map(function (array $pageRow) { return [ 'url' => $pageRow[0], 'pageViews' => (int)$pageRow[1], ]; }); } /** * @param Period $period * @return Collection */ public function fetchUserTypes(Period $period): Collection { $response = $this->performQuery( $period, 'ga:sessions', [ 'dimensions' => 'ga:userType', ] ); return collect($response->rows ?? [])->map(function (array $userRow) { return [ 'type' => $userRow[0], 'sessions' => (int)$userRow[1], ]; }); } /** * @param Period $period * @param int $maxResults * @return Collection */ public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection { $response = $this->performQuery( $period, 'ga:sessions', [ 'dimensions' => 'ga:browser', 'sort' => '-ga:sessions', ] ); $topBrowsers = collect($response['rows'] ?? [])->map(function (array $browserRow) { return [ 'browser' => $browserRow[0], 'sessions' => (int)$browserRow[1], ]; }); if ($topBrowsers->count() <= $maxResults) { return $topBrowsers; } return $this->summarizeTopBrowsers($topBrowsers, $maxResults); } /** * @param Collection $topBrowsers * @param int $maxResults * @return Collection */ protected function summarizeTopBrowsers(Collection $topBrowsers, int $maxResults): Collection { return $topBrowsers ->take($maxResults - 1) ->push([ 'browser' => 'Others', 'sessions' => $topBrowsers->splice($maxResults - 1)->sum('sessions'), ]); } /** * Call the query method on the authenticated client. * * @param Period $period * @param string $metrics * @param array $others * @return stdClass|null */ public function performQuery(Period $period, string $metrics, array $others = []) { return $this->client->performQuery( $this->viewId, $period->startDate, $period->endDate, $metrics, $others ); } /** * Get the underlying Google_Service_Analytics object. You can use this * to basically call anything on the Google Analytics API. * @return Google_Service_Analytics */ public function getAnalyticsService(): Google_Service_Analytics { return $this->client->getAnalyticsService(); } }
Upload File
Create Folder