IndexController.php 13 KB

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