Passed
Push — v4 ( b279ec...564170 )
by Benjamin
04:13
created

AnalyticsReporting::setRequestViewIdFromCriteria()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link      https://dukt.net/craft/analytics/
4
 * @copyright Copyright (c) 2018, Dukt
5
 * @license   https://dukt.net/craft/analytics/docs/license
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
     */
30
    public function getService()
31
    {
32
        $client = $this->getClient();
33
34
        return new Google_Service_AnalyticsReporting($client);
35
    }
36
37
    /**
38
     * Get report.
39
     *
40
     * @param ReportRequestCriteria $criteria
41
     * @param bool                  $toArray
42
     *
43
     * @return array|Google_Service_AnalyticsReporting_Report
44
     */
45
    public function getReport(ReportRequestCriteria $criteria, bool $toArray = false)
46
    {
47
        $reports = $this->getReports([$criteria], $toArray);
48
49
        if (isset($reports[0])) {
50
            return $reports[0];
51
        }
52
53
        return null;
54
    }
55
56
    /**
57
     * Get reports.
58
     *
59
     * @param array $criterias
60
     * @param bool  $toArray
61
     *
62
     * @return array
63
     */
64
    public function getReports(array $criterias, bool $toArray = false)
65
    {
66
        $reportsResponse = $this->getReportingReports($criterias);
67
68
        if ($toArray) {
69
            $reportsResponseArray = (array)$reportsResponse->toSimpleObject();
70
71
            return $reportsResponseArray['reports'];
72
        }
73
74
        return $reportsResponse->getReports();
75
    }
76
77
    // Private Methods
78
    // =========================================================================
79
80
    /**
81
     * Get reporting reports.
82
     *
83
     * @param array $criterias
84
     *
85
     * @return Google_Service_AnalyticsReporting_GetReportsResponse
86
     */
87
    private function getReportingReports($criterias)
88
    {
89
        $requests = [];
90
91
        foreach ($criterias as $criteria) {
92
            $request = $this->getReportingReportRequest($criteria);
93
            array_push($requests, $request);
94
        }
95
96
        $reportsRequest = new Google_Service_AnalyticsReporting_GetReportsRequest();
97
        $reportsRequest->setReportRequests($requests);
98
99
        return $this->getService()->reports->batchGet($reportsRequest);
100
    }
101
102
    /**
103
     * Get reporting report request.
104
     *
105
     * @param ReportRequestCriteria $criteria
106
     *
107
     * @return Google_Service_AnalyticsReporting_ReportRequest
108
     * @throws \yii\base\InvalidConfigException
109
     */
110
    private function getReportingReportRequest(ReportRequestCriteria $criteria)
111
    {
112
        $request = new Google_Service_AnalyticsReporting_ReportRequest();
113
114
        $this->setRequestViewIdFromCriteria($request, $criteria);
115
116
        $dateRange = new Google_Service_AnalyticsReporting_DateRange();
117
        $dateRange->setStartDate($criteria->startDate);
118
        $dateRange->setEndDate($criteria->endDate);
119
        $request->setDateRanges($dateRange);
120
121
        if ($criteria->samplingLevel) {
122
            $request->setSamplingLevel($criteria->samplingLevel);
123
        }
124
125
        if ($criteria->metrics) {
126
            $metricString = $criteria->metrics;
127
            $metrics = $this->getMetricsFromString($metricString);
128
            $request->setMetrics($metrics);
129
        }
130
131
        if (!empty($criteria->dimensions)) {
132
            $dimensionString = $criteria->dimensions;
133
            $dimensions = $this->getDimensionsFromString($dimensionString);
134
            $request->setDimensions($dimensions);
135
        }
136
137
        if (!empty($criteria->orderBys)) {
138
            $request->setOrderBys($criteria->orderBys);
139
        }
140
141
        if ($criteria->pageToken) {
142
            $pageToken = (string) $criteria->pageToken;
143
            $request->setPageToken($pageToken);
144
        }
145
146
        if ($criteria->pageSize) {
147
            $request->setPageSize($criteria->pageSize);
148
        }
149
150
        if ($criteria->filtersExpression) {
151
            $request->setFiltersExpression($criteria->filtersExpression);
152
        }
153
154
        if($criteria->includeEmptyRows) {
155
            $request->setIncludeEmptyRows($criteria->includeEmptyRows);
156
        }
157
158
        if($criteria->hideTotals) {
159
            $request->setHideTotals($criteria->hideTotals);
160
        }
161
162
        if($criteria->hideValueRanges) {
163
            $request->setHideValueRanges($criteria->hideValueRanges);
164
        }
165
166
        return $request;
167
    }
168
169
    /**
170
     * @param Google_Service_AnalyticsReporting_ReportRequest $request
171
     * @param ReportRequestCriteria                           $criteria
172
     *
173
     * @throws \yii\base\InvalidConfigException
174
     */
175
    private function setRequestViewIdFromCriteria(Google_Service_AnalyticsReporting_ReportRequest &$request, ReportRequestCriteria $criteria)
176
    {
177
        if($criteria->gaViewId) {
178
            $request->setViewId('ga:'.$criteria->gaViewId);
179
        } else {
180
            if ($criteria->viewId) {
181
                $view = Plugin::getInstance()->getViews()->getViewById($criteria->viewId);
182
183
                if ($view) {
184
                    $request->setViewId($view->gaViewId);
185
                }
186
            }
187
        }
188
    }
189
190
    /**
191
     * Get dimensions from string.
192
     *
193
     * @param $string
194
     *
195
     * @return array
196
     */
197
    private function getDimensionsFromString($string)
198
    {
199
        $dimensions = [];
200
        $_dimensions = explode(',', $string);
201
        foreach ($_dimensions as $_dimension) {
202
            $dimension = new Google_Service_AnalyticsReporting_Dimension();
203
            $dimension->setName($_dimension);
204
            array_push($dimensions, $dimension);
205
        }
206
207
        return $dimensions;
208
    }
209
210
    /**
211
     * Get metrics from string.
212
     *
213
     * @param $string
214
     *
215
     * @return array
216
     */
217
    private function getMetricsFromString($string)
218
    {
219
        $metrics = [];
220
        $_metrics = explode(',', $string);
221
        foreach ($_metrics as $_metric) {
222
            $metric = new Google_Service_AnalyticsReporting_Metric();
223
            $metric->setExpression($_metric);
224
            array_push($metrics, $metric);
225
        }
226
227
        return $metrics;
228
    }
229
}
230