load->library('session'); $this->load->_model('Model_shop','shop'); } public function _remap($arg,$arg_array) { if($arg=="contentpage"){ $this->$arg(); }elseif($arg=="trafficsource"){ $this->$arg(); }elseif($arg=="chart"){ $this->$arg(); }elseif($arg=="productinfo"){ $this->$arg($arg_array); }elseif($arg=="regionproduct"){ $this->$arg($arg_array); }elseif($arg=="goals"){ $this->$arg(); }else{ $this->_index(); } } public function _index(){ $url="http://getdata.mywigs.com/index/index"; if(isset($_SESSION['api'])) { $user = $this->user->get_api($_SESSION['api']); $usp = $user; $fgshop = "";$sid = ""; $usersp = explode('|',trim($user['shop'],'|')); foreach ($usersp as $value) { $fgshop .= " shop = ".$value." or"; $sid .= " id = ".$value." or"; } if($user['vip'] == 1) { $vip = 1; } else { $vip = 0; } } else { $vip = 0; } $wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or')); $this->data['wlshop'] = $wlshop; $post = $this->input->post(NULL, TRUE); $shop = $this->input->post('shop', TRUE); if(!$shop){ $shop=$wlshop[0]; } $this->data['shop'] = $shop; if(isset($post['shop'])) { $param=[]; $days=intval($post['days']); if($shop>6){ echo json_encode(array('msg'=>'无权限或者店铺不存在','success'=>false));exit; } $param['store']=$shop; $param['start']=date('Y-m-d',time()-3600*24*$days); $param['end']=date('Y-m-d',time()); $url=$url.'?'.http_build_query($param); $data=$this->_curl([],$url,300,"GET"); $data=json_decode($data,true); $last_data_url=$url.http_build_query([ 'store'=>$shop, 'start'=>date('Y-m-d',time()-3600*24*$days*2), 'end'=>date('Y-m-d',time()-3600*24*$days), ]); $last_data=$this->_curl([],$last_data_url,300,"GET"); $last_data=json_decode($last_data,true); if(!$data['status']||!$last_data['status']){ echo json_encode(array('msg'=>'获取失败,请重试','success'=>false));exit; }else{ $data=$data['data']; $last_data=$last_data['data']; $users=[]; $visitor=$data['visitor']; $geo=$data['geo']; foreach($visitor as $k=>$v){ if($v['userType']=="New Visitor"){ $users['new'][]=$v['newUsers']; $users['date'][]=date('Y-m-d',strtotime($v['date'])); }else{ $users['return'][]=$v['users']; } } $totalSellCount=array_sum(array_column($geo,'transactions')); $regionStr=""; foreach($geo as $k=>$v){ $percent=round($v['transactions']/$totalSellCount*100,2)."%"; $geo[$k]['percent']=$percent; } $sell=[]; $sell['date']['hour']=array_column($data['sell']['hour'],'dateHour'); $sell['date']['day']=array_column($data['sell']['day'],'date'); $sell['date']['week']=array_column($data['sell']['week'],'isoYearIsoWeek'); $sell['date']['month']=array_column($data['sell']['month'],'yearMonth'); $sell['date']['hour']=array_map(function($item){ return date('m/d H',strtotime($item."00")).":00"; },$sell['date']['hour']); $sell['date']['day']=array_map(function($item){ return date('m/d',strtotime($item)); },$sell['date']['day']); $sell['date']['week']=array_map(function($item){ return date('Y/W',strtotime(substr_replace($item,"W",-2,0))); },$sell['date']['week']); $sell['date']['month']=array_map(function($item){ return date('Y/m',strtotime($item)); },$sell['date']['month']); foreach($data['sell'] as $k=>$v){ foreach($v as $item){ if(isset($item['date'])&&!empty($item['date'])){ $date=date('m/d',strtotime($item['date'])); } if(isset($item['dateHour'])&&!empty($item['dateHour'])){ $date=date('m/d H',strtotime($item['dateHour']."00")).":00"; } if(isset($item['yearMonth'])&&!empty($item['yearMonth'])){ $date=date('Y-m',strtotime($item['yearMonth'])); } if(isset($item['isoYearIsoWeek'])&&!empty($item['isoYearIsoWeek'])){ $date=date('Y/W',strtotime(substr_replace($item['isoYearIsoWeek'],"W",-2,0))); } $item['date']=$date; // $date=$item['date']??($item['dateHour']??($item['yearWeek']??$item['yearMonth'])); $sell['data'][$k][$date]=$item; } } foreach($last_data['sell'] as $k=>$v){ foreach($v as $item){ $last_sell[$k][]=$item['itemQuantity']; } } $data['store']['广告-销量']=0; $data['store']['广告-订单数']=0; $data['store']['广告-收入']=0; $data['store']['广告-点击次数']=0; $data['store']['广告-成本']=0; foreach($data['ads'] as $k=>$v){ if($v['campaign']=="(not set)"){ continue; } $data['store']['广告-销量'] +=$v['itemQuantity']; $data['store']['广告-订单数'] +=$v['transactions']; $data['store']['广告-收入'] +=$v['transactionRevenue']; $data['store']['广告-点击次数'] +=$v['adClicks']; $data['store']['广告-成本'] +=$v['adCost']; } $data['store']['广告-收入']=round($data['store']['广告-收入'],2); $data['store']['广告-成本']=round($data['store']['广告-成本'],2); $page=array_slice($data['page'],0,99); foreach($page as $k=>$v){ $page[$k]['pageValue']=round($v['pageValue'],2); } $rows=array( 'success'=>1, 'data'=>[ 'users'=>$users, 'geo'=>$geo, 'product'=>$data['product'], 'ads'=>$data['ads'], 'sell'=>$sell, 'last_sell'=>$last_sell, 'store'=>$data['store'], 'page'=>$page, 'channel'=>$data['channel'] ] ); echo json_encode($rows);exit; } } $this->_Template('ga_index',$this->data); } public function getStoreData(){ $base_url="http://getdata.mywigs.com/index/index"; $post = $this->input->post(NULL, TRUE); // $shop=$post['store']; // $days=intval($post['days']); $shop=3; $days=15; $param['store']=$shop; $param['start']=date('Y-m-d',time()-3600*24*$days); $param['end']=date('Y-m-d',time()); $url=$base_url.'?'.http_build_query($param); $data=$this->_curl([],$url,300,"GET"); $data=json_decode($data,true); $param1['store']=$shop; $param1['start']=date('Y-m-d',time()-3600*24*($days*2+1)); $param1['end']=date('Y-m-d',time()-3600*24*($days+1)); $url=$base_url.'?'.http_build_query($param1); $last_data=$this->_curl([],$url,300,"GET"); $last_data=json_decode($last_data,true); if(!$data['status']||!$last_data['status']){ echo json_encode(array('msg'=>'获取失败,请重试','success'=>false));exit; }else{ $storeData=$data['data']['storeData']; $lastStoreData=$last_data['data']['storeData']; echo "
";
			print_r($storeData);exit;
			
			$res=[
				[
					'name'=>'销量',
					'value'=>$storeData['transactions'],
					// 'compare'=>round(($storeData['transactions']-$lastStoreData['transactions'])/$lastStoreData['transactions']*100,2)."%",
					'compare'=>round(($lastStoreData['transactions']-$storeData['transactions'])/$lastStoreData['transactions']*100,2)."%",
				],
				// [
				// 	'name'=>
				// ]
			];


			echo json_encode($res);
			exit;
		}
		




	}
	public function contentpage(){
		$url="http://getdata.mywigs.com/index/contentpage";
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['page']))  {
			$page = $this->input->post('page',true);
			$perpage = $this->input->post('perpage',true);
			
			if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
            
			$param=[];
			$param['limit']=$perpage;
            $param['page']=$page;
            $url=$url.'?'.http_build_query($param);
			$data=$this->_curl([],$url,300,"GET");
            $data=json_decode($data,true);
			if(!$data['status']){
				echo json_encode(array('msg'=>'获取失败,请重试','success'=>false));exit;
			}else{
				$data=$data['data'];
				$total=$data['total'];
				$items=$data['items'];
				foreach($items as $k=>$v){
					$items[$k]['avgTimeOnPage']=round($items[$k]['avgTimeOnPage'],2);
					$items[$k]['bounceRate']=round($items[$k]['bounceRate'],2);
					$items[$k]['exitRate']=round($items[$k]['exitRate'],2);
					$items[$k]['pageValue']=round($items[$k]['pageValue'],2);
					unset($items[$k]['created_at']);
				}
				$pagenum = ceil($total/$perpage);
				$over = $total-($start+$perpage);
				$rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($items));
				echo json_encode($rows);exit;
			}
		}
		$this->_Template('ga_contentpage',$this->data);

    }
    // public function Trafficsource(){
	// 	$url="http://getdata.mywigs.com/index/source";
	// 	$post = $this->input->post(NULL, TRUE);
	// 	if(isset($post['page']))  {
	// 		$page = $this->input->post('page',true);
	// 		$perpage = $this->input->post('perpage',true);
			
	// 		if(empty($page))
	// 	    {
    //             $start = 0;
	// 	    	$perpage = 1;
    //         }
	// 	    else
	// 	    {
    //             $start = ($page - 1)*$perpage;
    //         }
            
	// 		$param=[];
	// 		$param['limit']=$perpage;
    //         $param['page']=$page;
    //         $url=$url.'?'.http_build_query($param);
	// 		$data=$this->_curl([],$url,300,"GET");
    //         $data=json_decode($data,true);
	// 		if(!$data['status']){
	// 			echo json_encode(array('msg'=>'获取失败,请重试','success'=>false));exit;
	// 		}else{
	// 			$data=$data['data'];
	// 			$total=$data['total'];
	// 			$items=$data['items'];
	// 			foreach($items as $k=>$v){

	// 				$items[$k]['pageviewsPerSession']=round($items[$k]['pageviewsPerSession'],2);
	// 				$items[$k]['bounceRate']=round($items[$k]['bounceRate'],2);
	// 				$items[$k]['avgSessionDuration']=round($items[$k]['avgSessionDuration'],2);
	// 				$items[$k]['transactionsPerVisit']=round($items[$k]['transactionsPerVisit'],2);
	// 				unset($items[$k]['created_at']);
	// 			}
	// 			$pagenum = ceil($total/$perpage);
	// 			$over = $total-($start+$perpage);
	// 			$rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($items));
	// 			echo json_encode($rows);exit;
	// 		}
	// 	}
	// 	$this->_Template('ga_trafficsource',$this->data);

	// }
	public function Trafficsource(){
		$url="http://getdata.mywigs.com/index/statistic";
		if(isset($_SESSION['api']))
		{
			$user = $this->user->get_api($_SESSION['api']);
			$usp = $user;
		    $fgshop = "";$sid = "";
		    $usersp = explode('|',trim($user['shop'],'|'));
			foreach ($usersp as $value) 
		    {
				$fgshop .= " shop = ".$value." or";
				$sid .= " id = ".$value." or";
			}
			if($user['vip'] == 1)
			{
				$vip = 1;
			}
			else
			{
				$vip = 0;
			}
		}
		else
		{
			$vip = 0;
		}

		$wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or'));
		$this->data['wlshop'] = $wlshop;
		

		$post = $this->input->post(NULL, TRUE);
		$shop = $this->input->post('shop', TRUE);

		if(!$shop){
			$shop=$wlshop[0];
		}
		
		$this->data['shop'] = $shop;

		if(isset($post['shop']))  {
			$param=[];
			$days=intval($post['days']);
			if($shop>6){
				echo json_encode(array('msg'=>'无权限或者店铺不存在','success'=>false));exit;
			}
			$param['store']=$shop;
			$param['start']=date('Y-m-d',time()-3600*24*$days);
			$param['end']=date('Y-m-d',time());
            $url=$url.'?'.http_build_query($param);
			$data=$this->_curl([],$url,300,"GET");
			$data=json_decode($data,true);
			if(!$data['status']){
				echo json_encode(array('msg'=>'获取失败,请重试','success'=>false));exit;
			}else{
				$data=$data['data'];
				$users=[];
				$visitor=$data['visitor'];
				$sell=$data['sell'];
				foreach($visitor as $k=>$v){
					if($v['userType']=="New Visitor"){
						$users['new'][]=$v['newUsers'];
						$users['date'][]=date('Y-m-d',strtotime($v['date']));
					}else{
						$users['return'][]=$v['users'];
					}
				}
				$totalSellCount=array_sum(array_column($sell,'transactions'));
				// echo $totalSellCount;exit;
				$regionStr="";
				foreach($sell as $k=>$v){
					$percent=round($v['transactions']/$totalSellCount*100,2)."%";
					$sell[$k]['percent']=$percent;
				}

				$rows=array(
					'success'=>1,
					'data'=>[
						'users'=>$users,
						'sell'=>$sell,
						'product'=>$data['product'],
					]
				);
				echo json_encode($rows);exit;
			}
		}
		$this->_Template('ga_chart',$this->data);

	}
	public function productinfo($arg_array=[]){
		$url="http://getdata.mywigs.com/index/productinfo";
		$shop=$arg_array[0];
		$days=intval($arg_array[1])??7;
		$productName=base64_decode($arg_array[2]);

		if(isset($productName)&&!empty($productName))  {
			$param=[];
			$param['store']=$shop;
			$param['productName']=$productName;
			$param['start']=date('Y-m-d',time()-3600*24*$days);
			$param['end']=date('Y-m-d',time());
			$url=$url.'?'.http_build_query($param);
			$data=$this->_curl([],$url,300,"GET");
			$data=json_decode($data,true);
			if(!$data['status']){
				echo json_encode(array('msg'=>'获取失败,请重试','success'=>false));exit;
			}else{
				$data=$data['data'];

				$sell=[];
				$sell['date']['hour']=array_column($data['sell']['hour'],'dateHour');
				$sell['date']['day']=array_column($data['sell']['day'],'date');
				$sell['date']['week']=array_column($data['sell']['week'],'isoYearIsoWeek');
				$sell['date']['month']=array_column($data['sell']['month'],'yearMonth');

				$sell['date']['hour']=array_map(function($item){
					return date('m/d H',strtotime($item."00")).":00";
				},$sell['date']['hour']);

				$sell['date']['day']=array_map(function($item){
					return date('m/d',strtotime($item));
				},$sell['date']['day']);
				$sell['date']['week']=array_map(function($item){
					return date('Y/W',strtotime(substr_replace($item,"W",-2,0)));
				},$sell['date']['week']);

				$sell['date']['month']=array_map(function($item){
					return date('Y/m',strtotime($item));
				},$sell['date']['month']);
				// echo "
"; 
				// print_r($data['sell']['week']);
				foreach($data['sell'] as $k=>$v){
					foreach($v as $item){
						if(isset($item['date'])&&!empty($item['date'])){
							$date=date('m/d',strtotime($item['date']));
						}
						if(isset($item['dateHour'])&&!empty($item['dateHour'])){
							$date=date('m/d H',strtotime($item['dateHour']."00")).":00";
						}
						if(isset($item['yearMonth'])&&!empty($item['yearMonth'])){
							$date=date('Y-m',strtotime($item['yearMonth']));
						}

						if(isset($item['isoYearIsoWeek'])&&!empty($item['isoYearIsoWeek'])){
							$date=date('Y/W',strtotime(substr_replace($item['isoYearIsoWeek'],"W",-2,0)));
						}
						$item['date']=$date;
						// $date=$item['date']??($item['dateHour']??($item['yearWeek']??$item['yearMonth']));
						$sell['data'][$k][$date]=$item;
					}
				}
			}
			// echo "
";
			// print_r($sell);exit;
			$this->data['sell']=json_encode($sell,true);
			$this->data['sku']=json_encode($data['sku'],true);
			$this->data['days']=$days;
		}
		$this->data['productName']=$productName;
		$this->_Template('ga_productinfo',$this->data);

	}
	public function regionproduct($arg_array=[]){
		$url="http://getdata.mywigs.com/index/region-product";
		$shop=$arg_array[0];
		$days=intval($arg_array[1])??7;
		$region=base64_decode($arg_array[2]);
		if(isset($region)&&!empty($region))  {
			$param=[];
			$param['store']=$shop;
			$param['region']=$region;
			$param['start']=date('Y-m-d',time()-3600*24*$days);
			$param['end']=date('Y-m-d',time());
			$url=$url.'?'.http_build_query($param);
			$data=$this->_curl([],$url,300,"GET");
			$data=json_decode($data,true);
			if(!$data['status']){
				echo json_encode(array('msg'=>'获取失败,请重试','success'=>false));exit;
			}else{
				$data=$data['data'];
			}
			$this->data['info']=$data[0];
		}
		$this->_Template('ga_productinfo',$this->data);

	}
	public function goals(){
		$url="http://getdata.mywigs.com/index/goals";
		if(isset($_SESSION['api']))
		{
			$user = $this->user->get_api($_SESSION['api']);
			$usp = $user;
		    $fgshop = "";$sid = "";
		    $usersp = explode('|',trim($user['shop'],'|'));
			foreach ($usersp as $value) 
		    {
				$fgshop .= " shop = ".$value." or";
				$sid .= " id = ".$value." or";
			}
			if($user['vip'] == 1)
			{
				$vip = 1;
			}
			else
			{
				$vip = 0;
			}
		}
		else
		{
			$vip = 0;
		}

		$wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or'));
		$this->data['wlshop'] = $wlshop;
		

		$post = $this->input->post(NULL, TRUE);
		$shop = $this->input->post('shop', TRUE);

		if(!$shop){
			$shop=$wlshop[0];
		}
		
		$this->data['shop'] = $shop;
		
		

		if(isset($post['page']))  {
			$param=[];
			$timetk = $this->input->post('timetk',true);
			$timetj = $this->input->post('timetj',true);	
			// $days=intval($post['days']);
			if($shop>6){
				echo json_encode(array('msg'=>'无权限或者店铺不存在','success'=>false));exit;
			}
			$param['store']=$shop;
			$param['start']=date('Y-m-d',strtotime($timetk));
			$param['end']=date('Y-m-d',strtotime($timetj));
			$url=$url.'?'.http_build_query($param);
			$data=$this->_curl([],$url,300,"GET");
			$data=json_decode($data,true);
			if(!$data['status']){
				echo json_encode(array('msg'=>'获取失败,请重试','success'=>false));exit;
			}else{
				$data=$data['data'];
				// foreach($sell as $k=>$v){
				// 	$percent=round($v['transactions']/$totalSellCount*100,2)."%";
				// 	$sell[$k]['percent']=$percent;
				// }
				$row=[
					$data['pageviews'],
					$data['sessions'],
					$data['users'],
					$data['newUsers'],
					round($data['bounceRate'],2).'%',
					$data['cartCompletions'],
					round($data['cartConversionRate'],2).'%',
					$data['orderCompletions'],
					round($data['orderConversionRate'],2).'%',
					$data['transactionRevenue'],
					round($data['revenuePerTransaction'],2),
				];
				$rows = array('rows'=>[$row]);
				echo json_encode($rows);exit;
			}
		}
		$this->_Template('ga_goals',$this->data);
	}


	public function _curl($data,$url,$timeout=300,$httptype="POST",$date_type=false,$header=array(),$userpwd='')
    {
    
        if ($date_type == 'http_build_query')
		{
            $data = http_build_query($data);
        }
		else if ($date_type == 'json')
		{
            $data = json_encode($data);
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        switch ($httptype)
		{
            case "GET":
            curl_setopt($ch, CURLOPT_HTTPGET, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
            break;
			
            case "POST":
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            break;
			
            case "PUT":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            break;
			
            case "DELETE":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            break;
        }
        $isSecure = strpos($url, "https://");
        if ($isSecure === 0)
		{
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        }
        if(!empty($header))
		{
            curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
            curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
        }

        if(!empty($userpwd))
		{
            curl_setopt($ch,CURLOPT_USERPWD,$userpwd);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}