| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\Analytics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Google_Service_Analytics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Traits\Macroable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Analytics | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     use Macroable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var \Spatie\Analytics\AnalyticsClient */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $viewId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param \Spatie\Analytics\AnalyticsClient $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param string                            $viewId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function __construct(AnalyticsClient $client, string $viewId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->viewId = $viewId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param string $viewId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function setViewId(string $viewId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->viewId = $viewId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 43 |  | View Code Duplication |     public function fetchVisitorsAndPageViews(Period $period): Collection | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $response = $this->performQuery( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $period, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'ga:users,ga:pageviews', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ['dimensions' => 'ga:date,ga:pageTitle'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return collect($response['rows'] ?? [])->map(function (array $dateRow) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 'date' => Carbon::createFromFormat('Ymd', $dateRow[0]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 'pageTitle' => $dateRow[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 'visitors' => (int) $dateRow[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 'pageViews' => (int) $dateRow[3], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 61 |  | View Code Duplication |     public function fetchTotalVisitorsAndPageViews(Period $period): Collection | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $response = $this->performQuery( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $period, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'ga:users,ga:pageviews', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ['dimensions' => 'ga:date'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return collect($response['rows'] ?? [])->map(function (array $dateRow) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 'date' => Carbon::createFromFormat('Ymd', $dateRow[0]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 'visitors' => (int) $dateRow[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 'pageViews' => (int) $dateRow[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 78 |  | View Code Duplication |     public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $response = $this->performQuery( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $period, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'ga:pageviews', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 'dimensions' => 'ga:pagePath,ga:pageTitle', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 'sort' => '-ga:pageviews', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 'max-results' => $maxResults, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return collect($response['rows'] ?? []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ->map(function (array $pageRow) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                     'url' => $pageRow[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     'pageTitle' => $pageRow[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     'pageViews' => (int) $pageRow[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 100 |  | View Code Duplication |     public function fetchTopReferrers(Period $period, int $maxResults = 20): Collection | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         $response = $this->performQuery($period, | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             'ga:pageviews', | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                 'dimensions' => 'ga:fullReferrer', | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 'sort' => '-ga:pageviews', | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 'max-results' => $maxResults, | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         return collect($response['rows'] ?? [])->map(function (array $pageRow) { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 'url' => $pageRow[0], | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 'pageViews' => (int) $pageRow[1], | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $response = $this->performQuery( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $period, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             'ga:sessions', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 'dimensions' => 'ga:browser', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 'sort' => '-ga:sessions', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $topBrowsers = collect($response['rows'] ?? [])->map(function (array $browserRow) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 'browser' => $browserRow[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 'sessions' => (int) $browserRow[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         if ($topBrowsers->count() <= $maxResults) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             return $topBrowsers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return $this->summarizeTopBrowsers($topBrowsers, $maxResults); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     protected function summarizeTopBrowsers(Collection $topBrowsers, int $maxResults): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return $topBrowsers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             ->take($maxResults - 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             ->push([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 'browser' => 'Others', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 'sessions' => $topBrowsers->splice($maxResults - 1)->sum('sessions'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * Call the query method on the authenticated client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @param Period $period | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @param string $metrics | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @param array  $others | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @return array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     public function performQuery(Period $period, string $metrics, array $others = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         return $this->client->performQuery( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $this->viewId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             $period->startDate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             $period->endDate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             $metrics, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             $others | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * Get the underlying Google_Service_Analytics object. You can use this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * to basically call anything on the Google Analytics API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @return \Google_Service_Analytics | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     public function getAnalyticsService(): Google_Service_Analytics | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         return $this->client->getAnalyticsService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 184 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.