IndexController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. //首页数据
  15. public function actionIndex(){
  16. $ga=new GA();
  17. $request = Yii::$app->request;
  18. $store=$request->get('store');
  19. $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8));
  20. $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
  21. try{
  22. $storeData=$ga->storeData($store,$start_day,$end_day);
  23. $storeSellData=$ga->storeSellData($store,$start_day,$end_day,'week');
  24. // foreach($storeSellData as $v){
  25. // $timestr=$v['year']."W".$v['week'];
  26. // echo $v['week']."<br/>";
  27. // $time=date("Y-m-d",\strtotime($timestr));
  28. // echo $time."<br/>";
  29. // }
  30. echo "<pre>";
  31. print_r($storeSellData);exit;
  32. // $product=$ga->product($store,$start_day,$end_day);
  33. // $visitors=$ga->visitors_type($store,$start_day,$end_day);
  34. }catch(\Exception $e){
  35. $this->json(0,[],$e->getMessage());
  36. }
  37. }
  38. public function actionContentpage(){
  39. $request = Yii::$app->request;
  40. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  41. $store = $request->get('store', "supernova");
  42. $limit = $request->get('limit', "10");
  43. $page = $request->get('page', "1");
  44. $page=$page-1>=0?$page-1:0;
  45. $where=[];
  46. if(!$store){
  47. return ['status'=>false,'msg'=>"参数异常"];
  48. }
  49. $where['store']=$store;
  50. $query=ContentPage::find()->where(["store"=>$where]);
  51. $count = $query->count();
  52. $pagination = new Pagination(['totalCount' => $count,'pageSize'=>$limit,'page'=>$page]);
  53. $items = $query->offset($pagination->offset)
  54. ->limit($pagination->limit)
  55. ->orderBy([
  56. 'pageviews'=>SORT_DESC,
  57. 'id'=>SORT_DESC
  58. ])
  59. ->all();
  60. foreach($items as $k=>$v){
  61. $items[$k]['created_at']=date('Y-m-d H:i:s',$v['created_at']);
  62. }
  63. $data=[
  64. 'total'=>$count,
  65. 'items'=>$items,
  66. ];
  67. return [
  68. 'code' => 200,
  69. 'data'=>$data,
  70. 'status'=>true
  71. ];
  72. }
  73. public function actionSource(){
  74. $request = Yii::$app->request;
  75. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  76. $store = $request->get('store', "supernova");
  77. $limit = $request->get('limit', "10");
  78. $page = $request->get('page', "1");
  79. $page=$page-1>=0?$page-1:0;
  80. $where=[];
  81. if(!$store){
  82. return ['status'=>false,'msg'=>"参数异常"];
  83. }
  84. $where['store']=$store;
  85. $query=Trafficsource::find()->where(["store"=>$where]);
  86. $count = $query->count();
  87. $pagination = new Pagination(['totalCount' => $count,'pageSize'=>$limit,'page'=>$page]);
  88. $items = $query->offset($pagination->offset)
  89. ->limit($pagination->limit)
  90. ->orderBy([
  91. 'date'=>SORT_DESC,
  92. 'users'=>SORT_DESC
  93. ])
  94. ->all();
  95. foreach($items as $k=>$v){
  96. $items[$k]['created_at']=date('Y-m-d H:i:s',$v['created_at']);
  97. }
  98. $data=[
  99. 'total'=>$count,
  100. 'items'=>$items,
  101. ];
  102. return [
  103. 'code' => 200,
  104. 'data'=>$data,
  105. 'status'=>true
  106. ];
  107. }
  108. public function actionSynsdata(){
  109. $ga=new GA();
  110. $store="supernova";
  111. $request = Yii::$app->request;
  112. $start_day = $request->get('start', date('Y-m-d'),time()-24*3600);
  113. $end_day=$start_day;
  114. $query=ContentPage::find()->where(["store"=>$store,'date'=>$start_day]);
  115. $count = $query->count();
  116. if($count>0){
  117. echo $start_day.$store." ContentPage"."已经执行过,请不要重复执行";
  118. }else{
  119. $contentpages=$ga->contentpages($store,$start_day,$end_day);
  120. $this->addContentpages($contentpages,$store);
  121. }
  122. $query=Trafficsource::find()->where(["store"=>$store,'date'=>$start_day]);
  123. $count = $query->count();
  124. if($count>0){
  125. echo $start_day.$store." Trafficsource"."已经执行过,请不要重复执行";
  126. }else{
  127. $contentpages=$ga->trafficsource($store,$start_day,$end_day);
  128. $this->addSource($contentpages,$store);
  129. }
  130. echo $start_day." 任务完成"."<br/>";
  131. exit;
  132. }
  133. public function addSource($data,$store){
  134. $i=1;
  135. $limit=200;
  136. $modelKey=[
  137. 'date',
  138. 'store',
  139. 'source',
  140. 'channelGrouping',
  141. 'landingPagePath',
  142. 'keyword',
  143. 'socialNetwork',
  144. 'users',
  145. 'newUsers',
  146. 'sessions',
  147. 'bounceRate',
  148. 'pageviewsPerSession',
  149. 'avgSessionDuration',
  150. 'transactionsPerVisit',
  151. 'transactions',
  152. 'transactionRevenue',
  153. 'created_at',
  154. ];
  155. if(!empty($data)){
  156. $list=[];
  157. foreach($data as $k=>$v){
  158. $item=[
  159. date('Y-m-d',strtotime($v['date'])),
  160. $store,
  161. $v['source'],
  162. $v['channelGrouping'],
  163. $v['landingPagePath']!=="(not set)"?$v["landingPagePath"]:"",
  164. $v['keyword']!=="(not set)"?$v['keyword']:"",
  165. $v['socialNetwork']!=="(not set)"?$v['socialNetwork']:"",
  166. $v['users'],
  167. $v['newUsers'],
  168. $v['sessions'],
  169. $v['bounceRate'],
  170. $v['pageviewsPerSession'],
  171. $v['avgSessionDuration'],
  172. $v['transactionsPerVisit'],
  173. $v['transactions'],
  174. $v['transactionRevenue'],
  175. time(),
  176. ];
  177. $list[]=$item;
  178. if($i%$limit==0||$i==count($data)){
  179. \Yii::$app->db->createCommand()->batchInsert(Trafficsource::tableName(), $modelKey, $list)->execute();
  180. $list=[];
  181. echo "addSource 任务完成".$i."个"."<br/>";
  182. }
  183. $i++;
  184. }
  185. echo "addSource 任务全部完成"."<br/>";
  186. }
  187. }
  188. public function actionStatistic(){
  189. $ga=new GA();
  190. $request = Yii::$app->request;
  191. $store=$request->get('store');
  192. $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8));
  193. $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
  194. if(!$store){
  195. $this->json(0,[],"缺少参数");
  196. }
  197. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  198. //销量
  199. try{
  200. $sells=$ga->visitors_geo($store,$start_day,$end_day);
  201. $product=$ga->product($store,$start_day,$end_day);
  202. $visitors=$ga->visitors_type($store,$start_day,$end_day);
  203. }catch(\Exception $e){
  204. $this->json(0,[],$e->getMessage());
  205. }
  206. $data=[
  207. 'sell'=>$sells,
  208. 'product'=>$product,
  209. 'visitor'=>$visitors,
  210. ];
  211. $this->json(1,$data);
  212. }
  213. public function actionProductinfo(){
  214. $ga=new GA();
  215. $request = Yii::$app->request;
  216. $store=$request->get('store');
  217. $productName=$request->get('productName');
  218. $productName=urldecode($productName);
  219. if(!$productName||!$store){
  220. $this->json(0,[],'参数错误');
  221. }
  222. $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8));
  223. $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
  224. $productInfo=$ga->productInfo($productName,$store,$start_day,$end_day);
  225. $this->json(1,$productInfo);
  226. }
  227. public function addContentpages($contentpages,$store){
  228. $i=1;
  229. $limit=20;
  230. $modelKey=[
  231. 'pagePath',
  232. 'store',
  233. 'date',
  234. 'pageviews',
  235. 'uniquePageviews',
  236. 'avgTimeOnPage',
  237. 'entrances',
  238. 'bounceRate',
  239. 'exitRate',
  240. 'pageValue',
  241. 'created_at',
  242. ];
  243. if(!empty($contentpages)){
  244. $list=[];
  245. foreach($contentpages as $k=>$v){
  246. $item=[
  247. $v['pagePath'],
  248. $store,
  249. date('Y-m-d',strtotime($v['date'])),
  250. $v['pageviews'],
  251. $v['uniquePageviews'],
  252. $v['avgTimeOnPage'],
  253. $v['entrances'],
  254. $v['bounceRate'],
  255. $v['exitRate'],
  256. $v['pageValue'],
  257. time(),
  258. ];
  259. $list[]=$item;
  260. if($i%$limit==0||$i==count($contentpages)){
  261. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  262. $list=[];
  263. echo "addContentpages 任务完成".$i."个"."<br/>";
  264. }
  265. $i++;
  266. }
  267. echo "addContentpages 任务全部完成"."<br/>";
  268. }
  269. }
  270. public function actionRegionProduct(){
  271. $ga=new GA();
  272. $request = Yii::$app->request;
  273. $store=$request->get('store');
  274. $region=$request->get('region');
  275. if(!$region||!$store){
  276. $this->json(0,[],'参数错误');
  277. }
  278. $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8));
  279. $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
  280. try{
  281. $productInfo=$ga->regionProduct($region,$store,$start_day,$end_day);
  282. }catch(\Exception $e){
  283. $this->json(0,[],$e->getMessage());
  284. }
  285. $this->json(1,$productInfo);
  286. }
  287. public function actionGoals(){
  288. $ga=new GA();
  289. $request = Yii::$app->request;
  290. $store=$request->get('store');
  291. if(!$store){
  292. $this->json(0,[],'参数错误');
  293. }
  294. $metricArr=[
  295. 'pageviews'=>"ga:pageviews",
  296. 'sessions'=>"ga:sessions",
  297. 'users'=>"ga:users",
  298. 'newUsers'=>"ga:newUsers",
  299. 'bounceRate'=>"ga:bounceRate",
  300. // 'goal3Completions'=>"ga:goal3Completions",
  301. // 'goal3ConversionRate'=>"ga:goal3ConversionRate",
  302. // 'goal7Completions'=>"ga:goal7Completions",
  303. // 'goal7ConversionRate'=>"ga:goal7ConversionRate",
  304. 'transactionRevenue'=>"ga:transactionRevenue",
  305. 'revenuePerTransaction'=>"ga:revenuePerTransaction",
  306. ];
  307. switch($store){
  308. case "1"://supernovahair
  309. $metricArr['cartCompletions']="ga:goal8Completions";
  310. $metricArr['cartConversionRate']="ga:goal8ConversionRate";
  311. $metricArr['orderCompletions']="ga:goal7Completions";
  312. $metricArr['orderConversionRate']="ga:goal7ConversionRate";
  313. break;
  314. case "2"://asteriahair
  315. $metricArr['cartCompletions']="ga:goal12Completions";
  316. $metricArr['cartConversionRate']="ga:goal12ConversionRate";
  317. $metricArr['orderCompletions']="ga:goal14Completions";
  318. $metricArr['orderConversionRate']="ga:goal14ConversionRate";
  319. break;
  320. case "3"://alipearlhair
  321. $metricArr['cartCompletions']="ga:goal3Completions";
  322. $metricArr['cartConversionRate']="ga:goal3ConversionRate";
  323. $metricArr['orderCompletions']="ga:goal7Completions";
  324. $metricArr['orderConversionRate']="ga:goal7ConversionRate";
  325. break;
  326. case "4"://westkiss
  327. $metricArr['cartCompletions']="ga:goal3Completions";
  328. $metricArr['cartConversionRate']="ga:goal3ConversionRate";
  329. $metricArr['orderCompletions']="ga:goal2Completions";
  330. $metricArr['orderConversionRate']="ga:goal2ConversionRate";
  331. break;
  332. case "5"://yolissahair
  333. $metricArr['cartCompletions']="ga:goal2Completions";
  334. $metricArr['cartConversionRate']="ga:goal2ConversionRate";
  335. $metricArr['orderCompletions']="ga:goal3Completions";
  336. $metricArr['orderConversionRate']="ga:goal3ConversionRate";
  337. break;
  338. case "6"://wiggins
  339. $metricArr['cartCompletions']="ga:goal1Completions";
  340. $metricArr['cartConversionRate']="ga:goal1ConversionRate";
  341. $metricArr['orderCompletions']="ga:goal3Completions";
  342. $metricArr['orderConversionRate']="ga:goal3ConversionRate";
  343. break;
  344. }
  345. $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8));
  346. $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
  347. $chunks=array_chunk($metricArr,6,1);
  348. $data=[];
  349. foreach($chunks as $arr){
  350. $goals=$ga->goals($store,$arr,$start_day,$end_day);
  351. $data= empty($data)?$goals[0]:array_merge_recursive($data,$goals[0]);
  352. }
  353. $this->json(1,$data);
  354. }
  355. }