[ '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() { $analytics=$this->initializeAnalytics(); $metrics=[ 'sessions'=>"ga:sessions", 'bounceRate'=>"ga:bounceRate", 'sessionDuration'=>"ga:sessionDuration", 'bounceRate'=>"ga:bounceRate", 'hits'=>"ga:hits", 'transactions'=>"ga:transactions", 'transactionsPerSession'=>"ga:transactionsPerSession", 'transactionRevenue'=>"ga:transactionRevenue", 'totalValue'=>"ga:totalValue", 'itemQuantity'=>"ga:itemQuantity", 'productAddsToCart'=>"ga:productAddsToCart", 'productCheckouts'=>"ga:productCheckouts", ]; $i=1; $datas=array_chunk($metrics,2,true); foreach($datas as $data){ $response = $this->getReport($analytics,$data); // $this->printResults($response); $res=$this->getResults($response); echo "
";
                print_r($res);
          }
        exit;
        return $this->render('index');
    }

    function initializeAnalytics()
{

  // Use the developers console and download your service account
  // credentials in JSON format. Place them in this directory or
  // change the key file location if necessary.
  $KEY_FILE_LOCATION = BASE_DIR . 'config/savvy-theory-278005-43552bf9fe94.json';

  // Create and configure a new client object.
  $client = new \Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  $analytics = new \Google_Service_AnalyticsReporting($client);

  return $analytics;
}

/**
 * Queries the Analytics Reporting API V4.
 *
 * @param service An authorized Analytics Reporting API V4 service object.
 * @return The Analytics Reporting API V4 response.
 */
function getReport($analytics,$data) {
  $VIEW_ID = "155703065";
  // Create the DateRange object.
  $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  $dateRange->setStartDate("7daysAgo");
  $dateRange->setEndDate("today");
  $metrics=[];
  $dimensions=[];

  foreach($data as $k=>$v){
    $Metric = new \Google_Service_AnalyticsReporting_Metric();
    $Metric->setExpression($v);
    $Metric->setAlias($k);
    array_push($metrics,$Metric);
  }
  $date = new \Google_Service_AnalyticsReporting_Dimension();
  $date->setName("ga:date");
  array_push($dimensions,$date);
  // Create the ReportRequest object.
  $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  $request->setViewId($VIEW_ID);
  $request->setDateRanges($dateRange);
  $request->setMetrics($metrics);
  $request->setDimensions($dimensions);

  $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  $body->setReportRequests( array( $request) );
  return $analytics->reports->batchGet( $body );
}

/**
 * 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"
"; } } 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"
"; } } } } } } 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 "
";
        if($dimensionHeaders&&$dimensions){
        //   for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
        //         $list[$dimensionHeaders[$i]]=
        //         print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
        //         echo"
"; // } $date=$dimensions[0]; }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[$day][$entry->getName()]=$values[$k]; // print($entry->getName() . ": " . $values[$k] . "\n\r"); // echo"
"; } } } } } return $data; } }