Blame view

backend/models/Analytics.php 7.54 KB
3cae1b75   Alexey Boroda   -Analytics almost...
1
2
3
4
  <?php
      namespace backend\models;
      
      use yii\base\Model;
5e021d7a   Alexey Boroda   -Tabs for analyti...
5
      use yii\helpers\ArrayHelper;
85d9a37b   Alexey Boroda   -Analytics in pro...
6
7
      use yii\helpers\VarDumper;
  
3cae1b75   Alexey Boroda   -Analytics almost...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
      class Analytics extends Model
      {
          public $viewId;
          
          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...
26
27
28
29
      
              /**
               * Setting metrics and dimensions for first query
               */
3cae1b75   Alexey Boroda   -Analytics almost...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
              $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...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
      
              /**
               * 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...
89
90
91
92
93
94
      
              $ordering = new \Google_Service_AnalyticsReporting_OrderBy();
              $ordering->setFieldName("ga:sessions");
              $ordering->setOrderType("VALUE");
              $ordering->setSortOrder("DESCENDING");
              $request2->setOrderBys($ordering);
3cae1b75   Alexey Boroda   -Analytics almost...
95
96
              
              $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
85d9a37b   Alexey Boroda   -Analytics in pro...
97
98
99
100
101
102
              $body->setReportRequests(
                  [
                      $request,
                      $request2,
                  ]
              );
3cae1b75   Alexey Boroda   -Analytics almost...
103
104
105
106
107
108
109
110
              $response = $analytics->reports->batchGet($body);
              
              return $response;
          }
          
          public function generateData()
          {
              $reports = $this->executeQuery();
3cae1b75   Alexey Boroda   -Analytics almost...
111
              $data = [];
85d9a37b   Alexey Boroda   -Analytics in pro...
112
      
5e021d7a   Alexey Boroda   -Tabs for analyti...
113
114
115
              /**
               * Generating data for Sessions and users
               */
85d9a37b   Alexey Boroda   -Analytics in pro...
116
              $report = $reports[ 0 ];
3cae1b75   Alexey Boroda   -Analytics almost...
117
                  $header = $report->getColumnHeader();
e01541b2   Alexey Boroda   -Am charts
118
              $dimensionHeaders = $header->getDimensions();
3cae1b75   Alexey Boroda   -Analytics almost...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
                  $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...
133
                      $row = $rows[ $rowIndex ];
e01541b2   Alexey Boroda   -Am charts
134
135
136
137
138
139
      
                      $dimensions = $row->getDimensions();
                      for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
                          $data[ 'plot' ][ $rowIndex ][ 'day' ] = $dimensions[ $i ];
                      }
                      
3cae1b75   Alexey Boroda   -Analytics almost...
140
141
142
143
                      $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
144
145
146
147
148
149
150
                              if ($valueIndex === 0) {
                                  $name = 'sessions';
                              } elseif ($valueIndex === 1) {
                                  $name = 'users';
                              } else {
                                  continue;
                              }
3cae1b75   Alexey Boroda   -Analytics almost...
151
                              $value = $values->getValues()[ $valueIndex ];
e01541b2   Alexey Boroda   -Am charts
152
                              $data[ 'plot' ][ $rowIndex ][ $name ] = (int) $value;
3cae1b75   Alexey Boroda   -Analytics almost...
153
154
155
                          }
                      }
                  }
85d9a37b   Alexey Boroda   -Analytics in pro...
156
157
      
              /**
5e021d7a   Alexey Boroda   -Tabs for analyti...
158
               * Generating data for
85d9a37b   Alexey Boroda   -Analytics in pro...
159
               */
5e021d7a   Alexey Boroda   -Tabs for analyti...
160
161
162
163
              $data2 = [];
      
              $report = $reports[ 1 ];
              $header = $report->getColumnHeader();
e01541b2   Alexey Boroda   -Am charts
164
              //            $dimensionHeaders = $header->getDimensions();
5e021d7a   Alexey Boroda   -Tabs for analyti...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
              $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...
194
195
196
              return $data;
          }
      }