IndexController.php 12 KB

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