IndexController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\filters\AccessControl;
  5. use yii\web\Controller;
  6. use yii\web\Response;
  7. use yii\filters\VerbFilter;
  8. use app\models\LoginForm;
  9. use app\models\ContactForm;
  10. class IndexController extends Controller
  11. {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function behaviors()
  16. {
  17. return [
  18. 'access' => [
  19. 'class' => AccessControl::className(),
  20. 'only' => ['logout'],
  21. 'rules' => [
  22. [
  23. 'actions' => ['logout'],
  24. 'allow' => true,
  25. 'roles' => ['@'],
  26. ],
  27. ],
  28. ],
  29. 'verbs' => [
  30. 'class' => VerbFilter::className(),
  31. 'actions' => [
  32. 'logout' => ['post'],
  33. ],
  34. ],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function actions()
  41. {
  42. return [
  43. 'error' => [
  44. 'class' => 'yii\web\ErrorAction',
  45. ],
  46. 'captcha' => [
  47. 'class' => 'yii\captcha\CaptchaAction',
  48. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  49. ],
  50. ];
  51. }
  52. /**
  53. * Displays homepage.
  54. *
  55. * @return string
  56. */
  57. public function actionIndex()
  58. {
  59. $analytics=$this->initializeAnalytics();
  60. $metrics=[
  61. 'sessions'=>"ga:sessions",
  62. 'bounceRate'=>"ga:bounceRate",
  63. 'sessionDuration'=>"ga:sessionDuration",
  64. 'hits'=>"ga:hits",
  65. 'transactions'=>"ga:transactions",
  66. 'transactionsPerSession'=>"ga:transactionsPerSession",
  67. 'transactionRevenue'=>"ga:transactionRevenue",
  68. 'totalValue'=>"ga:totalValue",
  69. 'itemQuantity'=>"ga:itemQuantity",
  70. 'productAddsToCart'=>"ga:productAddsToCart",
  71. 'productCheckouts'=>"ga:productCheckouts",
  72. 'productDetailViews'=>"ga:productDetailViews",
  73. ];
  74. $i=1;
  75. $datas=array_chunk($metrics,2,true);
  76. foreach($datas as $data){
  77. $response = $this->getReport($analytics,$data);
  78. // $this->printResults($response);
  79. $res=$this->getResults($response);
  80. echo "<pre>";
  81. print_r($data);
  82. }
  83. exit;
  84. return $this->render('index');
  85. }
  86. function initializeAnalytics()
  87. {
  88. // Use the developers console and download your service account
  89. // credentials in JSON format. Place them in this directory or
  90. // change the key file location if necessary.
  91. $KEY_FILE_LOCATION = BASE_DIR . 'config/savvy-theory-278005-43552bf9fe94.json';
  92. // Create and configure a new client object.
  93. $client = new \Google_Client();
  94. $client->setApplicationName("Hello Analytics Reporting");
  95. $client->setAuthConfig($KEY_FILE_LOCATION);
  96. $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  97. $analytics = new \Google_Service_AnalyticsReporting($client);
  98. return $analytics;
  99. }
  100. /**
  101. * Queries the Analytics Reporting API V4.
  102. *
  103. * @param service An authorized Analytics Reporting API V4 service object.
  104. * @return The Analytics Reporting API V4 response.
  105. */
  106. function getReport($analytics,$data) {
  107. $VIEW_ID = "155703065";
  108. // Create the DateRange object.
  109. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  110. $dateRange->setStartDate("7daysAgo");
  111. $dateRange->setEndDate("today");
  112. $metrics=[];
  113. $dimensions=[];
  114. foreach($data as $k=>$v){
  115. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  116. $Metric->setExpression($v);
  117. $Metric->setAlias($k);
  118. array_push($metrics,$Metric);
  119. }
  120. $date = new \Google_Service_AnalyticsReporting_Dimension();
  121. $date->setName("ga:date");
  122. array_push($dimensions,$date);
  123. // Create the ReportRequest object.
  124. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  125. $request->setViewId($VIEW_ID);
  126. $request->setDateRanges($dateRange);
  127. $request->setMetrics($metrics);
  128. $request->setDimensions($dimensions);
  129. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  130. $body->setReportRequests( array( $request) );
  131. return $analytics->reports->batchGet( $body );
  132. }
  133. /**
  134. * Parses and prints the Analytics Reporting API V4 response.
  135. *
  136. * @param An Analytics Reporting API V4 response.
  137. */
  138. function printResults($reports) {
  139. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  140. $report = $reports[ $reportIndex ];
  141. $header = $report->getColumnHeader();
  142. $dimensionHeaders = $header->getDimensions();
  143. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  144. $rows = $report->getData()->getRows();
  145. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  146. $row = $rows[ $rowIndex ];
  147. $dimensions = $row->getDimensions();
  148. $metrics = $row->getMetrics();
  149. if($dimensionHeaders&&$dimensions){
  150. for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  151. print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  152. echo"<br/>";
  153. }
  154. }
  155. if($metricHeaders&&$metrics){
  156. for ($j = 0; $j < count($metrics); $j++) {
  157. $values = $metrics[$j]->getValues();
  158. for ($k = 0; $k < count($values); $k++) {
  159. $entry = $metricHeaders[$k];
  160. print($entry->getName() . ": " . $values[$k] . "\n\r");
  161. echo"<br/>";
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }
  168. function getResults($reports) {
  169. $data=[];
  170. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  171. $item=[];
  172. $report = $reports[ $reportIndex ];
  173. $header = $report->getColumnHeader();
  174. $dimensionHeaders = $header->getDimensions();
  175. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  176. $rows = $report->getData()->getRows();
  177. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  178. $row = $rows[ $rowIndex ];
  179. $dimensions = $row->getDimensions();
  180. $metrics = $row->getMetrics();
  181. // echo "<pre>";
  182. if($dimensionHeaders&&$dimensions){
  183. // for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  184. // $list[$dimensionHeaders[$i]]=
  185. // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  186. // echo"<br/>";
  187. // }
  188. $date=$dimensions[0];
  189. }else{
  190. continue;
  191. }
  192. if($metricHeaders&&$metrics){
  193. for ($j = 0; $j < count($metrics); $j++) {
  194. $values = $metrics[$j]->getValues();
  195. for ($k = 0; $k < count($values); $k++) {
  196. $entry = $metricHeaders[$k];
  197. $data[$date][$entry->getName()]=$values[$k];
  198. // print($entry->getName() . ": " . $values[$k] . "\n\r");
  199. // echo"<br/>";
  200. }
  201. }
  202. }
  203. }
  204. }
  205. return $data;
  206. }
  207. }