chengwl 4 年之前
父節點
當前提交
54bc487e87
共有 2 個文件被更改,包括 16 次插入11 次删除
  1. 13 4
      controllers/IndexController.php
  2. 3 7
      lib/GA.php

+ 13 - 4
controllers/IndexController.php

@@ -63,12 +63,17 @@ class IndexController extends Controller
     public function actionSynsdata(){
       $ga=new GA();
       $store="supernova";
-      // $start_day=
-      $data=$ga->trafficsource($store);
+      $request = Yii::$app->request;
+      $start_day = $request->get('start', date('Y-m-d'),time()-24*3600);
+      $end_day=$start_day;
+      $contentpages=$ga->contentpages($store,$start_day,$end_day);
+      $this->addContentpages($contentpages,$store);
+
+      $data=$ga->trafficsource($store,$start_day,$end_day);
       $this->addSource($data,$store);
+      echo $start_day." 任务完成";
       exit;
-      // $contentpages=$ga->contentpages($store);
-      // $this->addContentpages($contentpages,$store);
+
 
      
 
@@ -121,9 +126,11 @@ class IndexController extends Controller
           if($i%$limit==0||$i==count($data)){
             \Yii::$app->db->createCommand()->batchInsert(Trafficsource::tableName(), $modelKey, $list)->execute();
             $list=[];
+            echo "addSource 任务完成".$i."个"."\n";
           }
           $i++;
         }
+        echo "addSource 任务全部完成"."\n";
       }
     }
 
@@ -166,9 +173,11 @@ class IndexController extends Controller
             if($i%$limit==0||$i==count($contentpages)){
               \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
               $list=[];
+              echo "addContentpages 任务完成".$i."个"."\n";
             }
             $i++;
           }
+          echo "addContentpages 任务全部完成"."\n";
         }
 
     }

+ 3 - 7
lib/GA.php

@@ -23,7 +23,7 @@ class GA{
     private function getViewId($dp){
         return "155703065";
     }
-    public function contentpages($dp){
+    public function contentpages($dp,$start_at,$end_at){
         $analytics=self::getInstance($dp);
         $metricArr=[
             'pageviews'=>"ga:pageviews",
@@ -40,8 +40,6 @@ class GA{
         ];
         $VIEW_ID = $this->getViewId($dp);//
         $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
-        $start_at=date('Y-m-d',time()-24*3600);
-        $end_at=date('Y-m-d',time()-24*3600);
         $dateRange->setStartDate($start_at);
         $dateRange->setEndDate($end_at);
         $metrics=[];
@@ -64,7 +62,7 @@ class GA{
         $pageviewsFilter->setOperator("GREATER_THAN");
         $pageviewsFilter->setComparisonValue("1");
 
-                $MetricFilterClause=new \Google_Service_AnalyticsReporting_MetricFilterClause();
+        $MetricFilterClause=new \Google_Service_AnalyticsReporting_MetricFilterClause();
         $MetricFilterClause->setFilters(array($pageviewsFilter));
 
 
@@ -134,7 +132,7 @@ class GA{
          return $result;
        
     }
-    public function trafficsource(){
+    public function trafficsource($dp,$start_at,$end_at){
         $analytics=self::getInstance($dp);
         $metricArr=[
             'users'=>"ga:users",
@@ -157,8 +155,6 @@ class GA{
         ];
         $VIEW_ID = $this->getViewId($dp);//
         $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
-        $start_at=date('Y-m-d',time()-24*3600);
-        $end_at=date('Y-m-d',time()-24*3600);
         $dateRange->setStartDate($start_at);
         $dateRange->setEndDate($end_at);
         $metrics=[];