Blame view

backend/models/Analytics.php 5.56 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
6
      use yii\helpers\VarDumper;
  
3cae1b75   Alexey Boroda   -Analytics almost...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
      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...
25
26
27
28
      
              /**
               * Setting metrics and dimensions for first query
               */
3cae1b75   Alexey Boroda   -Analytics almost...
29
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
              $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...
60
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
      
              /**
               * 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,
                  ]
              );
3cae1b75   Alexey Boroda   -Analytics almost...
88
89
              
              $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
85d9a37b   Alexey Boroda   -Analytics in pro...
90
91
92
93
94
95
              $body->setReportRequests(
                  [
                      $request,
                      $request2,
                  ]
              );
3cae1b75   Alexey Boroda   -Analytics almost...
96
97
98
99
100
101
102
103
104
105
              $response = $analytics->reports->batchGet($body);
              
              return $response;
          }
          
          public function generateData()
          {
              $reports = $this->executeQuery();
              
              $data = [];
85d9a37b   Alexey Boroda   -Analytics in pro...
106
107
      
              $report = $reports[ 0 ];
3cae1b75   Alexey Boroda   -Analytics almost...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
                  $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...
139
140
141
142
      
              /**
               * @todo Get data from second query
               */
3cae1b75   Alexey Boroda   -Analytics almost...
143
144
145
              return $data;
          }
      }