IndexController.php 12 KB

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