123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace app\controllers;
- use Yii;
- use yii\filters\AccessControl;
- use yii\web\Controller;
- use yii\web\Response;
- use yii\filters\VerbFilter;
- use app\lib\GA;
- use app\models\ContentPage;
- class IndexController extends Controller
- {
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'only' => ['logout'],
- 'rules' => [
- [
- 'actions' => ['logout'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post'],
- ],
- ],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- 'captcha' => [
- 'class' => 'yii\captcha\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- ],
- ];
- }
- /**
- * Displays homepage.
- *
- * @return string
- */
- public function actionIndex()
- {
- exit;
-
- $metrics=[
- 'sessions'=>"ga:sessions",
- 'bounceRate'=>"ga:bounceRate",
- 'sessionDuration'=>"ga:sessionDuration",
- 'hits'=>"ga:hits",
- 'transactions'=>"ga:transactions",
- 'transactionsPerSession'=>"ga:transactionsPerSession",
- 'transactionRevenue'=>"ga:transactionRevenue",
- 'totalValue'=>"ga:totalValue",
- 'itemQuantity'=>"ga:itemQuantity",
- 'productAddsToCart'=>"ga:productAddsToCart",
- 'productCheckouts'=>"ga:productCheckouts",
- 'productDetailViews'=>"ga:productDetailViews",
- ];
- $i=1;
- $res=[];
- $metrics=array_chunk($metrics,6,true);
- foreach($metrics as $metric){
- $response = $this->getReport($analytics,$metric);
- // $this->printResults($response);
- $data=$this->getResults($response);
- if(empty($res)){
- $res=$data;
- }else{
- echo "<pre>";
- print_r($res);
- print_r($data);
- echo "1111111\r\n";
- $res=array_merge_recursive($res,$data);
- echo "<pre>";
- print_r($res);
- exit;
- }
- // if(!empty($data)){
- // $res=empty($res)?$data:array_merge_recursive($res,$data);
- // }
- }
- echo "<pre>";
- print_r($res);
- exit;
- return $this->render('index');
- }
- public function actionSynsdata(){
- $ga=new GA();
- $store="supernova";
- $contentpages=$ga->contentpages($store);
- $i=1;
- $limit=20;
- $modelKey=[
- 'pagePath',
- 'store',
- 'date',
- 'pageviews',
- 'uniquePageviews',
- 'avgTimeOnPage',
- 'entrances',
- 'bounceRate',
- 'exitRate',
- 'pageValue',
- 'created_at',
- ];
- if(!empty($contentpages)){
- foreach($contentpages as $k=>$v){
- $list=[];
- $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 111111;exit;
- }
- $i++;
- }
- }
- echo 111111;exit;
- }
-
- /**
- * Parses and prints the Analytics Reporting API V4 response.
- *
- * @param An Analytics Reporting API V4 response.
- */
- function printResults($reports) {
- for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
- $report = $reports[ $reportIndex ];
- $header = $report->getColumnHeader();
- $dimensionHeaders = $header->getDimensions();
- $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
- $rows = $report->getData()->getRows();
- for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
- $row = $rows[ $rowIndex ];
- $dimensions = $row->getDimensions();
- $metrics = $row->getMetrics();
- if($dimensionHeaders&&$dimensions){
- for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
- print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
- echo"<br/>";
- }
- }
-
- if($metricHeaders&&$metrics){
- for ($j = 0; $j < count($metrics); $j++) {
- $values = $metrics[$j]->getValues();
- for ($k = 0; $k < count($values); $k++) {
- $entry = $metricHeaders[$k];
- print($entry->getName() . ": " . $values[$k] . "\n\r");
- echo"<br/>";
- }
- }
- }
-
- }
- }
- }
- function getResults($reports) {
- $data=[];
- for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
- $item=[];
- $report = $reports[ $reportIndex ];
-
- $header = $report->getColumnHeader();
- $dimensionHeaders = $header->getDimensions();
- $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
- $rows = $report->getData()->getRows();
-
-
- for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
- $row = $rows[ $rowIndex ];
- $dimensions = $row->getDimensions();
-
- $metrics = $row->getMetrics();
- // echo "<pre>";
- if($dimensionHeaders&&$dimensions){
- // for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
- // $list[$dimensionHeaders[$i]]=
- // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
- // echo"<br/>";
- // }
- $date=$dimensions[0];
- $date=date('Y-m-d',strtotime($date));
- }else{
- continue;
- }
-
- if($metricHeaders&&$metrics){
- for ($j = 0; $j < count($metrics); $j++) {
- $values = $metrics[$j]->getValues();
- for ($k = 0; $k < count($values); $k++) {
- $entry = $metricHeaders[$k];
- $data[$date][$entry->getName()]=$values[$k];
- // print($entry->getName() . ": " . $values[$k] . "\n\r");
- // echo"<br/>";
- }
- }
- }
-
- }
- }
- return $data;
- }
- }
|