IndexController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 \app\controllers\HomeController
  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 actionStatistic(){
  168. $ga=new GA();
  169. $request = Yii::$app->request;
  170. $store=$request->get('store');
  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. if(!$store){
  174. $this->json(0,[],"缺少参数");
  175. }
  176. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  177. //销量
  178. try{
  179. $sells=$ga->visitors_geo($store,$start_day,$end_day);
  180. $product=$ga->product($store,$start_day,$end_day);
  181. $visitors=$ga->visitors_type($store,$start_day,$end_day);
  182. }catch(\Exception $e){
  183. $this->json(0,[],$e->getMessage());
  184. }
  185. $data=[
  186. 'sell'=>$sells,
  187. 'product'=>$product,
  188. 'visitor'=>$visitors,
  189. ];
  190. $this->json(1,$data);
  191. }
  192. public function actionProductinfo(){
  193. $ga=new GA();
  194. $request = Yii::$app->request;
  195. $store=$request->get('store');
  196. $productName=$request->get('productName');
  197. $productName=urldecode($productName);
  198. if(!$productName||!$store){
  199. $this->json(0,[],'参数错误');
  200. }
  201. $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8));
  202. $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
  203. $productInfo=$ga->productInfo($productName,$store,$start_day,$end_day);
  204. $this->json(1,$productInfo);
  205. }
  206. public function addContentpages($contentpages,$store){
  207. $i=1;
  208. $limit=20;
  209. $modelKey=[
  210. 'pagePath',
  211. 'store',
  212. 'date',
  213. 'pageviews',
  214. 'uniquePageviews',
  215. 'avgTimeOnPage',
  216. 'entrances',
  217. 'bounceRate',
  218. 'exitRate',
  219. 'pageValue',
  220. 'created_at',
  221. ];
  222. if(!empty($contentpages)){
  223. $list=[];
  224. foreach($contentpages as $k=>$v){
  225. $item=[
  226. $v['pagePath'],
  227. $store,
  228. date('Y-m-d',strtotime($v['date'])),
  229. $v['pageviews'],
  230. $v['uniquePageviews'],
  231. $v['avgTimeOnPage'],
  232. $v['entrances'],
  233. $v['bounceRate'],
  234. $v['exitRate'],
  235. $v['pageValue'],
  236. time(),
  237. ];
  238. $list[]=$item;
  239. if($i%$limit==0||$i==count($contentpages)){
  240. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  241. $list=[];
  242. echo "addContentpages 任务完成".$i."个"."<br/>";
  243. }
  244. $i++;
  245. }
  246. echo "addContentpages 任务全部完成"."<br/>";
  247. }
  248. }
  249. public function actionRegionProduct(){
  250. $ga=new GA();
  251. $request = Yii::$app->request;
  252. $store=$request->get('store');
  253. $region=$request->get('region');
  254. if(!$region||!$store){
  255. $this->json(0,[],'参数错误');
  256. }
  257. $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8));
  258. $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
  259. try{
  260. $productInfo=$ga->regionProduct($region,$store,$start_day,$end_day);
  261. }catch(\Exception $e){
  262. $this->json(0,[],$e->getMessage());
  263. }
  264. $this->json(1,$productInfo);
  265. }
  266. public function actionGoals(){
  267. $ga=new GA();
  268. $request = Yii::$app->request;
  269. $store=$request->get('store');
  270. if(!$store){
  271. $this->json(0,[],'参数错误');
  272. }
  273. $metricArr=[
  274. 'pageviews'=>"ga:pageviews",
  275. 'sessionCount'=>"ga:sessionCount",
  276. 'users'=>"ga:users",
  277. 'newUsers'=>"ga:newUsers",
  278. 'bounceRate'=>"ga:bounceRate",
  279. 'goal3Completions'=>"ga:goal3Completions",
  280. 'goal3ConversionRate'=>"ga:goal3ConversionRate",
  281. 'goal7Completions'=>"ga:goal7Completions",
  282. 'goal7ConversionRate'=>"ga:goal7ConversionRate",
  283. 'transactionRevenue'=>"ga:transactionRevenue",
  284. 'revenuePerTransaction'=>"ga:revenuePerTransaction",
  285. ];
  286. $start_day=$request->get('start', date('Y-m-d',time()-3600*24));
  287. $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
  288. $chunks=array_chunk($metricArr,10);
  289. $data=[];
  290. foreach($chunks as $arr){
  291. $goals=$ga->goals($store,$arr,$start_day,$end_day);
  292. $data=empty($goals)?$goals:array_merge_recursive($data,$goals);
  293. }
  294. $this->json(1,$data);
  295. }
  296. }