IndexController.php 14 KB

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