IndexController.php 13 KB

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