IndexController.php 13 KB

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