request; $store=$request->get('store'); $store=3; $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8)); $end_day = $request->get('end', date('Y-m-d',time()-3600*24)); $res=[]; try{ $storeConfig=[ 'dp'=>$store, 'metricArr'=>[ '销量'=>"ga:itemQuantity", '订单量'=>"ga:transactions", '订单金额'=>"ga:transactionRevenue", ], 'date'=>['start_at'=>$start_day,'end_at'=>$end_day] ]; $storeData=$ga->getData($storeConfig); $storeData=$storeData[0]; //广告 $adsConfig=[ 'dp'=>$store, 'dimensionArr'=>[ 'source'=>"ga:adwordsCampaignID", ], 'metricArr'=>[ '销量'=>"ga:itemQuantity", '订单量'=>"ga:transactions", '订单金额'=>"ga:transactionRevenue", '广告点击次数'=>"ga:adClicks",//点击次数 '广告花费'=>"ga:adCost",//成本 'CTR'=>"ga:CTR",//点击率 'ROAS'=>"ga:ROAS",//回报率 ], 'orderBy'=>['name'=>'ga:adClicks','order'=>'DESCENDING'], 'date'=>['start_at'=>$start_day,'end_at'=>$end_day] ]; $adsData=$ga->getData($adsConfig); $sells=[ 'hour'=>[ 'date'=>'ga:dateHour', ], 'day'=>[ 'date'=>"ga:date", ], 'week'=>[ 'date'=>'ga:yearWeek', ], 'month'=>[ 'date'=>'ga:yearMonth' ], ]; foreach($sells as $k=>$v){ $sellConfig=[ 'dp'=>$store, 'dimensionArr'=>$v, 'metricArr'=>[ '销量'=>"ga:itemQuantity", '订单量'=>"ga:transactions", '订单金额'=>"ga:transactionRevenue", ], 'date'=>['start_at'=>$start_day,'end_at'=>$end_day] ]; $sellData[$k]=$ga->getData($sellConfig); } echo "
";
        print_r($sellData);exit;
        







        $this->json(1,$adsData);
        $res['storeData']=$storeData;
      
        $storeSellData=$ga->storeSellData($store,$start_day,$end_day,'week');
        // $trafficsource=$ga->trafficsource_overview($store,$start_day,$end_day);
        // $googleAds=$ga->googleAds($store,$start_day,$end_day);
        
      
        
        
        // $product=$ga->product($store,$start_day,$end_day);
        // $visitors=$ga->visitors_type($store,$start_day,$end_day);
      }catch(\Exception $e){
         $this->json(0,[],$e->getMessage());
      }
      $this->json(1,$res);








    }
    public function actionContentpage(){
      $request = Yii::$app->request;
      \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
      $store = $request->get('store', "supernova");  
      $limit = $request->get('limit', "10");  
      $page = $request->get('page', "1"); 
      
      $page=$page-1>=0?$page-1:0;
      $where=[];
      if(!$store){
        return ['status'=>false,'msg'=>"参数异常"];
      }
      $where['store']=$store;

      $query=ContentPage::find()->where(["store"=>$where]);
      $count = $query->count();
      $pagination = new Pagination(['totalCount' => $count,'pageSize'=>$limit,'page'=>$page]);

      $items = $query->offset($pagination->offset)
          ->limit($pagination->limit)
          ->orderBy([
            'pageviews'=>SORT_DESC,
            'id'=>SORT_DESC
          ])
          ->all();
        foreach($items as $k=>$v){
          $items[$k]['created_at']=date('Y-m-d H:i:s',$v['created_at']);
        }
      $data=[
        'total'=>$count,
        'items'=>$items,
      ];
      
      return [
        'code' => 200,
        'data'=>$data,
        'status'=>true    
      ];
    }
    public function actionSource(){
      $request = Yii::$app->request;
      \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
      $store = $request->get('store', "supernova");  
      $limit = $request->get('limit', "10");  
      $page = $request->get('page', "1"); 
      
      $page=$page-1>=0?$page-1:0;
      $where=[];
      if(!$store){
        return ['status'=>false,'msg'=>"参数异常"];
      }
      $where['store']=$store;

      $query=Trafficsource::find()->where(["store"=>$where]);
      $count = $query->count();
      $pagination = new Pagination(['totalCount' => $count,'pageSize'=>$limit,'page'=>$page]);

      $items = $query->offset($pagination->offset)
          ->limit($pagination->limit)
          ->orderBy([
            'date'=>SORT_DESC,
            'users'=>SORT_DESC
          ])
          ->all();
        foreach($items as $k=>$v){
          $items[$k]['created_at']=date('Y-m-d H:i:s',$v['created_at']);
        }
      $data=[
        'total'=>$count,
        'items'=>$items,
      ];
      
      return [
        'code' => 200,
        'data'=>$data,
        'status'=>true    
      ];
    }

    public function actionSynsdata(){
      $ga=new GA();
      $store="supernova";
      $request = Yii::$app->request;
      $start_day = $request->get('start', date('Y-m-d'),time()-24*3600);
      $end_day=$start_day;

      $query=ContentPage::find()->where(["store"=>$store,'date'=>$start_day]);
      $count = $query->count();
      if($count>0){
        echo $start_day.$store." ContentPage"."已经执行过,请不要重复执行";
      }else{
        $contentpages=$ga->contentpages($store,$start_day,$end_day);
        $this->addContentpages($contentpages,$store);
      }

      $query=Trafficsource::find()->where(["store"=>$store,'date'=>$start_day]);
      $count = $query->count();
      if($count>0){
        echo $start_day.$store." Trafficsource"."已经执行过,请不要重复执行";
      }else{
        $contentpages=$ga->trafficsource($store,$start_day,$end_day);
        $this->addSource($contentpages,$store);
      }
      echo $start_day." 任务完成"."
"; exit; } 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['newUsers'], $v['sessions'], $v['bounceRate'], $v['pageviewsPerSession'], $v['avgSessionDuration'], $v['transactionsPerVisit'], $v['transactions'], $v['transactionRevenue'], time(), ]; $list[]=$item; if($i%$limit==0||$i==count($data)){ \Yii::$app->db->createCommand()->batchInsert(Trafficsource::tableName(), $modelKey, $list)->execute(); $list=[]; echo "addSource 任务完成".$i."个"."
"; } $i++; } echo "addSource 任务全部完成"."
"; } } public function actionStatistic(){ $ga=new GA(); $request = Yii::$app->request; $store=$request->get('store'); $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8)); $end_day = $request->get('end', date('Y-m-d',time()-3600*24)); if(!$store){ $this->json(0,[],"缺少参数"); } \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; //销量 try{ $sells=$ga->visitors_geo($store,$start_day,$end_day); $product=$ga->product($store,$start_day,$end_day); $visitors=$ga->visitors_type($store,$start_day,$end_day); }catch(\Exception $e){ $this->json(0,[],$e->getMessage()); } $data=[ 'sell'=>$sells, 'product'=>$product, 'visitor'=>$visitors, ]; $this->json(1,$data); } public function actionProductinfo(){ $ga=new GA(); $request = Yii::$app->request; $store=$request->get('store'); $productName=$request->get('productName'); $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8)); $end_day = $request->get('end', date('Y-m-d',time()-3600*24)); $productName=urldecode($productName); if(!$productName||!$store){ $this->json(0,[],'参数错误'); } $storeConfig=[ 'dp'=>$store, 'dimensionArr'=>[ 'productSku'=>"ga:productSku", ], 'metricArr'=>[ 'itemRevenue'=>"ga:itemRevenue", 'uniquePurchases'=>"ga:uniquePurchases", 'itemQuantity'=>"ga:itemQuantity", 'revenuePerItem'=>"ga:revenuePerItem", ], 'SegmentDimensionFilters'=>[ [ 'name'=>'ga:productName', 'operator'=>'EXACT', 'expression'=>[$productName] ] ], 'date'=>['start_at'=>$start_day,'end_at'=>$end_day] ]; $productInfo=$ga->getData($storeConfig); $this->json(1,$productInfo); } public function addContentpages($contentpages,$store){ $i=1; $limit=20; $modelKey=[ 'pagePath', 'store', 'date', 'pageviews', 'uniquePageviews', 'avgTimeOnPage', 'entrances', 'bounceRate', 'exitRate', 'pageValue', 'created_at', ]; if(!empty($contentpages)){ $list=[]; foreach($contentpages as $k=>$v){ $item=[ $v['pagePath'], $store, date('Y-m-d',strtotime($v['date'])), $v['pageviews'], $v['uniquePageviews'], $v['avgTimeOnPage'], $v['entrances'], $v['bounceRate'], $v['exitRate'], $v['pageValue'], time(), ]; $list[]=$item; if($i%$limit==0||$i==count($contentpages)){ \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute(); $list=[]; echo "addContentpages 任务完成".$i."个"."
"; } $i++; } echo "addContentpages 任务全部完成"."
"; } } public function actionRegionProduct(){ $ga=new GA(); $request = Yii::$app->request; $store=$request->get('store'); $region=$request->get('region'); if(!$region||!$store){ $this->json(0,[],'参数错误'); } $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8)); $end_day = $request->get('end', date('Y-m-d',time()-3600*24)); try{ $productInfo=$ga->regionProduct($region,$store,$start_day,$end_day); }catch(\Exception $e){ $this->json(0,[],$e->getMessage()); } $this->json(1,$productInfo); } public function actionGoals(){ $ga=new GA(); $request = Yii::$app->request; $store=$request->get('store'); if(!$store){ $this->json(0,[],'参数错误'); } $metricArr=[ 'pageviews'=>"ga:pageviews", 'sessions'=>"ga:sessions", 'users'=>"ga:users", 'newUsers'=>"ga:newUsers", 'bounceRate'=>"ga:bounceRate", // 'goal3Completions'=>"ga:goal3Completions", // 'goal3ConversionRate'=>"ga:goal3ConversionRate", // 'goal7Completions'=>"ga:goal7Completions", // 'goal7ConversionRate'=>"ga:goal7ConversionRate", 'transactionRevenue'=>"ga:transactionRevenue", 'revenuePerTransaction'=>"ga:revenuePerTransaction", ]; switch($store){ case "1"://supernovahair $metricArr['cartCompletions']="ga:goal8Completions"; $metricArr['cartConversionRate']="ga:goal8ConversionRate"; $metricArr['orderCompletions']="ga:goal7Completions"; $metricArr['orderConversionRate']="ga:goal7ConversionRate"; break; case "2"://asteriahair $metricArr['cartCompletions']="ga:goal12Completions"; $metricArr['cartConversionRate']="ga:goal12ConversionRate"; $metricArr['orderCompletions']="ga:goal14Completions"; $metricArr['orderConversionRate']="ga:goal14ConversionRate"; break; case "3"://alipearlhair $metricArr['cartCompletions']="ga:goal3Completions"; $metricArr['cartConversionRate']="ga:goal3ConversionRate"; $metricArr['orderCompletions']="ga:goal7Completions"; $metricArr['orderConversionRate']="ga:goal7ConversionRate"; break; case "4"://westkiss $metricArr['cartCompletions']="ga:goal3Completions"; $metricArr['cartConversionRate']="ga:goal3ConversionRate"; $metricArr['orderCompletions']="ga:goal2Completions"; $metricArr['orderConversionRate']="ga:goal2ConversionRate"; break; case "5"://yolissahair $metricArr['cartCompletions']="ga:goal2Completions"; $metricArr['cartConversionRate']="ga:goal2ConversionRate"; $metricArr['orderCompletions']="ga:goal3Completions"; $metricArr['orderConversionRate']="ga:goal3ConversionRate"; break; case "6"://wiggins $metricArr['cartCompletions']="ga:goal1Completions"; $metricArr['cartConversionRate']="ga:goal1ConversionRate"; $metricArr['orderCompletions']="ga:goal3Completions"; $metricArr['orderConversionRate']="ga:goal3ConversionRate"; break; } $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8)); $end_day = $request->get('end', date('Y-m-d',time()-3600*24)); $chunks=array_chunk($metricArr,6,1); $data=[]; foreach($chunks as $arr){ $goals=$ga->goals($store,$arr,$start_day,$end_day); $data= empty($data)?$goals[0]:array_merge_recursive($data,$goals[0]); } $this->json(1,$data); } }