IndexController.php 4.4 KB

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