IndexController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. use app\lib\GA;
  11. class IndexController extends Controller
  12. {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function behaviors()
  17. {
  18. return [
  19. 'access' => [
  20. 'class' => AccessControl::className(),
  21. 'only' => ['logout'],
  22. 'rules' => [
  23. [
  24. 'actions' => ['logout'],
  25. 'allow' => true,
  26. 'roles' => ['@'],
  27. ],
  28. ],
  29. ],
  30. 'verbs' => [
  31. 'class' => VerbFilter::className(),
  32. 'actions' => [
  33. 'logout' => ['post'],
  34. ],
  35. ],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function actions()
  42. {
  43. return [
  44. 'error' => [
  45. 'class' => 'yii\web\ErrorAction',
  46. ],
  47. 'captcha' => [
  48. 'class' => 'yii\captcha\CaptchaAction',
  49. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  50. ],
  51. ];
  52. }
  53. /**
  54. * Displays homepage.
  55. *
  56. * @return string
  57. */
  58. public function actionIndex()
  59. {
  60. $ga=new GA();
  61. $contentpages=$ga->contentpages();
  62. exit;
  63. $metrics=[
  64. 'sessions'=>"ga:sessions",
  65. 'bounceRate'=>"ga:bounceRate",
  66. 'sessionDuration'=>"ga:sessionDuration",
  67. 'hits'=>"ga:hits",
  68. 'transactions'=>"ga:transactions",
  69. 'transactionsPerSession'=>"ga:transactionsPerSession",
  70. 'transactionRevenue'=>"ga:transactionRevenue",
  71. 'totalValue'=>"ga:totalValue",
  72. 'itemQuantity'=>"ga:itemQuantity",
  73. 'productAddsToCart'=>"ga:productAddsToCart",
  74. 'productCheckouts'=>"ga:productCheckouts",
  75. 'productDetailViews'=>"ga:productDetailViews",
  76. ];
  77. $i=1;
  78. $res=[];
  79. $metrics=array_chunk($metrics,6,true);
  80. foreach($metrics as $metric){
  81. $response = $this->getReport($analytics,$metric);
  82. // $this->printResults($response);
  83. $data=$this->getResults($response);
  84. if(empty($res)){
  85. $res=$data;
  86. }else{
  87. echo "<pre>";
  88. print_r($res);
  89. print_r($data);
  90. echo "1111111\r\n";
  91. $res=array_merge_recursive($res,$data);
  92. echo "<pre>";
  93. print_r($res);
  94. exit;
  95. }
  96. // if(!empty($data)){
  97. // $res=empty($res)?$data:array_merge_recursive($res,$data);
  98. // }
  99. }
  100. echo "<pre>";
  101. print_r($res);
  102. exit;
  103. return $this->render('index');
  104. }
  105. /**
  106. * Parses and prints the Analytics Reporting API V4 response.
  107. *
  108. * @param An Analytics Reporting API V4 response.
  109. */
  110. function printResults($reports) {
  111. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  112. $report = $reports[ $reportIndex ];
  113. $header = $report->getColumnHeader();
  114. $dimensionHeaders = $header->getDimensions();
  115. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  116. $rows = $report->getData()->getRows();
  117. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  118. $row = $rows[ $rowIndex ];
  119. $dimensions = $row->getDimensions();
  120. $metrics = $row->getMetrics();
  121. if($dimensionHeaders&&$dimensions){
  122. for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  123. print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  124. echo"<br/>";
  125. }
  126. }
  127. if($metricHeaders&&$metrics){
  128. for ($j = 0; $j < count($metrics); $j++) {
  129. $values = $metrics[$j]->getValues();
  130. for ($k = 0; $k < count($values); $k++) {
  131. $entry = $metricHeaders[$k];
  132. print($entry->getName() . ": " . $values[$k] . "\n\r");
  133. echo"<br/>";
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. function getResults($reports) {
  141. $data=[];
  142. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  143. $item=[];
  144. $report = $reports[ $reportIndex ];
  145. $header = $report->getColumnHeader();
  146. $dimensionHeaders = $header->getDimensions();
  147. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  148. $rows = $report->getData()->getRows();
  149. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  150. $row = $rows[ $rowIndex ];
  151. $dimensions = $row->getDimensions();
  152. $metrics = $row->getMetrics();
  153. // echo "<pre>";
  154. if($dimensionHeaders&&$dimensions){
  155. // for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  156. // $list[$dimensionHeaders[$i]]=
  157. // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  158. // echo"<br/>";
  159. // }
  160. $date=$dimensions[0];
  161. $date=date('Y-m-d',strtotime($date));
  162. }else{
  163. continue;
  164. }
  165. if($metricHeaders&&$metrics){
  166. for ($j = 0; $j < count($metrics); $j++) {
  167. $values = $metrics[$j]->getValues();
  168. for ($k = 0; $k < count($values); $k++) {
  169. $entry = $metricHeaders[$k];
  170. $data[$date][$entry->getName()]=$values[$k];
  171. // print($entry->getName() . ": " . $values[$k] . "\n\r");
  172. // echo"<br/>";
  173. }
  174. }
  175. }
  176. }
  177. }
  178. return $data;
  179. }
  180. }