1 | <?php |
||
2 | /** |
||
3 | * @link https://dukt.net/analytics/ |
||
4 | * @copyright Copyright (c) 2022, Dukt |
||
5 | * @license https://github.com/dukt/analytics/blob/master/LICENSE.md |
||
6 | */ |
||
7 | |||
8 | namespace dukt\analytics\apis; |
||
9 | |||
10 | use dukt\analytics\base\Api; |
||
11 | use dukt\analytics\models\ReportRequestCriteria; |
||
12 | use dukt\analytics\Plugin; |
||
13 | use \Google_Service_AnalyticsReporting; |
||
14 | use \Google_Service_AnalyticsReporting_DateRange; |
||
15 | use \Google_Service_AnalyticsReporting_Dimension; |
||
16 | use \Google_Service_AnalyticsReporting_GetReportsRequest; |
||
17 | use \Google_Service_AnalyticsReporting_GetReportsResponse; |
||
18 | use \Google_Service_AnalyticsReporting_Metric; |
||
19 | use \Google_Service_AnalyticsReporting_Report; |
||
20 | use \Google_Service_AnalyticsReporting_ReportRequest; |
||
21 | |||
22 | class AnalyticsReporting extends Api |
||
23 | { |
||
24 | // Public Methods |
||
25 | // ========================================================================= |
||
26 | |||
27 | /** |
||
28 | * @return Google_Service_AnalyticsReporting |
||
29 | * @throws \yii\base\InvalidConfigException |
||
30 | */ |
||
31 | public function getService() |
||
32 | { |
||
33 | $client = $this->getClient(); |
||
34 | |||
35 | return new Google_Service_AnalyticsReporting($client); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Get report. |
||
40 | * |
||
41 | * @param ReportRequestCriteria $criteria |
||
42 | * @param bool $toArray |
||
43 | * |
||
44 | * @return array|Google_Service_AnalyticsReporting_Report |
||
45 | * @throws \yii\base\InvalidConfigException |
||
46 | */ |
||
47 | public function getReport(ReportRequestCriteria $criteria, bool $toArray = false) |
||
48 | { |
||
49 | $reports = $this->getReports([$criteria], $toArray); |
||
50 | |||
51 | if (isset($reports[0])) { |
||
52 | return $reports[0]; |
||
53 | } |
||
54 | |||
55 | return null; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Get reports. |
||
60 | * |
||
61 | * @param array $criterias |
||
62 | * @param bool $toArray |
||
63 | * |
||
64 | * @return array |
||
65 | * @throws \yii\base\InvalidConfigException |
||
66 | */ |
||
67 | public function getReports(array $criterias, bool $toArray = false) |
||
68 | { |
||
69 | $reportsResponse = $this->getReportingReports($criterias); |
||
70 | |||
71 | if ($toArray) { |
||
72 | $reportsResponseArray = (array)$reportsResponse->toSimpleObject(); |
||
73 | |||
74 | return $reportsResponseArray['reports']; |
||
75 | } |
||
76 | |||
77 | return $reportsResponse->getReports(); |
||
78 | } |
||
79 | |||
80 | // Private Methods |
||
81 | // ========================================================================= |
||
82 | |||
83 | /** |
||
84 | * Get reporting reports. |
||
85 | * |
||
86 | * @param array $criterias |
||
87 | * |
||
88 | * @return Google_Service_AnalyticsReporting_GetReportsResponse |
||
89 | * @throws \yii\base\InvalidConfigException |
||
90 | */ |
||
91 | private function getReportingReports($criterias) |
||
92 | { |
||
93 | $requests = []; |
||
94 | |||
95 | foreach ($criterias as $criteria) { |
||
96 | $request = $this->getReportingReportRequest($criteria); |
||
97 | array_push($requests, $request); |
||
98 | } |
||
99 | |||
100 | $reportsRequest = new Google_Service_AnalyticsReporting_GetReportsRequest(); |
||
101 | $reportsRequest->setReportRequests($requests); |
||
102 | |||
103 | return $this->getService()->reports->batchGet($reportsRequest); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Get reporting report request. |
||
108 | * |
||
109 | * @param ReportRequestCriteria $criteria |
||
110 | * |
||
111 | * @return Google_Service_AnalyticsReporting_ReportRequest |
||
112 | * @throws \yii\base\InvalidConfigException |
||
113 | */ |
||
114 | private function getReportingReportRequest(ReportRequestCriteria $criteria) |
||
115 | { |
||
116 | $request = new Google_Service_AnalyticsReporting_ReportRequest(); |
||
117 | |||
118 | $this->setRequestViewIdFromCriteria($request, $criteria); |
||
119 | $this->setRequestDateRangeFromCriteria($request, $criteria); |
||
120 | $this->setRequestMetricsFromCriteria($request, $criteria); |
||
121 | $this->setRequestDimensionsFromCriteria($request, $criteria); |
||
122 | |||
123 | if ($criteria->samplingLevel) { |
||
124 | $request->setSamplingLevel($criteria->samplingLevel); |
||
125 | } |
||
126 | |||
127 | if (!empty($criteria->orderBys)) { |
||
128 | $request->setOrderBys($criteria->orderBys); |
||
129 | } |
||
130 | |||
131 | if ($criteria->pageToken) { |
||
132 | $pageToken = (string) $criteria->pageToken; |
||
133 | $request->setPageToken($pageToken); |
||
134 | } |
||
135 | |||
136 | if ($criteria->pageSize) { |
||
137 | $request->setPageSize($criteria->pageSize); |
||
138 | } |
||
139 | |||
140 | if ($criteria->filtersExpression) { |
||
141 | $request->setFiltersExpression($criteria->filtersExpression); |
||
142 | } |
||
143 | |||
144 | if ($criteria->includeEmptyRows) { |
||
145 | $request->setIncludeEmptyRows($criteria->includeEmptyRows); |
||
146 | } |
||
147 | |||
148 | if ($criteria->hideTotals) { |
||
149 | $request->setHideTotals($criteria->hideTotals); |
||
150 | } |
||
151 | |||
152 | if ($criteria->hideValueRanges) { |
||
153 | $request->setHideValueRanges($criteria->hideValueRanges); |
||
154 | } |
||
155 | |||
156 | return $request; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param Google_Service_AnalyticsReporting_ReportRequest $request |
||
161 | * @param ReportRequestCriteria $criteria |
||
162 | * |
||
163 | * @throws \yii\base\InvalidConfigException |
||
164 | */ |
||
165 | private function setRequestViewIdFromCriteria(Google_Service_AnalyticsReporting_ReportRequest &$request, ReportRequestCriteria $criteria) |
||
166 | { |
||
167 | if ($criteria->gaViewId) { |
||
168 | $request->setViewId('ga:'.$criteria->gaViewId); |
||
169 | } else { |
||
170 | if ($criteria->viewId) { |
||
171 | $view = Plugin::getInstance()->getViews()->getViewById($criteria->viewId); |
||
172 | |||
173 | if ($view) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
174 | $request->setViewId($view->gaViewId); |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param Google_Service_AnalyticsReporting_ReportRequest $request |
||
182 | * @param ReportRequestCriteria $criteria |
||
183 | */ |
||
184 | private function setRequestDateRangeFromCriteria(Google_Service_AnalyticsReporting_ReportRequest &$request, ReportRequestCriteria $criteria) |
||
185 | { |
||
186 | $dateRange = new Google_Service_AnalyticsReporting_DateRange(); |
||
187 | $dateRange->setStartDate($criteria->startDate); |
||
188 | $dateRange->setEndDate($criteria->endDate); |
||
189 | $request->setDateRanges($dateRange); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param Google_Service_AnalyticsReporting_ReportRequest $request |
||
194 | * @param ReportRequestCriteria $criteria |
||
195 | */ |
||
196 | private function setRequestMetricsFromCriteria(Google_Service_AnalyticsReporting_ReportRequest &$request, ReportRequestCriteria $criteria) |
||
197 | { |
||
198 | if ($criteria->metrics) { |
||
199 | $metricString = $criteria->metrics; |
||
200 | $metrics = $this->getMetricsFromString($metricString); |
||
201 | $request->setMetrics($metrics); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param Google_Service_AnalyticsReporting_ReportRequest $request |
||
207 | * @param ReportRequestCriteria $criteria |
||
208 | */ |
||
209 | private function setRequestDimensionsFromCriteria(Google_Service_AnalyticsReporting_ReportRequest &$request, ReportRequestCriteria $criteria) |
||
210 | { |
||
211 | if (!empty($criteria->dimensions)) { |
||
212 | $dimensionString = $criteria->dimensions; |
||
213 | $dimensions = $this->getDimensionsFromString($dimensionString); |
||
214 | $request->setDimensions($dimensions); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Get dimensions from string. |
||
220 | * |
||
221 | * @param $string |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | private function getDimensionsFromString($string) |
||
226 | { |
||
227 | $dimensions = []; |
||
228 | $_dimensions = explode(',', $string); |
||
229 | foreach ($_dimensions as $_dimension) { |
||
230 | $dimension = new Google_Service_AnalyticsReporting_Dimension(); |
||
231 | $dimension->setName($_dimension); |
||
232 | array_push($dimensions, $dimension); |
||
233 | } |
||
234 | |||
235 | return $dimensions; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Get metrics from string. |
||
240 | * |
||
241 | * @param $string |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | private function getMetricsFromString($string) |
||
246 | { |
||
247 | $metrics = []; |
||
248 | $_metrics = explode(',', $string); |
||
249 | foreach ($_metrics as $_metric) { |
||
250 | $metric = new Google_Service_AnalyticsReporting_Metric(); |
||
251 | $metric->setExpression($_metric); |
||
252 | array_push($metrics, $metric); |
||
253 | } |
||
254 | |||
255 | return $metrics; |
||
256 | } |
||
257 | } |
||
258 |