|
@@ -0,0 +1,152 @@
|
|
|
+<?php
|
|
|
+namespace app\lib;
|
|
|
+class GA{
|
|
|
+ private static $insances=[];
|
|
|
+ private static function initializeAnalytics($dp){
|
|
|
+ $KEY_FILE_LOCATION = BASE_DIR . 'config/savvy-theory-278005-43552bf9fe94.json';
|
|
|
+ $client = new \Google_Client();
|
|
|
+ $client->setApplicationName("Hello Analytics Reporting");
|
|
|
+ $client->setAuthConfig($KEY_FILE_LOCATION);
|
|
|
+ $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
|
|
|
+ $analytics = new \Google_Service_AnalyticsReporting($client);
|
|
|
+
|
|
|
+ return $analytics;
|
|
|
+ }
|
|
|
+ public static function getInstance($dp){
|
|
|
+ if(isset(self::$insances[$dp])){
|
|
|
+ return self::$insances[$dp];
|
|
|
+ }else{
|
|
|
+ self::$insances[$dp]=self::initializeAnalytics($dp);
|
|
|
+ return self::$insances[$dp];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private function getViewId($dp){
|
|
|
+ return "155703065";
|
|
|
+ }
|
|
|
+ public function contentpages($dp){
|
|
|
+ $analytics=self::getInstance($dp);
|
|
|
+ $metrics=[
|
|
|
+ 'pageviews'=>"ga:pageviews",
|
|
|
+ 'uniquePageviews'=>"ga:uniquePageviews",
|
|
|
+ 'avgPageDuration'=>"ga:avgPageDuration",
|
|
|
+ 'entrances'=>"ga:entrances",
|
|
|
+ 'bounceRate'=>"ga:bounceRate",
|
|
|
+ 'exitRate'=>"ga:exitRate",
|
|
|
+ 'pageValue'=>"ga:pageValue",
|
|
|
+ ];
|
|
|
+ $dimensionArr=[
|
|
|
+ 'pagePath'=>'ga:pagePath',
|
|
|
+ 'date'=>"ga:date",
|
|
|
+ ];
|
|
|
+ $VIEW_ID = $this->getViewId($dp);//
|
|
|
+ $reports=$this->getReport($VIEW_ID,$analytics,$metric);
|
|
|
+ $result=$this->getResults();
|
|
|
+ echo "</pre>";
|
|
|
+ print_r($result);exit;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Queries the Analytics Reporting API V4.
|
|
|
+ *
|
|
|
+ * @param service An authorized Analytics Reporting API V4 service object.
|
|
|
+ * @return The Analytics Reporting API V4 response.
|
|
|
+ */
|
|
|
+ function getReport($analytics,$metricArr,$dimensionArr) {
|
|
|
+ $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
|
|
|
+ $dateRange->setStartDate("7daysAgo");
|
|
|
+ $dateRange->setEndDate("today");
|
|
|
+ $metrics=[];
|
|
|
+ $dimensions=[];
|
|
|
+ foreach($metricArr as $k=>$v){
|
|
|
+ $Metric = new \Google_Service_AnalyticsReporting_Metric();
|
|
|
+ $Metric->setExpression($v);
|
|
|
+ $Metric->setAlias($k);
|
|
|
+ array_push($metrics,$Metric);
|
|
|
+ }
|
|
|
+ foreach($dimensionArr as $k=>$v){
|
|
|
+ $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
|
|
|
+ $Dimension->setName($v);
|
|
|
+ array_push($dimensions,$Dimension);
|
|
|
+ }
|
|
|
+ $request = new \Google_Service_AnalyticsReporting_ReportRequest();
|
|
|
+ $request->setViewId($VIEW_ID);
|
|
|
+ $request->setDateRanges($dateRange);
|
|
|
+ $request->setMetrics($metrics);
|
|
|
+ $request->setDimensions($dimensions);
|
|
|
+
|
|
|
+ $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
|
|
|
+ $body->setReportRequests( array( $request) );
|
|
|
+ return $analytics->reports->batchGet( $body );
|
|
|
+
|
|
|
+ }
|
|
|
+ function getResults($reports) {
|
|
|
+ $data=[];
|
|
|
+ for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
|
|
|
+ $item=[];
|
|
|
+ $report = $reports[ $reportIndex ];
|
|
|
+
|
|
|
+ $header = $report->getColumnHeader();
|
|
|
+ $dimensionHeaders = $header->getDimensions();
|
|
|
+ $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
|
|
|
+ $rows = $report->getData()->getRows();
|
|
|
+
|
|
|
+
|
|
|
+ for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
|
|
|
+ $row = $rows[ $rowIndex ];
|
|
|
+ $dimensions = $row->getDimensions();
|
|
|
+
|
|
|
+ $metrics = $row->getMetrics();
|
|
|
+ // echo "<pre>";
|
|
|
+ if($dimensionHeaders&&$dimensions){
|
|
|
+ for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
|
|
|
+ $item[$dimensionHeaders[$i]]=$dimensions[$i];
|
|
|
+ // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
|
|
|
+ // echo"<br/>";
|
|
|
+ }
|
|
|
+ // $date=$dimensions[0];
|
|
|
+ // $date=date('Y-m-d',strtotime($date));
|
|
|
+
|
|
|
+ }else{
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($metricHeaders&&$metrics){
|
|
|
+ for ($j = 0; $j < count($metrics); $j++) {
|
|
|
+ $values = $metrics[$j]->getValues();
|
|
|
+ for ($k = 0; $k < count($values); $k++) {
|
|
|
+ $entry = $metricHeaders[$k];
|
|
|
+ $list[$entry->getName()]=$values[$k];
|
|
|
+ // print($entry->getName() . ": " . $values[$k] . "\n\r");
|
|
|
+ // echo"<br/>";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|