IndexController.php 12 KB

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