Blame view

backend/models/Analytics.php 7.87 KB
3cae1b75   Alexey Boroda   -Analytics almost...
1
2
3
4
  <?php
      namespace backend\models;
      
      use yii\base\Model;
85d9a37b   Alexey Boroda   -Analytics in pro...
5
  
c39f00f8   Alexey Boroda   -Am charts ready
6
7
8
9
10
      /**
       * Class Analytics
       *
       * @package backend\models
       */
3cae1b75   Alexey Boroda   -Analytics almost...
11
12
13
      class Analytics extends Model
      {
          public $viewId;
c39f00f8   Alexey Boroda   -Am charts ready
14
15
16
17
18
19
      
          /**
           * Preparing and querying data
           *
           * @return \Google_Service_AnalyticsReporting_GetReportsResponse
           */
3cae1b75   Alexey Boroda   -Analytics almost...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
          private function executeQuery()
          {
              $client = new \Google_Client();
              
              $client->setAuthConfig(\Yii::getAlias('@common/config/Artbox-85b8559147bc.json'));
              $client->addScope(\Google_Service_Analytics::ANALYTICS_READONLY);
              
              $analytics = new \Google_Service_AnalyticsReporting($client);
              
              $profile_id = $this->viewId;
              
              $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
              $dateRange->setStartDate("30daysAgo");
              $dateRange->setEndDate("today");
85d9a37b   Alexey Boroda   -Analytics in pro...
34
35
36
37
      
              /**
               * Setting metrics and dimensions for first query
               */
3cae1b75   Alexey Boroda   -Analytics almost...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
              $sessions = new \Google_Service_AnalyticsReporting_Metric();
              $sessions->setExpression('ga:sessions');
              $sessions->setAlias('Сеансы');
              
              $users = new \Google_Service_AnalyticsReporting_Metric();
              $users->setExpression('ga:users');
              $users->setAlias('Пользователи');
              
              $views = new \Google_Service_AnalyticsReporting_Metric();
              $views->setExpression('ga:pageviews');
              $views->setAlias('Просмотры');
              
              $new_sessions = new \Google_Service_AnalyticsReporting_Metric();
              $new_sessions->setExpression('ga:percentNewSessions');
              $new_sessions->setAlias('Новые сессии');
              
              $dimensions = new \Google_Service_AnalyticsReporting_Dimension();
              $dimensions->setName('ga:date');
              
              $request = new \Google_Service_AnalyticsReporting_ReportRequest();
              $request->setViewId($profile_id);
              $request->setDateRanges($dateRange);
              $request->setMetrics(
                  [
                      $sessions,
                      $users,
                      $views,
                      $new_sessions,
                  ]
              );
              $request->setDimensions($dimensions);
85d9a37b   Alexey Boroda   -Analytics in pro...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
      
              /**
               * Setting parameters for second query
               */
              $sessions2 = new \Google_Service_AnalyticsReporting_Metric();
              $sessions2->setExpression('ga:sessions');
              $new_sessions->setAlias('Сессии');
      
              $browser = new \Google_Service_AnalyticsReporting_Dimension();
              $browser->setName('ga:browser');
      
              $city = new \Google_Service_AnalyticsReporting_Dimension();
              $city->setName('ga:city');
      
              $country = new \Google_Service_AnalyticsReporting_Dimension();
              $country->setName('ga:country');
      
              $request2 = new \Google_Service_AnalyticsReporting_ReportRequest();
              $request2->setViewId($profile_id);
              $request2->setDateRanges($dateRange);
              $request2->setMetrics($sessions2);
              $request2->setDimensions(
                  [
                      $browser,
                      $city,
                      $country,
                  ]
              );
5e021d7a   Alexey Boroda   -Tabs for analyti...
97
98
99
100
101
102
      
              $ordering = new \Google_Service_AnalyticsReporting_OrderBy();
              $ordering->setFieldName("ga:sessions");
              $ordering->setOrderType("VALUE");
              $ordering->setSortOrder("DESCENDING");
              $request2->setOrderBys($ordering);
3cae1b75   Alexey Boroda   -Analytics almost...
103
104
              
              $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
85d9a37b   Alexey Boroda   -Analytics in pro...
105
106
107
108
109
110
              $body->setReportRequests(
                  [
                      $request,
                      $request2,
                  ]
              );
3cae1b75   Alexey Boroda   -Analytics almost...
111
112
113
114
              $response = $analytics->reports->batchGet($body);
              
              return $response;
          }
c39f00f8   Alexey Boroda   -Am charts ready
115
116
117
118
119
120
      
          /**
           * Generates data for view
           *
           * @return array
           */
3cae1b75   Alexey Boroda   -Analytics almost...
121
122
123
          public function generateData()
          {
              $reports = $this->executeQuery();
3cae1b75   Alexey Boroda   -Analytics almost...
124
              $data = [];
85d9a37b   Alexey Boroda   -Analytics in pro...
125
      
5e021d7a   Alexey Boroda   -Tabs for analyti...
126
127
128
              /**
               * Generating data for Sessions and users
               */
85d9a37b   Alexey Boroda   -Analytics in pro...
129
              $report = $reports[ 0 ];
3cae1b75   Alexey Boroda   -Analytics almost...
130
                  $header = $report->getColumnHeader();
e01541b2   Alexey Boroda   -Am charts
131
              $dimensionHeaders = $header->getDimensions();
3cae1b75   Alexey Boroda   -Analytics almost...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
                  $metricHeaders = $header->getMetricHeader()
                                          ->getMetricHeaderEntries();
                  $rows = $report->getData()
                                 ->getRows();
                  $totals = $report->getData()
                                   ->getTotals();
                  $total_values = $totals[ 0 ]->getValues();
                  
                  $data[ 'sessions' ] = $total_values[ 0 ];
                  $data[ 'users' ] = $total_values[ 1 ];
                  $data[ 'views' ] = $total_values[ 2 ];
                  $data[ 'new' ] = $total_values[ 3 ];
                  
                  for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
3cae1b75   Alexey Boroda   -Analytics almost...
146
                      $row = $rows[ $rowIndex ];
e01541b2   Alexey Boroda   -Am charts
147
148
149
      
                      $dimensions = $row->getDimensions();
                      for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
c39f00f8   Alexey Boroda   -Am charts ready
150
                          $data[ 'plot' ][ $rowIndex ][ 'day' ] = date('d-m-Y', strtotime($dimensions[ $i ]));
e01541b2   Alexey Boroda   -Am charts
151
152
                      }
                      
3cae1b75   Alexey Boroda   -Analytics almost...
153
154
155
156
                      $metrics = $row->getMetrics();
                      for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) {
                          $values = $metrics[ $j ];
                          for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) {
e01541b2   Alexey Boroda   -Am charts
157
158
159
160
161
162
163
                              if ($valueIndex === 0) {
                                  $name = 'sessions';
                              } elseif ($valueIndex === 1) {
                                  $name = 'users';
                              } else {
                                  continue;
                              }
3cae1b75   Alexey Boroda   -Analytics almost...
164
                              $value = $values->getValues()[ $valueIndex ];
e01541b2   Alexey Boroda   -Am charts
165
                              $data[ 'plot' ][ $rowIndex ][ $name ] = (int) $value;
3cae1b75   Alexey Boroda   -Analytics almost...
166
167
168
                          }
                      }
                  }
