IndexController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. class IndexController extends Controller
  11. {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function behaviors()
  16. {
  17. return [
  18. 'access' => [
  19. 'class' => AccessControl::className(),
  20. 'only' => ['logout'],
  21. 'rules' => [
  22. [
  23. 'actions' => ['logout'],
  24. 'allow' => true,
  25. 'roles' => ['@'],
  26. ],
  27. ],
  28. ],
  29. 'verbs' => [
  30. 'class' => VerbFilter::className(),
  31. 'actions' => [
  32. 'logout' => ['post'],
  33. ],
  34. ],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function actions()
  41. {
  42. return [
  43. 'error' => [
  44. 'class' => 'yii\web\ErrorAction',
  45. ],
  46. 'captcha' => [
  47. 'class' => 'yii\captcha\CaptchaAction',
  48. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  49. ],
  50. ];
  51. }
  52. /**
  53. * Displays homepage.
  54. *
  55. * @return string
  56. */
  57. public function actionIndex()
  58. {
  59. exit;
  60. $metrics=[
  61. 'sessions'=>"ga:sessions",
  62. 'bounceRate'=>"ga:bounceRate",
  63. 'sessionDuration'=>"ga:sessionDuration",
  64. 'hits'=>"ga:hits",
  65. 'transactions'=>"ga:transactions",
  66. 'transactionsPerSession'=>"ga:transactionsPerSession",
  67. 'transactionRevenue'=>"ga:transactionRevenue",
  68. 'totalValue'=>"ga:totalValue",
  69. 'itemQuantity'=>"ga:itemQuantity",
  70. 'productAddsToCart'=>"ga:productAddsToCart",
  71. 'productCheckouts'=>"ga:productCheckouts",
  72. 'productDetailViews'=>"ga:productDetailViews",
  73. ];
  74. $i=1;
  75. $res=[];
  76. $metrics=array_chunk($metrics,6,true);
  77. foreach($metrics as $metric){
  78. $response = $this->getReport($analytics,$metric);
  79. // $this->printResults($response);
  80. $data=$this->getResults($response);
  81. if(empty($res)){
  82. $res=$data;
  83. }else{
  84. echo "<pre>";
  85. print_r($res);
  86. print_r($data);
  87. echo "1111111\r\n";
  88. $res=array_merge_recursive($res,$data);
  89. echo "<pre>";
  90. print_r($res);
  91. exit;
  92. }
  93. // if(!empty($data)){
  94. // $res=empty($res)?$data:array_merge_recursive($res,$data);
  95. // }
  96. }
  97. echo "<pre>";
  98. print_r($res);
  99. exit;
  100. return $this->render('index');
  101. }
  102. public function actionSynsdata(){
  103. $ga=new GA();
  104. $store="supernova";
  105. $contentpages=$ga->contentpages($store);
  106. $i=1;
  107. $limit=20;
  108. $modelKey=[
  109. 'pagePath',
  110. 'store',
  111. 'date',
  112. 'pageviews',
  113. 'uniquePageviews',
  114. 'avgTimeOnPage',
  115. 'entrances',
  116. 'bounceRate',
  117. 'exitRate',
  118. 'pageValue',
  119. 'created_at',
  120. ];
  121. if(!empty($contentpages)){
  122. foreach($contentpages as $k=>$v){
  123. $list=[];
  124. $item=[
  125. $v['pagePath'],
  126. $store,
  127. date('y-m-d',strtotime($v['date'])),
  128. $v['pageviews'],
  129. $v['uniquePageviews'],
  130. $v['avgTimeOnPage'],
  131. $v['entrances'],
  132. $v['bounceRate'],
  133. $v['exitRate'],
  134. $v['pageValue'],
  135. time(),
  136. ];
  137. $list[]=$item;
  138. if($i%$limit==0||$i==count($contentpages)){
  139. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  140. $list=[];
  141. echo 111111;exit;
  142. }
  143. $i++;
  144. }
  145. }
  146. echo 111111;exit;
  147. }
  148. /**
  149. * Parses and prints the Analytics Reporting API V4 response.
  150. *
  151. * @param An Analytics Reporting API V4 response.
  152. */
  153. function printResults($reports) {
  154. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  155. $report = $reports[ $reportIndex ];
  156. $header = $report->getColumnHeader();
  157. $dimensionHeaders = $header->getDimensions();
  158. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  159. $rows = $report->getData()->getRows();
  160. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  161. $row = $rows[ $rowIndex ];
  162. $dimensions = $row->getDimensions();
  163. $metrics = $row->getMetrics();
  164. if($dimensionHeaders&&$dimensions){
  165. for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  166. print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  167. echo"<br/>";
  168. }
  169. }
  170. if($metricHeaders&&$metrics){
  171. for ($j = 0; $j < count($metrics); $j++) {
  172. $values = $metrics[$j]->getValues();
  173. for ($k = 0; $k < count($values); $k++) {
  174. $entry = $metricHeaders[$k];
  175. print($entry->getName() . ": " . $values[$k] . "\n\r");
  176. echo"<br/>";
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. function getResults($reports) {
  184. $data=[];
  185. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  186. $item=[];
  187. $report = $reports[ $reportIndex ];
  188. $header = $report->getColumnHeader();
  189. $dimensionHeaders = $header->getDimensions();
  190. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  191. $rows = $report->getData()->getRows();
  192. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  193. $row = $rows[ $rowIndex ];
  194. $dimensions = $row->getDimensions();
  195. $metrics = $row->getMetrics();
  196. // echo "<pre>";
  197. if($dimensionHeaders&&$dimensions){
  198. // for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  199. // $list[$dimensionHeaders[$i]]=
  200. // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  201. // echo"<br/>";
  202. // }
  203. $date=$dimensions[0];
  204. $date=date('Y-m-d',strtotime($date));
  205. }else{
  206. continue;
  207. }
  208. if($metricHeaders&&$metrics){
  209. for ($j = 0; $j < count($metrics); $j++) {
  210. $values = $metrics[$j]->getValues();
  211. for ($k = 0; $k < count($values); $k++) {
  212. $entry = $metricHeaders[$k];
  213. $data[$date][$entry->getName()]=$values[$k];
  214. // print($entry->getName() . ": " . $values[$k] . "\n\r");
  215. // echo"<br/>";
  216. }
  217. }
  218. }
  219. }
  220. }
  221. return $data;
  222. }
  223. }