IndexController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\filters\AccessControl;
  5. use yii\web\Controller;
  6. use yii\web\Response;
  7. use yii\filters\VerbFilter;
  8. use app\models\LoginForm;
  9. use app\models\ContactForm;
  10. class IndexController extends Controller
  11. {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function behaviors()
  16. {
  17. return [
  18. 'access' => [
  19. 'class' => AccessControl::className(),
  20. 'only' => ['logout'],
  21. 'rules' => [
  22. [
  23. 'actions' => ['logout'],
  24. 'allow' => true,
  25. 'roles' => ['@'],
  26. ],
  27. ],
  28. ],
  29. 'verbs' => [
  30. 'class' => VerbFilter::className(),
  31. 'actions' => [
  32. 'logout' => ['post'],
  33. ],
  34. ],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function actions()
  41. {
  42. return [
  43. 'error' => [
  44. 'class' => 'yii\web\ErrorAction',
  45. ],
  46. 'captcha' => [
  47. 'class' => 'yii\captcha\CaptchaAction',
  48. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  49. ],
  50. ];
  51. }
  52. /**
  53. * Displays homepage.
  54. *
  55. * @return string
  56. */
  57. public function actionIndex()
  58. {
  59. $analytics=$this->initializeAnalytics();
  60. $response = $this->getReport($analytics);
  61. printResults($response);
  62. exit;
  63. return $this->render('index');
  64. }
  65. function initializeAnalytics()
  66. {
  67. // Use the developers console and download your service account
  68. // credentials in JSON format. Place them in this directory or
  69. // change the key file location if necessary.
  70. $KEY_FILE_LOCATION = BASE_DIR . 'config/savvy-theory-278005-43552bf9fe94.json';
  71. // Create and configure a new client object.
  72. $client = new \Google_Client();
  73. $client->setApplicationName("Hello Analytics Reporting");
  74. $client->setAuthConfig($KEY_FILE_LOCATION);
  75. $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  76. $analytics = new \Google_Service_AnalyticsReporting($client);
  77. return $analytics;
  78. }
  79. /**
  80. * Queries the Analytics Reporting API V4.
  81. *
  82. * @param service An authorized Analytics Reporting API V4 service object.
  83. * @return The Analytics Reporting API V4 response.
  84. */
  85. function getReport($analytics) {
  86. // Replace with your view ID, for example XXXX.
  87. $VIEW_ID = "155703065";
  88. // Create the DateRange object.
  89. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  90. $dateRange->setStartDate("7daysAgo");
  91. $dateRange->setEndDate("today");
  92. // Create the Metrics object.
  93. $sessions = new \Google_Service_AnalyticsReporting_Metric();
  94. $sessions->setExpression("ga:sessions");
  95. $sessions->setAlias("sessions");
  96. // Create the ReportRequest object.
  97. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  98. $request->setViewId($VIEW_ID);
  99. $request->setDateRanges($dateRange);
  100. $request->setMetrics(array($sessions));
  101. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  102. $body->setReportRequests( array( $request) );
  103. return $analytics->reports->batchGet( $body );
  104. }
  105. /**
  106. * Parses and prints the Analytics Reporting API V4 response.
  107. *
  108. * @param An Analytics Reporting API V4 response.
  109. */
  110. function printResults($reports) {
  111. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  112. $report = $reports[ $reportIndex ];
  113. $header = $report->getColumnHeader();
  114. $dimensionHeaders = $header->getDimensions();
  115. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  116. $rows = $report->getData()->getRows();
  117. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  118. $row = $rows[ $rowIndex ];
  119. $dimensions = $row->getDimensions();
  120. $metrics = $row->getMetrics();
  121. for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  122. print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
  123. }
  124. for ($j = 0; $j < count($metrics); $j++) {
  125. $values = $metrics[$j]->getValues();
  126. for ($k = 0; $k < count($values); $k++) {
  127. $entry = $metricHeaders[$k];
  128. print($entry->getName() . ": " . $values[$k] . "\n");
  129. }
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * Login action.
  136. *
  137. * @return Response|string
  138. */
  139. public function actionLogin()
  140. {
  141. if (!Yii::$app->user->isGuest) {
  142. return $this->goHome();
  143. }
  144. $model = new LoginForm();
  145. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  146. return $this->goBack();
  147. }
  148. $model->password = '';
  149. return $this->render('login', [
  150. 'model' => $model,
  151. ]);
  152. }
  153. /**
  154. * Logout action.
  155. *
  156. * @return Response
  157. */
  158. public function actionLogout()
  159. {
  160. Yii::$app->user->logout();
  161. return $this->goHome();
  162. }
  163. /**
  164. * Displays contact page.
  165. *
  166. * @return Response|string
  167. */
  168. public function actionContact()
  169. {
  170. $model = new ContactForm();
  171. if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  172. Yii::$app->session->setFlash('contactFormSubmitted');
  173. return $this->refresh();
  174. }
  175. return $this->render('contact', [
  176. 'model' => $model,
  177. ]);
  178. }
  179. /**
  180. * Displays about page.
  181. *
  182. * @return string
  183. */
  184. public function actionAbout()
  185. {
  186. return $this->render('about');
  187. }
  188. }