GA.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\lib;
  3. class GA{
  4. private static $insances=[];
  5. private static function initializeAnalytics($dp){
  6. $KEY_FILE_LOCATION = BASE_DIR . 'config/savvy-theory-278005-43552bf9fe94.json';
  7. $client = new \Google_Client();
  8. $client->setApplicationName("Hello Analytics Reporting");
  9. $client->setAuthConfig($KEY_FILE_LOCATION);
  10. $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  11. $analytics = new \Google_Service_AnalyticsReporting($client);
  12. return $analytics;
  13. }
  14. public static function getInstance($dp){
  15. if(isset(self::$insances[$dp])){
  16. return self::$insances[$dp];
  17. }else{
  18. self::$insances[$dp]=self::initializeAnalytics($dp);
  19. return self::$insances[$dp];
  20. }
  21. }
  22. private function getViewId($dp){
  23. return "155703065";
  24. }
  25. public function contentpages($dp){
  26. $analytics=self::getInstance($dp);
  27. $metricArr=[
  28. 'pageviews'=>"ga:pageviews",
  29. 'uniquePageviews'=>"ga:uniquePageviews",
  30. 'avgTimeOnPage'=>"ga:avgTimeOnPage",
  31. 'entrances'=>"ga:entrances",
  32. 'bounceRate'=>"ga:bounceRate",
  33. 'exitRate'=>"ga:exitRate",
  34. 'pageValue'=>"ga:pageValue",
  35. ];
  36. $dimensionArr=[
  37. 'pagePath'=>'ga:pagePath',
  38. 'date'=>"ga:date",
  39. ];
  40. $VIEW_ID = $this->getViewId($dp);//
  41. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  42. $start_at=date('Y-m-d',time()-24*3600);
  43. $end_at=date('Y-m-d',time()-24*3600);
  44. $dateRange->setStartDate($start_at);
  45. $dateRange->setEndDate($end_at);
  46. $metrics=[];
  47. $dimensions=[];
  48. foreach($metricArr as $k=>$v){
  49. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  50. $Metric->setExpression($v);
  51. $Metric->setAlias($k);
  52. array_push($metrics,$Metric);
  53. }
  54. foreach($dimensionArr as $k=>$v){
  55. $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
  56. $Dimension->setName($v);
  57. array_push($dimensions,$Dimension);
  58. }
  59. $pageviewsFilter = new \Google_Service_AnalyticsReporting_MetricFilter();
  60. $pageviewsFilter->setMetricName("ga:pageviews");
  61. $pageviewsFilter->setOperator("GREATER_THAN");
  62. $pageviewsFilter->setComparisonValue("1");
  63. $MetricFilterClause=new \Google_Service_AnalyticsReporting_MetricFilterClause();
  64. $MetricFilterClause->setFilters(array($pageviewsFilter));
  65. $orderBy=new \Google_Service_AnalyticsReporting_OrderBy;
  66. $orderBy->setFieldName('ga:pageviews');
  67. $orderBy->setSortOrder("DESCENDING");
  68. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  69. $request->setViewId($VIEW_ID);
  70. $request->setDateRanges($dateRange);
  71. $request->setMetrics($metrics);
  72. $request->setDimensions($dimensions);
  73. $request->setOrderBys($orderBy);
  74. $request->setMetricFilterClauses($MetricFilterClause);
  75. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  76. $body->setReportRequests( array( $request) );
  77. $reports= $analytics->reports->batchGet( $body );
  78. $result=$this->getResults($reports);
  79. echo "<pre>";
  80. print_r($result);exit;
  81. }
  82. /**
  83. * Queries the Analytics Reporting API V4.
  84. *
  85. * @param service An authorized Analytics Reporting API V4 service object.
  86. * @return The Analytics Reporting API V4 response.
  87. */
  88. function getContentReport($VIEW_ID,$analytics,$metricArr,$dimensionArr) {
  89. }
  90. function getResults($reports) {
  91. $data=[];
  92. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  93. $item=[];
  94. $report = $reports[ $reportIndex ];
  95. $header = $report->getColumnHeader();
  96. $dimensionHeaders = $header->getDimensions();
  97. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  98. $rows = $report->getData()->getRows();
  99. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  100. $row = $rows[ $rowIndex ];
  101. $dimensions = $row->getDimensions();
  102. $metrics = $row->getMetrics();
  103. if($dimensionHeaders&&$dimensions){
  104. for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  105. $header = $dimensionHeaders[$i];
  106. $header=str_replace('ga:','',$header);
  107. $item[$header]=$dimensions[$i];
  108. // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  109. // echo"<br/>";
  110. }
  111. // $date=$dimensions[0];
  112. // $date=date('Y-m-d',strtotime($date));
  113. }else{
  114. continue;
  115. }
  116. if($metricHeaders&&$metrics){
  117. for ($j = 0; $j < count($metrics); $j++) {
  118. $values = $metrics[$j]->getValues();
  119. for ($k = 0; $k < count($values); $k++) {
  120. $entry = $metricHeaders[$k];
  121. $item[$entry->getName()]=$values[$k];
  122. // print($entry->getName() . ": " . $values[$k] . "\n\r");
  123. // echo"<br/>";
  124. }
  125. }
  126. }
  127. $data[]=$item;
  128. }
  129. }
  130. return $data;
  131. }
  132. }