85d9a37b   Alexey Boroda   -Analytics in pro...
169
170
      
              /**
c39f00f8   Alexey Boroda   -Am charts ready
171
172
               * Generating data for tabs,
               * with browser, city and country statistics
85d9a37b   Alexey Boroda   -Analytics in pro...
173
               */
5e021d7a   Alexey Boroda   -Tabs for analyti...
174
175
176
177
              $data2 = [];
      
              $report = $reports[ 1 ];
              $header = $report->getColumnHeader();
e01541b2   Alexey Boroda   -Am charts
178
              //            $dimensionHeaders = $header->getDimensions();
5e021d7a   Alexey Boroda   -Tabs for analyti...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
              $metricHeaders = $header->getMetricHeader()
                                      ->getMetricHeaderEntries();
              $rows = $report->getData()
                             ->getRows();
      
              for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
                  $row = $rows[ $rowIndex ];
          
                  $metrics = $row->getMetrics();
                  for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) {
                      $values = $metrics[ $j ];
                      for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) {
                          $value = $values->getValues()[ $valueIndex ];
                          $currentValue = (int) $value;
                      }
                  }
          
                  $dimensions = $row->getDimensions();
                  foreach ($dimensions as $key => $dimension) {
                      if (empty($data2[ $key ][ $dimension ])) {
                          $data2[ $key ][ $dimension ] = $currentValue;
                      } else {
                          $data2[ $key ][ $dimension ] += $currentValue;
                      }
                  }
              }
      
              $data[ 'table' ] = $data2;
              
3cae1b75   Alexey Boroda   -Analytics almost...
208
209
210
              return $data;
          }
      }