IndexController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. $request = Yii::$app->request;
  56. $start_day = $request->get('start', date('Y-m-d'),time()-24*3600);
  57. $end_day=$start_day;
  58. $contentpages=$ga->contentpages($store,$start_day,$end_day);
  59. $this->addContentpages($contentpages,$store);
  60. $data=$ga->trafficsource($store,$start_day,$end_day);
  61. $this->addSource($data,$store);
  62. echo $start_day." 任务完成";
  63. exit;
  64. }
  65. public function addSource($data,$store){
  66. $i=1;
  67. $limit=200;
  68. $modelKey=[
  69. 'date',
  70. 'store',
  71. 'source',
  72. 'channelGrouping',
  73. 'landingPagePath',
  74. 'keyword',
  75. 'socialNetwork',
  76. 'users',
  77. 'newUsers',
  78. 'sessions',
  79. 'bounceRate',
  80. 'pageviewsPerSession',
  81. 'avgSessionDuration',
  82. 'transactionsPerVisit',
  83. 'transactions',
  84. 'transactionRevenue',
  85. 'created_at',
  86. ];
  87. if(!empty($data)){
  88. $list=[];
  89. foreach($data as $k=>$v){
  90. $item=[
  91. date('Y-m-d',strtotime($v['date'])),
  92. $store,
  93. $v['source'],
  94. $v['channelGrouping'],
  95. $v['landingPagePath']!=="(not set)"?$v["landingPagePath"]:"",
  96. $v['keyword']!=="(not set)"?$v['keyword']:"",
  97. $v['socialNetwork']!=="(not set)"?$v['socialNetwork']:"",
  98. $v['users'],
  99. $v['newUsers'],
  100. $v['sessions'],
  101. $v['bounceRate'],
  102. $v['pageviewsPerSession'],
  103. $v['avgSessionDuration'],
  104. $v['transactionsPerVisit'],
  105. $v['transactions'],
  106. $v['transactionRevenue'],
  107. time(),
  108. ];
  109. $list[]=$item;
  110. if($i%$limit==0||$i==count($data)){
  111. \Yii::$app->db->createCommand()->batchInsert(Trafficsource::tableName(), $modelKey, $list)->execute();
  112. $list=[];
  113. echo "addSource 任务完成".$i."个"."\n";
  114. }
  115. $i++;
  116. }
  117. echo "addSource 任务全部完成"."\n";
  118. }
  119. }
  120. public function addContentpages($contentpages,$store){
  121. $i=1;
  122. $limit=20;
  123. $modelKey=[
  124. 'pagePath',
  125. 'store',
  126. 'date',
  127. 'pageviews',
  128. 'uniquePageviews',
  129. 'avgTimeOnPage',
  130. 'entrances',
  131. 'bounceRate',
  132. 'exitRate',
  133. 'pageValue',
  134. 'created_at',
  135. ];
  136. if(!empty($contentpages)){
  137. $list=[];
  138. foreach($contentpages as $k=>$v){
  139. $item=[
  140. $v['pagePath'],
  141. $store,
  142. date('Y-m-d',strtotime($v['date'])),
  143. $v['pageviews'],
  144. $v['uniquePageviews'],
  145. $v['avgTimeOnPage'],
  146. $v['entrances'],
  147. $v['bounceRate'],
  148. $v['exitRate'],
  149. $v['pageValue'],
  150. time(),
  151. ];
  152. $list[]=$item;
  153. if($i%$limit==0||$i==count($contentpages)){
  154. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  155. $list=[];
  156. echo "addContentpages 任务完成".$i."个"."\n";
  157. }
  158. $i++;
  159. }
  160. echo "addContentpages 任务全部完成"."\n";
  161. }
  162. }
  163. }