IndexController.php 14 KB

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