IndexController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. echo "<pre>";
  62. \print_r($response);
  63. echo "</pre>";exit;
  64. $this->printResults($response);
  65. exit;
  66. return $this->render('index');
  67. }
  68. function initializeAnalytics()
  69. {
  70. // Use the developers console and download your service account
  71. // credentials in JSON format. Place them in this directory or
  72. // change the key file location if necessary.
  73. $KEY_FILE_LOCATION = BASE_DIR . 'config/savvy-theory-278005-43552bf9fe94.json';
  74. // Create and configure a new client object.
  75. $client = new \Google_Client();
  76. $client->setApplicationName("Hello Analytics Reporting");
  77. $client->setAuthConfig($KEY_FILE_LOCATION);
  78. $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  79. $analytics = new \Google_Service_AnalyticsReporting($client);
  80. return $analytics;
  81. }
  82. /**
  83. * Queries the Analytics Reporting API V4.
  84. *
  85. * @param service An authorized Analytics Reporting API V4 service object.
  86. * @return The Analytics Reporting API V4 response.
  87. */
  88. function getReport($analytics) {
  89. // Replace with your view ID, for example XXXX.
  90. $VIEW_ID = "155703065";
  91. // Create the DateRange object.
  92. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  93. $dateRange->setStartDate("7daysAgo");
  94. $dateRange->setEndDate("today");
  95. // Create the Metrics object.
  96. $sessions = new \Google_Service_AnalyticsReporting_Metric();
  97. $sessions->setExpression("ga:sessions");
  98. $sessions->setAlias("sessions");
  99. // Create the ReportRequest object.
  100. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  101. $request->setViewId($VIEW_ID);
  102. $request->setDateRanges($dateRange);
  103. $request->setMetrics(array($sessions));
  104. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  105. $body->setReportRequests( array( $request) );
  106. return $analytics->reports->batchGet( $body );
  107. }
  108. /**
  109. * Parses and prints the Analytics Reporting API V4 response.
  110. *
  111. * @param An Analytics Reporting API V4 response.
  112. */
  113. function printResults($reports) {
  114. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  115. $report = $reports[ $reportIndex ];
  116. $header = $report->getColumnHeader();
  117. $dimensionHeaders = $header->getDimensions();
  118. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  119. $rows = $report->getData()->getRows();
  120. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  121. $row = $rows[ $rowIndex ];
  122. $dimensions = $row->getDimensions();
  123. $metrics = $row->getMetrics();
  124. for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  125. print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
  126. }
  127. for ($j = 0; $j < count($metrics); $j++) {
  128. $values = $metrics[$j]->getValues();
  129. for ($k = 0; $k < count($values); $k++) {
  130. $entry = $metricHeaders[$k];
  131. print($entry->getName() . ": " . $values[$k] . "\n");
  132. }
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Login action.
  139. *
  140. * @return Response|string
  141. */
  142. public function actionLogin()
  143. {
  144. if (!Yii::$app->user->isGuest) {
  145. return $this->goHome();
  146. }
  147. $model = new LoginForm();
  148. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  149. return $this->goBack();
  150. }
  151. $model->password = '';
  152. return $this->render('login', [
  153. 'model' => $model,
  154. ]);
  155. }
  156. /**
  157. * Logout action.
  158. *
  159. * @return Response
  160. */
  161. public function actionLogout()
  162. {
  163. Yii::$app->user->logout();
  164. return $this->goHome();
  165. }
  166. /**
  167. * Displays contact page.
  168. *
  169. * @return Response|string
  170. */
  171. public function actionContact()
  172. {
  173. $model = new ContactForm();
  174. if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  175. Yii::$app->session->setFlash('contactFormSubmitted');
  176. return $this->refresh();
  177. }
  178. return $this->render('contact', [
  179. 'model' => $model,
  180. ]);
  181. }
  182. /**
  183. * Displays about page.
  184. *
  185. * @return string
  186. */
  187. public function actionAbout()
  188. {
  189. return $this->render('about');
  190. }
  191. }