| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672 | <?php defined('BASEPATH') OR exit('No direct script access allowed');class Ga extends Start_Controller {	public function __construct(){		parent::__construct();		$this->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 "<pre>";			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 "<pre>"; 				// 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 "<pre>";			// 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;    }}
 |