/backend/modules/analytics/actions/content.php

https://github.com/jasonweng/forkcms · PHP · 202 lines · 125 code · 26 blank · 51 comment · 16 complexity · 8ebcff19e233030271b9e82a1a8cc32b MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Fork CMS.
  4. *
  5. * For the full copyright and license information, please view the license
  6. * file that was distributed with this source code.
  7. */
  8. /**
  9. * This is the content-action, it will display the overview of analytics posts
  10. *
  11. * @author Dieter Vanden Eynde <dieter.vandeneynde@netlash.com>
  12. * @author Annelies Van Extergem <annelies.vanextergem@netlash.com>
  13. */
  14. class BackendAnalyticsContent extends BackendAnalyticsBase
  15. {
  16. /**
  17. * Execute the action
  18. */
  19. public function execute()
  20. {
  21. parent::execute();
  22. $this->parse();
  23. $this->display();
  24. }
  25. /**
  26. * Parse this page
  27. */
  28. protected function parse()
  29. {
  30. parent::parse();
  31. $this->parseOverviewData();
  32. $this->parseChartData();
  33. $this->parseImportantPages();
  34. $this->parseImportantExitPages();
  35. $this->parseImportantLandingPages();
  36. $googleURL = BackendAnalyticsModel::GOOGLE_ANALYTICS_URL . '/%1$s?id=%2$s&amp;pdr=%3$s';
  37. $googleTableId = str_replace('ga:', '', BackendAnalyticsModel::getTableId());
  38. $googleDate = date('Ymd', $this->startTimestamp) . '-' . date('Ymd', $this->endTimestamp);
  39. // parse links to google
  40. $this->tpl->assign('googleTopContentURL', sprintf($googleURL, 'top_content', $googleTableId, $googleDate));
  41. $this->tpl->assign('googleTopExitPagesURL', sprintf($googleURL, 'exits', $googleTableId, $googleDate));
  42. $this->tpl->assign('googleTopLandingPagesURL', sprintf($googleURL, 'entrances', $googleTableId, $googleDate));
  43. $this->tpl->assign('googleContentURL', sprintf($googleURL, 'content', $googleTableId, $googleDate));
  44. }
  45. /**
  46. * Parses the data to make the chart with
  47. */
  48. private function parseChartData()
  49. {
  50. $maxYAxis = 2;
  51. $metrics = array('visitors', 'pageviews');
  52. $graphData = array();
  53. // get metrics per day
  54. $metricsPerDay = BackendAnalyticsModel::getMetricsPerDay($metrics, $this->startTimestamp, $this->endTimestamp);
  55. foreach($metrics as $i => $metric)
  56. {
  57. // build graph data array
  58. $graphData[$i] = array();
  59. $graphData[$i]['title'] = $metric;
  60. $graphData[$i]['label'] = ucfirst(BL::lbl(SpoonFilter::toCamelCase($metric)));
  61. $graphData[$i]['i'] = $i + 1;
  62. $graphData[$i]['data'] = array();
  63. // loop metrics per day
  64. foreach($metricsPerDay as $j => $data)
  65. {
  66. // cast SimpleXMLElement to array
  67. $data = (array) $data;
  68. // build array
  69. $graphData[$i]['data'][$j]['date'] = (int) $data['timestamp'];
  70. $graphData[$i]['data'][$j]['value'] = (string) $data[$metric];
  71. }
  72. }
  73. // loop the metrics
  74. foreach($graphData as $metric)
  75. {
  76. foreach($metric['data'] as $data)
  77. {
  78. // get the maximum value
  79. if((int) $data['value'] > $maxYAxis) $maxYAxis = (int) $data['value'];
  80. }
  81. }
  82. $this->tpl->assign('maxYAxis', $maxYAxis);
  83. $this->tpl->assign('tickInterval', ($maxYAxis == 2 ? '1' : ''));
  84. $this->tpl->assign('graphData', $graphData);
  85. }
  86. /**
  87. * Parses the most important exit pages
  88. */
  89. private function parseImportantExitPages()
  90. {
  91. $results = BackendAnalyticsModel::getTopExitPages($this->startTimestamp, $this->endTimestamp);
  92. if(!empty($results))
  93. {
  94. $dataGrid = new BackendDataGridArray($results);
  95. $dataGrid->setColumnHidden('page_encoded');
  96. $dataGrid->setColumnURL('page', BackendModel::createURLForAction('detail_page') . '&amp;page=[page_encoded]');
  97. // parse the datagrid
  98. $this->tpl->assign('dgExitPages', $dataGrid->getContent());
  99. }
  100. }
  101. /**
  102. * Parse the most important landing pages
  103. */
  104. private function parseImportantLandingPages()
  105. {
  106. $results = BackendAnalyticsModel::getLandingPages($this->startTimestamp, $this->endTimestamp, 5);
  107. if(!empty($results))
  108. {
  109. $dataGrid = new BackendDataGridArray($results);
  110. $dataGrid->setColumnsHidden('start_date', 'end_date', 'updated_on', 'page_encoded');
  111. $dataGrid->setColumnURL('page_path', BackendModel::createURLForAction('detail_page') . '&amp;page=[page_encoded]');
  112. // set headers
  113. $dataGrid->setHeaderLabels(
  114. array('page_path' => ucfirst(BL::lbl('Page')))
  115. );
  116. // parse the datagrid
  117. $this->tpl->assign('dgLandingPages', $dataGrid->getContent());
  118. }
  119. }
  120. /**
  121. * Parses the most important pages
  122. */
  123. private function parseImportantPages()
  124. {
  125. $results = BackendAnalyticsModel::getTopPages($this->startTimestamp, $this->endTimestamp);
  126. if(!empty($results))
  127. {
  128. $dataGrid = new BackendDataGridArray($results);
  129. $dataGrid->setColumnHidden('page_encoded');
  130. $dataGrid->setColumnURL('page', BackendModel::createURLForAction('detail_page') . '&amp;page=[page_encoded]');
  131. // set headers
  132. $dataGrid->setHeaderLabels(
  133. array('pageviews_percentage' => '% ' . ucfirst(BL::lbl('Pageviews')))
  134. );
  135. // parse the datagrid
  136. $this->tpl->assign('dgContent', $dataGrid->getContent());
  137. }
  138. }
  139. /**
  140. * Parses the overview data
  141. */
  142. private function parseOverviewData()
  143. {
  144. // get aggregates
  145. $results = BackendAnalyticsModel::getAggregates($this->startTimestamp, $this->endTimestamp);
  146. $resultsTotal = BackendAnalyticsModel::getAggregatesTotal($this->startTimestamp, $this->endTimestamp);
  147. // are there some values?
  148. $dataAvailable = false;
  149. foreach($resultsTotal as $data) if($data != 0) $dataAvailable = true;
  150. // show message if there is no data
  151. $this->tpl->assign('dataAvailable', $dataAvailable);
  152. if(!empty($results))
  153. {
  154. // new visitors
  155. $newVisits = ($results['entrances'] == 0) ? 0 : number_format(($results['newVisits'] / $results['entrances']) * 100, 0);
  156. $newVisitsTotal = ($resultsTotal['entrances'] == 0) ? 0 : number_format(($resultsTotal['newVisits'] / $resultsTotal['entrances']) * 100, 0);
  157. $newVisitsDifference = ($newVisitsTotal == 0) ? 0 : number_format((($newVisits - $newVisitsTotal) / $newVisitsTotal) * 100, 0);
  158. if($newVisitsDifference > 0) $newVisitsDifference = '+' . $newVisitsDifference;
  159. // bounces
  160. $bounces = ($results['entrances'] == 0) ? 0 : number_format(($results['bounces'] / $results['entrances']) * 100, 0);
  161. $bouncesTotal = ($resultsTotal['entrances'] == 0) ? 0 : number_format(($resultsTotal['bounces'] / $resultsTotal['entrances']) * 100, 0);
  162. $bouncesDifference = ($bouncesTotal == 0) ? 0 : number_format((($bounces - $bouncesTotal) / $bouncesTotal) * 100, 0);
  163. if($bouncesDifference > 0) $bouncesDifference = '+' . $bouncesDifference;
  164. $this->tpl->assign('pageviews', $results['pageviews']);
  165. $this->tpl->assign('pageviewsTotal', $resultsTotal['pageviews']);
  166. $this->tpl->assign('uniquePageviews', $results['uniquePageviews']);
  167. $this->tpl->assign('uniquePageviewsTotal', $resultsTotal['uniquePageviews']);
  168. $this->tpl->assign('newVisits', $newVisits);
  169. $this->tpl->assign('newVisitsTotal', $newVisitsTotal);
  170. $this->tpl->assign('newVisitsDifference', $newVisitsDifference);
  171. $this->tpl->assign('bounces', $bounces);
  172. $this->tpl->assign('bouncesTotal', $bouncesTotal);
  173. $this->tpl->assign('bouncesDifference', $bouncesDifference);
  174. }
  175. }
  176. }