IndexController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. $this->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. $users = new \Google_Service_AnalyticsReporting_Metric();
  97. $users->setExpression("ga:users");
  98. $users->setAlias("users");
  99. $source = new \Google_Service_AnalyticsReporting_Dimension();
  100. $source->setName("ga:source");
  101. $segmentDimensions = new \Google_Service_AnalyticsReporting_Dimension();
  102. $segmentDimensions->setName("ga:segment");
  103. // Create the ReportRequest object.
  104. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  105. $request->setViewId($VIEW_ID);
  106. $request->setDateRanges($dateRange);
  107. $request->setMetrics(array($sessions,$users));
  108. $request->setDimensions(array($source, $segmentDimensions));
  109. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  110. $body->setReportRequests( array( $request) );
  111. return $analytics->reports->batchGet( $body );
  112. }
  113. /**
  114. * Parses and prints the Analytics Reporting API V4 response.
  115. *
  116. * @param An Analytics Reporting API V4 response.
  117. */
  118. function printResults($reports) {
  119. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  120. $report = $reports[ $reportIndex ];
  121. $header = $report->getColumnHeader();
  122. $dimensionHeaders = $header->getDimensions();
  123. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  124. $rows = $report->getData()->getRows();
  125. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  126. $row = $rows[ $rowIndex ];
  127. $dimensions = $row->getDimensions();
  128. $metrics = $row->getMetrics();
  129. if($dimensionHeaders&&$dimensions){
  130. for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  131. print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
  132. }
  133. }
  134. if($metricHeaders&&$metrics){
  135. for ($j = 0; $j < count($metrics); $j++) {
  136. $values = $metrics[$j]->getValues();
  137. for ($k = 0; $k < count($values); $k++) {
  138. $entry = $metricHeaders[$k];
  139. print($entry->getName() . ": " . $values[$k] . "\n");
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Login action.
  148. *
  149. * @return Response|string
  150. */
  151. public function actionLogin()
  152. {
  153. if (!Yii::$app->user->isGuest) {
  154. return $this->goHome();
  155. }
  156. $model = new LoginForm();
  157. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  158. return $this->goBack();
  159. }
  160. $model->password = '';
  161. return $this->render('login', [
  162. 'model' => $model,
  163. ]);
  164. }
  165. /**
  166. * Logout action.
  167. *
  168. * @return Response
  169. */
  170. public function actionLogout()
  171. {
  172. Yii::$app->user->logout();
  173. return $this->goHome();
  174. }
  175. /**
  176. * Displays contact page.
  177. *
  178. * @return Response|string
  179. */
  180. public function actionContact()
  181. {
  182. $model = new ContactForm();
  183. if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  184. Yii::$app->session->setFlash('contactFormSubmitted');
  185. return $this->refresh();
  186. }
  187. return $this->render('contact', [
  188. 'model' => $model,
  189. ]);
  190. }
  191. /**
  192. * Displays about page.
  193. *
  194. * @return string
  195. */
  196. public function actionAbout()
  197. {
  198. return $this->render('about');
  199. }
  200. }