Blame view

backend/models/Analytics.php 7.31 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
111
112
              $response = $analytics->reports->batchGet($body);
              
              return $response;
          }
          
          public function generateData()
          {
              $reports = $this->executeQuery();
              
              $data = [];
85d9a37b   Alexey Boroda   -Analytics in pro...
113
      
5e021d7a   Alexey Boroda   -Tabs for analyti...
114
115
116
              /**
               * Generating data for Sessions and users
               */
85d9a37b   Alexey Boroda   -Analytics in pro...
117
              $report = $reports[ 0 ];
3cae1b75   Alexey Boroda   -Analytics almost...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
                  $header = $report->getColumnHeader();
                  //                $dimensionHeaders = $header->getDimensions();
                  $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++) {
                      //                    $dimensions = $row->getDimensions();
                      //                    for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
                      //                    print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
                      //                    }
                      
                      $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 ];
                              $data[ $valueIndex ][] = (int) $value;
                          }
                      }
                  }
85d9a37b   Alexey Boroda   -Analytics in pro...
149
150
      
              /**
5e021d7a   Alexey Boroda   -Tabs for analyti...
151
               * Generating data for
85d9a37b   Alexey Boroda   -Analytics in pro...
152
               */
5e021d7a   Alexey Boroda   -Tabs for analyti...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
              $data2 = [];
      
              $report = $reports[ 1 ];
              $header = $report->getColumnHeader();
              $dimensionHeaders = $header->getDimensions();
              $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...
187
188
189
              return $data;
          }
      }