[
'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();
$response = $this->getReport($analytics);
$this->printResults($response);
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) {
// Replace with your view ID, for example XXXX.
$VIEW_ID = "155703065";
// Create the DateRange object.
$dateRange = new \Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");
$metrics=[];
$dimensions=[];
// Create the Metrics object.
$sessions = new \Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
array_push($metrics,$sessions);
// $users = new \Google_Service_AnalyticsReporting_Metric();
// $users->setExpression("ga:users");
// $users->setAlias("users");
// array_push($metrics,$users);
//跳出率
$bounceRate = new \Google_Service_AnalyticsReporting_Metric();
$bounceRate->setExpression("ga:bounceRate");
$bounceRate->setAlias("bounceRate");
array_push($metrics,$bounceRate);
//
$sessionDuration = new \Google_Service_AnalyticsReporting_Metric();
$sessionDuration->setExpression("ga:sessionDuration");
$sessionDuration->setAlias("sessionDuration");
array_push($metrics,$sessionDuration);
// $hits = new \Google_Service_AnalyticsReporting_Metric();
// $hits->setExpression("ga:hits");
// $hits->setAlias("hits");
// array_push($metrics,$hits);
//交易阶段
$shoppingStage = new \Google_Service_AnalyticsReporting_Dimension();
$shoppingStage->setName("ga:shoppingStage");
array_push($dimensions,$shoppingStage);
//交易次数
$transactions = new \Google_Service_AnalyticsReporting_Metric();
$transactions->setExpression("ga:transactions");
$transactions->setAlias("transactions");
array_push($metrics,$transactions);
//电子商务转化率
$transactionsPerSession = new \Google_Service_AnalyticsReporting_Metric();
$transactionsPerSession->setExpression("ga:transactionsPerSession");
$transactionsPerSession->setAlias("transactionsPerSession");
array_push($metrics,$transactionsPerSession);
$transactionRevenue = new \Google_Service_AnalyticsReporting_Metric();
$transactionRevenue->setExpression("ga:transactionRevenue");
$transactionRevenue->setAlias("transactionRevenue");
array_push($metrics,$transactionRevenue);
//
$totalValue = new \Google_Service_AnalyticsReporting_Metric();
$totalValue->setExpression("ga:totalValue");
$totalValue->setAlias("totalValue");
array_push($metrics,$totalValue);
$itemQuantity = new \Google_Service_AnalyticsReporting_Metric();
$itemQuantity->setExpression("ga:itemQuantity");
$itemQuantity->setAlias("itemQuantity");
array_push($metrics,$itemQuantity);
$productAddsToCart = new \Google_Service_AnalyticsReporting_Metric();
$productAddsToCart->setExpression("ga:productAddsToCart");
$productAddsToCart->setAlias("productAddsToCart");
array_push($metrics,$productAddsToCart);
$productCheckouts = new \Google_Service_AnalyticsReporting_Metric();
$productCheckouts->setExpression("ga:productCheckouts");
$productCheckouts->setAlias("productCheckouts");
array_push($metrics,$productCheckouts);
// 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"
";
}
}
}
}
}
}
/**
* Login action.
*
* @return Response|string
*/
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
$model->password = '';
return $this->render('login', [
'model' => $model,
]);
}
/**
* Logout action.
*
* @return Response
*/
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
/**
* Displays contact page.
*
* @return Response|string
*/
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
/**
* Displays about page.
*
* @return string
*/
public function actionAbout()
{
return $this->render('about');
}
}