IndexController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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\lib\GA;
  9. use app\models\ContentPage;
  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. public function actionContentpage(){
  53. $pages=ContentPage::find()->asArray()->all();
  54. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  55. return [
  56. 'code' => 200,
  57. 'data'=>$pages,
  58. 'status'=>true
  59. ];
  60. }
  61. public function actionSynsdata(){
  62. $ga=new GA();
  63. $store="supernova";
  64. $contentpages=$ga->contentpages($store);
  65. $i=1;
  66. $limit=20;
  67. $modelKey=[
  68. 'pagePath',
  69. 'store',
  70. 'date',
  71. 'pageviews',
  72. 'uniquePageviews',
  73. 'avgTimeOnPage',
  74. 'entrances',
  75. 'bounceRate',
  76. 'exitRate',
  77. 'pageValue',
  78. 'created_at',
  79. ];
  80. if(!empty($contentpages)){
  81. $list=[];
  82. foreach($contentpages as $k=>$v){
  83. $item=[
  84. $v['pagePath'],
  85. $store,
  86. date('y-m-d',strtotime($v['date'])),
  87. $v['pageviews'],
  88. $v['uniquePageviews'],
  89. $v['avgTimeOnPage'],
  90. $v['entrances'],
  91. $v['bounceRate'],
  92. $v['exitRate'],
  93. $v['pageValue'],
  94. time(),
  95. ];
  96. $list[]=$item;
  97. if($i%$limit==0||$i==count($contentpages)){
  98. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  99. $list=[];
  100. echo 111111;exit;
  101. }
  102. $i++;
  103. }
  104. }
  105. echo 111111;exit;
  106. }
  107. /**
  108. * Parses and prints the Analytics Reporting API V4 response.
  109. *
  110. * @param An Analytics Reporting API V4 response.
  111. */
  112. function printResults($reports) {
  113. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  114. $report = $reports[ $reportIndex ];
  115. $header = $report->getColumnHeader();
  116. $dimensionHeaders = $header->getDimensions();
  117. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  118. $rows = $report->getData()->getRows();
  119. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  120. $row = $rows[ $rowIndex ];
  121. $dimensions = $row->getDimensions();
  122. $metrics = $row->getMetrics();
  123. if($dimensionHeaders&&$dimensions){
  124. for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  125. print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  126. echo"<br/>";
  127. }
  128. }
  129. if($metricHeaders&&$metrics){
  130. for ($j = 0; $j < count($metrics); $j++) {
  131. $values = $metrics[$j]->getValues();
  132. for ($k = 0; $k < count($values); $k++) {
  133. $entry = $metricHeaders[$k];
  134. print($entry->getName() . ": " . $values[$k] . "\n\r");
  135. echo"<br/>";
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. function getResults($reports) {
  143. $data=[];
  144. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  145. $item=[];
  146. $report = $reports[ $reportIndex ];
  147. $header = $report->getColumnHeader();
  148. $dimensionHeaders = $header->getDimensions();
  149. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  150. $rows = $report->getData()->getRows();
  151. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  152. $row = $rows[ $rowIndex ];
  153. $dimensions = $row->getDimensions();
  154. $metrics = $row->getMetrics();
  155. // echo "<pre>";
  156. if($dimensionHeaders&&$dimensions){
  157. // for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  158. // $list[$dimensionHeaders[$i]]=
  159. // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  160. // echo"<br/>";
  161. // }
  162. $date=$dimensions[0];
  163. $date=date('Y-m-d',strtotime($date));
  164. }else{
  165. continue;
  166. }
  167. if($metricHeaders&&$metrics){
  168. for ($j = 0; $j < count($metrics); $j++) {
  169. $values = $metrics[$j]->getValues();
  170. for ($k = 0; $k < count($values); $k++) {
  171. $entry = $metricHeaders[$k];
  172. $data[$date][$entry->getName()]=$values[$k];
  173. // print($entry->getName() . ": " . $values[$k] . "\n\r");
  174. // echo"<br/>";
  175. }
  176. }
  177. }
  178. }
  179. }
  180. return $data;
  181. }
  182. }