chengwl 4 роки тому
батько
коміт
87bc62f268
3 змінених файлів з 89 додано та 4 видалено
  1. 61 0
      controllers/IndexController.php
  2. 2 4
      lib/GA.php
  3. 26 0
      models/Trafficsource.php

+ 61 - 0
controllers/IndexController.php

@@ -62,7 +62,9 @@ class IndexController extends Controller
     public function actionSynsdata(){
       $ga=new GA();
       $store="supernova";
+      // $start_day=
       $data=$ga->trafficsource($store);
+      $this->addSource($data,$store);
       exit;
       // $contentpages=$ga->contentpages($store);
       // $this->addContentpages($contentpages,$store);
@@ -70,6 +72,65 @@ class IndexController extends Controller
      
 
     }
+    public function addSource($data,$store){
+      $i=1;
+      $limit=200;
+      $modelKey=[
+        'date',
+        'store',
+        'source',
+        'channelGrouping',
+        'landingPagePath',
+        'keyword',
+        'socialNetwork',
+        'users',
+        'newUsers',
+        'sessions',
+        'bounceRate',
+        'pageviewsPerSession',
+        'avgSessionDuration',
+        'transactionsPerVisit',
+        'transactions',
+        'transactionRevenue',
+        'created_at',
+      ];
+      if(!empty($data)){
+        $list=[];
+        foreach($data as $k=>$v){
+          $item=[
+            date('Y-m-d',strtotime($v['date'])),
+            $store,
+            $v['source'],
+            $v['channelGrouping'],
+            $v['landingPagePath']!=="(not set)"?$v["landingPagePath"]:"",
+            $v['keyword']!=="(not set)"?$v['keyword']:"",
+            $v['socialNetwork']!=="(not set)"?$v['socialNetwork']:"",
+            $v['users'],
+            $v['uniquePageviews'],
+            $v['sessions'],
+            $v['bounceRate'],
+            $v['pageviewsPerSession'],
+            $v['avgSessionDuration'],
+            $v['transactionsPerVisit'],
+            $v['transactions'],
+            $v['transactionRevenue'],
+            time(),
+          ];
+          $list[]=$item;
+          if($i%$limit==0||$i==count($data)){
+            echo "<pre>";
+            print_r($list);exit;
+            \Yii::$app->db->createCommand()->batchInsert(Trafficsource::tableName(), $modelKey, $list)->execute();
+            $list=[];
+          }
+          $i++;
+        }
+      }
+    }
+
+
+
+
     public function addContentpages($contentpages,$store){
         $i=1;
         $limit=20;

+ 2 - 4
lib/GA.php

@@ -138,7 +138,7 @@ class GA{
         $analytics=self::getInstance($dp);
         $metricArr=[
             'users'=>"ga:users",
-            'uniquePageviews'=>"ga:newUsers",
+            'newUsers'=>"ga:newUsers",
             'sessions'=>"ga:sessions",
             'bounceRate'=>"ga:bounceRate",
             'pageviewsPerSession'=>"ga:pageviewsPerSession",
@@ -157,7 +157,7 @@ class GA{
         ];
         $VIEW_ID = $this->getViewId($dp);//
         $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
-        $start_at=date('Y-m-d',time()-24*3600);
+        $start_at=date('Y-m-d',time()-24*3600*7);
         $end_at=date('Y-m-d',time()-24*3600);
         $dateRange->setStartDate($start_at);
         $dateRange->setEndDate($end_at);
@@ -233,8 +233,6 @@ class GA{
         $reports= $analytics->reports->batchGet( $body );
 
         $result=$this->getResults($reports);
-        echo "<pre>";
-        print_r($result);exit;
          return $result;
 
     }

+ 26 - 0
models/Trafficsource.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace app\models;
+use yii\db\ActiveRecord;
+
+class Trafficsource extends ActiveRecord 
+{
+
+     function rules(){
+        return [
+            // name, email, subject 和 body 属性必须有值
+            [['store','date','channelGrouping'], 'required'],
+            [[
+                'landingPagePath','keyword',
+                'socialNetwork','users',
+                'newUsers','sessions',
+                'bounceRate','pageviewsPerSession',
+                'avgSessionDuration','transactionsPerVisit',
+                'transactions','transactionRevenue',
+            ], 'safe'],
+        ];
+    }
+   
+   
+    
+}