IndexController.php 12 KB

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