IndexController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 actionSource(){
  53. $request = Yii::$app->request;
  54. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  55. $store = $request->get('store', "supernova");
  56. $limit = $request->get('limit', "10");
  57. $page = $request->get('page', "1");
  58. $page=$page-1>=0?$page-1:0;
  59. $where=[];
  60. if(!$store){
  61. return ['status'=>false,'msg'=>"参数异常"];
  62. }
  63. $where['store']=$store;
  64. $query=Trafficsource::find()->where(["store"=>$where]);
  65. $count = $query->count();
  66. $pagination = new Pagination(['totalCount' => $count,'pageSize'=>$limit,'page'=>$page]);
  67. $items = $query->offset($pagination->offset)
  68. ->limit($pagination->limit)
  69. ->orderBy([
  70. 'date'=>SORT_DESC,
  71. 'users'=>SORT_DESC
  72. ])
  73. ->all();
  74. foreach($items as $k=>$v){
  75. $items[$k]['created_at']=date('Y-m-d H:i:s',$v['created_at']);
  76. }
  77. $data=[
  78. 'total'=>$count,
  79. 'items'=>$items,
  80. ];
  81. return [
  82. 'code' => 200,
  83. 'data'=>$data,
  84. 'status'=>true
  85. ];
  86. }
  87. public function actionSynsdata(){
  88. $ga=new GA();
  89. $store="supernova";
  90. $request = Yii::$app->request;
  91. $start_day = $request->get('start', date('Y-m-d'),time()-24*3600);
  92. $end_day=$start_day;
  93. $query=ContentPage::find()->where(["store"=>$store,'date'=>$start_day]);
  94. $count = $query->count();
  95. if($count>0){
  96. echo $start_day.$store." ContentPage"."已经执行过,请不要重复执行";
  97. }else{
  98. $contentpages=$ga->contentpages($store,$start_day,$end_day);
  99. $this->addContentpages($contentpages,$store);
  100. }
  101. $query=Trafficsource::find()->where(["store"=>$store,'date'=>$start_day]);
  102. $count = $query->count();
  103. if($count>0){
  104. echo $start_day.$store." Trafficsource"."已经执行过,请不要重复执行";
  105. }else{
  106. $contentpages=$ga->trafficsource($store,$start_day,$end_day);
  107. $this->addSource($contentpages,$store);
  108. }
  109. echo $start_day." 任务完成"."<br/>";
  110. exit;
  111. }
  112. public function addSource($data,$store){
  113. $i=1;
  114. $limit=200;
  115. $modelKey=[
  116. 'date',
  117. 'store',
  118. 'source',
  119. 'channelGrouping',
  120. 'landingPagePath',
  121. 'keyword',
  122. 'socialNetwork',
  123. 'users',
  124. 'newUsers',
  125. 'sessions',
  126. 'bounceRate',
  127. 'pageviewsPerSession',
  128. 'avgSessionDuration',
  129. 'transactionsPerVisit',
  130. 'transactions',
  131. 'transactionRevenue',
  132. 'created_at',
  133. ];
  134. if(!empty($data)){
  135. $list=[];
  136. foreach($data as $k=>$v){
  137. $item=[
  138. date('Y-m-d',strtotime($v['date'])),
  139. $store,
  140. $v['source'],
  141. $v['channelGrouping'],
  142. $v['landingPagePath']!=="(not set)"?$v["landingPagePath"]:"",
  143. $v['keyword']!=="(not set)"?$v['keyword']:"",
  144. $v['socialNetwork']!=="(not set)"?$v['socialNetwork']:"",
  145. $v['users'],
  146. $v['newUsers'],
  147. $v['sessions'],
  148. $v['bounceRate'],
  149. $v['pageviewsPerSession'],
  150. $v['avgSessionDuration'],
  151. $v['transactionsPerVisit'],
  152. $v['transactions'],
  153. $v['transactionRevenue'],
  154. time(),
  155. ];
  156. $list[]=$item;
  157. if($i%$limit==0||$i==count($data)){
  158. \Yii::$app->db->createCommand()->batchInsert(Trafficsource::tableName(), $modelKey, $list)->execute();
  159. $list=[];
  160. echo "addSource 任务完成".$i."个"."<br/>";
  161. }
  162. $i++;
  163. }
  164. echo "addSource 任务全部完成"."<br/>";
  165. }
  166. }
  167. public function actionTest(){
  168. $ga=new GA();
  169. $store="supernova";
  170. $request = Yii::$app->request;
  171. $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8));
  172. $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
  173. //销量
  174. $sells=$ga->visitors_geo($store,$start_day,$end_day);
  175. $sellSku=$ga->productSku($store,$start_day,$end_day);
  176. $visitors=$ga->visitors_type($store,$start_day,$end_day);
  177. echo "<pre>";
  178. print_r($visitors);exit;
  179. }
  180. public function addContentpages($contentpages,$store){
  181. $i=1;
  182. $limit=20;
  183. $modelKey=[
  184. 'pagePath',
  185. 'store',
  186. 'date',
  187. 'pageviews',
  188. 'uniquePageviews',
  189. 'avgTimeOnPage',
  190. 'entrances',
  191. 'bounceRate',
  192. 'exitRate',
  193. 'pageValue',
  194. 'created_at',
  195. ];
  196. if(!empty($contentpages)){
  197. $list=[];
  198. foreach($contentpages as $k=>$v){
  199. $item=[
  200. $v['pagePath'],
  201. $store,
  202. date('Y-m-d',strtotime($v['date'])),
  203. $v['pageviews'],
  204. $v['uniquePageviews'],
  205. $v['avgTimeOnPage'],
  206. $v['entrances'],
  207. $v['bounceRate'],
  208. $v['exitRate'],
  209. $v['pageValue'],
  210. time(),
  211. ];
  212. $list[]=$item;
  213. if($i%$limit==0||$i==count($contentpages)){
  214. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  215. $list=[];
  216. echo "addContentpages 任务完成".$i."个"."<br/>";
  217. }
  218. $i++;
  219. }
  220. echo "addContentpages 任务全部完成"."<br/>";
  221. }
  222. }
  223. }