IndexController.php 14 KB

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