IndexController.php 13 KB

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