IndexController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. use app\models\Trafficsource;
  11. use yii\data\Pagination;
  12. class IndexController extends Controller
  13. {
  14. public function actionIndex(){
  15. echo 111;exit;
  16. }
  17. public function actionContentpage(){
  18. $request = Yii::$app->request;
  19. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  20. $store = $request->get('store', "supernova");
  21. $limit = $request->get('limit', "10");
  22. $page = $request->get('page', "1");
  23. $page=$page-1>=0?$page-1:0;
  24. $where=[];
  25. if(!$store){
  26. return ['status'=>false,'msg'=>"参数异常"];
  27. }
  28. $where['store']=$store;
  29. $query=ContentPage::find()->where(["store"=>$where]);
  30. $count = $query->count();
  31. $pagination = new Pagination(['totalCount' => $count,'pageSize'=>$limit,'page'=>$page]);
  32. $items = $query->offset($pagination->offset)
  33. ->limit($pagination->limit)
  34. ->orderBy([
  35. 'pageviews'=>SORT_DESC,
  36. 'id'=>SORT_DESC
  37. ])
  38. ->all();
  39. foreach($items as $k=>$v){
  40. $items[$k]['created_at']=date('Y-m-d H:i:s',$v['created_at']);
  41. }
  42. $data=[
  43. 'total'=>$count,
  44. 'items'=>$items,
  45. ];
  46. return [
  47. 'code' => 200,
  48. 'data'=>$data,
  49. 'status'=>true
  50. ];
  51. }
  52. public function actionSynsdata(){
  53. $ga=new GA();
  54. $store="supernova";
  55. // $start_day=
  56. $data=$ga->trafficsource($store);
  57. $this->addSource($data,$store);
  58. exit;
  59. // $contentpages=$ga->contentpages($store);
  60. // $this->addContentpages($contentpages,$store);
  61. }
  62. public function addSource($data,$store){
  63. $i=1;
  64. $limit=200;
  65. $modelKey=[
  66. 'date',
  67. 'store',
  68. 'source',
  69. 'channelGrouping',
  70. 'landingPagePath',
  71. 'keyword',
  72. 'socialNetwork',
  73. 'users',
  74. 'newUsers',
  75. 'sessions',
  76. 'bounceRate',
  77. 'pageviewsPerSession',
  78. 'avgSessionDuration',
  79. 'transactionsPerVisit',
  80. 'transactions',
  81. 'transactionRevenue',
  82. 'created_at',
  83. ];
  84. if(!empty($data)){
  85. $list=[];
  86. foreach($data as $k=>$v){
  87. $item=[
  88. date('Y-m-d',strtotime($v['date'])),
  89. $store,
  90. $v['source'],
  91. $v['channelGrouping'],
  92. $v['landingPagePath']!=="(not set)"?$v["landingPagePath"]:"",
  93. $v['keyword']!=="(not set)"?$v['keyword']:"",
  94. $v['socialNetwork']!=="(not set)"?$v['socialNetwork']:"",
  95. $v['users'],
  96. $v['newUsers'],
  97. $v['sessions'],
  98. $v['bounceRate'],
  99. $v['pageviewsPerSession'],
  100. $v['avgSessionDuration'],
  101. $v['transactionsPerVisit'],
  102. $v['transactions'],
  103. $v['transactionRevenue'],
  104. time(),
  105. ];
  106. $list[]=$item;
  107. if($i%$limit==0||$i==count($data)){
  108. \Yii::$app->db->createCommand()->batchInsert(Trafficsource::tableName(), $modelKey, $list)->execute();
  109. $list=[];
  110. }
  111. $i++;
  112. }
  113. }
  114. }
  115. public function addContentpages($contentpages,$store){
  116. $i=1;
  117. $limit=20;
  118. $modelKey=[
  119. 'pagePath',
  120. 'store',
  121. 'date',
  122. 'pageviews',
  123. 'uniquePageviews',
  124. 'avgTimeOnPage',
  125. 'entrances',
  126. 'bounceRate',
  127. 'exitRate',
  128. 'pageValue',
  129. 'created_at',
  130. ];
  131. if(!empty($contentpages)){
  132. $list=[];
  133. foreach($contentpages as $k=>$v){
  134. $item=[
  135. $v['pagePath'],
  136. $store,
  137. date('Y-m-d',strtotime($v['date'])),
  138. $v['pageviews'],
  139. $v['uniquePageviews'],
  140. $v['avgTimeOnPage'],
  141. $v['entrances'],
  142. $v['bounceRate'],
  143. $v['exitRate'],
  144. $v['pageValue'],
  145. time(),
  146. ];
  147. $list[]=$item;
  148. if($i%$limit==0||$i==count($contentpages)){
  149. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  150. $list=[];
  151. }
  152. $i++;
  153. }
  154. }
  155. }
  156. }