/default_www/backend/modules/analytics/actions/all_pages.php

https://github.com/foonnnnn/forkcms · PHP · 213 lines · 103 code · 37 blank · 73 comment · 20 complexity · edfb9caaf245ff38c355945949b75c11 MD5 · raw file

  1. <?php
  2. /**
  3. * This is the all-pages-action, it will display the overview of analytics posts
  4. *
  5. * @package backend
  6. * @subpackage analytics
  7. *
  8. * @author Dieter Vanden Eynde <dieter@netlash.com>
  9. * @author Annelies Van Extergem <annelies@netlash.com>
  10. * @since 2.0
  11. */
  12. class BackendAnalyticsAllPages extends BackendAnalyticsBase
  13. {
  14. /**
  15. * Execute the action
  16. *
  17. * @return void
  18. */
  19. public function execute()
  20. {
  21. // call parent, this will probably add some general CSS/JS or other required files
  22. parent::execute();
  23. // parse
  24. $this->parse();
  25. // display the page
  26. $this->display();
  27. }
  28. /**
  29. * Parse this page
  30. *
  31. * @return void
  32. */
  33. protected function parse()
  34. {
  35. // call parent parse
  36. parent::parse();
  37. // overview data
  38. $this->parseOverviewData();
  39. // get and parse data for chart
  40. $this->parseChartData();
  41. // parse pages
  42. $this->parsePages();
  43. // init google url
  44. $googleURL = BackendAnalyticsModel::GOOGLE_ANALYTICS_URL . '/%1$s?id=%2$s&amp;pdr=%3$s';
  45. $googleTableId = str_replace('ga:', '', BackendAnalyticsModel::getTableId());
  46. $googleDate = date('Ymd', $this->startTimestamp) . '-' . date('Ymd', $this->endTimestamp);
  47. // parse links to google
  48. $this->tpl->assign('googleTopContentURL', sprintf($googleURL, 'top_content', $googleTableId, $googleDate));
  49. }
  50. /**
  51. * Parses the data to make the chart with
  52. *
  53. * @return void
  54. */
  55. private function parseChartData()
  56. {
  57. // init vars
  58. $maxYAxis = 2;
  59. $metrics = array('visitors', 'pageviews');
  60. $graphData = array();
  61. // get metrics per day
  62. $metricsPerDay = BackendAnalyticsModel::getMetricsPerDay($metrics, $this->startTimestamp, $this->endTimestamp);
  63. // loop metrics
  64. foreach($metrics as $i => $metric)
  65. {
  66. // build graph data array
  67. $graphData[$i] = array();
  68. $graphData[$i]['title'] = $metric;
  69. $graphData[$i]['label'] = ucfirst(BL::lbl(SpoonFilter::toCamelCase($metric)));
  70. $graphData[$i]['i'] = $i + 1;
  71. $graphData[$i]['data'] = array();
  72. // loop metrics per day
  73. foreach($metricsPerDay as $j => $data)
  74. {
  75. // cast SimpleXMLElement to array
  76. $data = (array) $data;
  77. // build array
  78. $graphData[$i]['data'][$j]['date'] = (int) $data['timestamp'];
  79. $graphData[$i]['data'][$j]['value'] = (string) $data[$metric];
  80. }
  81. }
  82. // loop the metrics
  83. foreach($graphData as $metric)
  84. {
  85. // loop the data
  86. foreach($metric['data'] as $data)
  87. {
  88. // get the maximum value
  89. if((int) $data['value'] > $maxYAxis) $maxYAxis = (int) $data['value'];
  90. }
  91. }
  92. // parse
  93. $this->tpl->assign('maxYAxis', $maxYAxis);
  94. $this->tpl->assign('tickInterval', ($maxYAxis == 2 ? '1' : ''));
  95. $this->tpl->assign('graphData', $graphData);
  96. }
  97. /**
  98. * Parses the overview data
  99. *
  100. * @return void
  101. */
  102. private function parseOverviewData()
  103. {
  104. // get aggregates
  105. $results = BackendAnalyticsModel::getAggregates($this->startTimestamp, $this->endTimestamp);
  106. // get total aggregates
  107. $resultsTotal = BackendAnalyticsModel::getAggregatesTotal($this->startTimestamp, $this->endTimestamp);
  108. // are there some values?
  109. $dataAvailable = false;
  110. foreach($resultsTotal as $data) if($data != 0) $dataAvailable = true;
  111. // show message if there is no data
  112. $this->tpl->assign('dataAvailable', $dataAvailable);
  113. // there are some results
  114. if(!empty($results))
  115. {
  116. // pageviews percentage of total
  117. $pageviewsPercentageOfTotal = ($results['pageviews'] == 0) ? 0 : number_format(($results['allPagesPageviews'] / $results['pageviews']) * 100, 0);
  118. // unique pageviews percentage of total
  119. $uniquePageviewsPercentageOfTotal = ($results['uniquePageviews'] == 0) ? 0 : number_format(($results['allPagesUniquePageviews'] / $results['uniquePageviews']) * 100, 0);
  120. // time on site values
  121. $timeOnSite = ($results['entrances'] == 0) ? 0 : ($results['timeOnSite'] / $results['entrances']);
  122. $timeOnSiteTotal = ($resultsTotal['entrances'] == 0) ? 0 : ($resultsTotal['timeOnSite'] / $resultsTotal['entrances']);
  123. $timeOnSiteDifference = ($timeOnSiteTotal == 0) ? 0 : number_format((($timeOnSite - $timeOnSiteTotal) / $timeOnSiteTotal) * 100, 0);
  124. if($timeOnSiteDifference > 0) $timeOnSiteDifference = '+' . $timeOnSiteDifference;
  125. // bounces
  126. $bounces = ($results['entrances'] == 0) ? 0 : number_format(($results['bounces'] / $results['entrances']) * 100, 0);
  127. $bouncesTotal = ($resultsTotal['entrances'] == 0) ? 0 : number_format(($resultsTotal['bounces'] / $resultsTotal['entrances']) * 100, 0);
  128. $bouncesDifference = ($bouncesTotal == 0) ? 0 : number_format((($bounces - $bouncesTotal) / $bouncesTotal) * 100, 0);
  129. if($bouncesDifference > 0) $bouncesDifference = '+' . $bouncesDifference;
  130. // exits percentage
  131. $exitsPercentage = ($results['allPagesPageviews'] == 0) ? 0 : number_format(($results['exits'] / $results['allPagesPageviews']) * 100, 0);
  132. $exitsPercentageTotal = ($resultsTotal['pageviews'] == 0) ? 0 : number_format(($resultsTotal['exits'] / $resultsTotal['pageviews']) * 100, 0);
  133. $exitsPercentageDifference = ($exitsPercentageTotal == 0) ? 0 : number_format((($exitsPercentage - $exitsPercentageTotal) / $exitsPercentageTotal) * 100, 0);
  134. if($exitsPercentageDifference > 0) $exitsPercentageDifference = '+' . $exitsPercentageDifference;
  135. // parse data
  136. $this->tpl->assign('timeOnSite', BackendAnalyticsModel::getTimeFromSeconds($timeOnSite));
  137. $this->tpl->assign('timeOnSiteTotal', BackendAnalyticsModel::getTimeFromSeconds($timeOnSiteTotal));
  138. $this->tpl->assign('timeOnSiteDifference', $timeOnSiteDifference);
  139. $this->tpl->assign('pageviews', $results['pageviews']);
  140. $this->tpl->assign('pageviewsPercentageOfTotal', $pageviewsPercentageOfTotal);
  141. $this->tpl->assign('uniquePageviews', $results['uniquePageviews']);
  142. $this->tpl->assign('uniquePageviewsPercentageOfTotal', $uniquePageviewsPercentageOfTotal);
  143. $this->tpl->assign('bounces', $bounces);
  144. $this->tpl->assign('bouncesTotal', $bouncesTotal);
  145. $this->tpl->assign('bouncesDifference', $bouncesDifference);
  146. $this->tpl->assign('exitsPercentage', $exitsPercentage);
  147. $this->tpl->assign('exitsPercentageTotal', $exitsPercentageTotal);
  148. $this->tpl->assign('exitsPercentageDifference', $exitsPercentageDifference);
  149. }
  150. }
  151. /**
  152. * Parse pages datagrid
  153. *
  154. * @return void
  155. */
  156. private function parsePages()
  157. {
  158. // get results
  159. $results = BackendAnalyticsModel::getPages($this->startTimestamp, $this->endTimestamp);
  160. // there are some results
  161. if(!empty($results))
  162. {
  163. // get the datagrid
  164. $dataGrid = new BackendDataGridArray($results);
  165. // no pagination
  166. $dataGrid->setPaging();
  167. // hide columns
  168. $dataGrid->setColumnHidden('page_encoded');
  169. // set url
  170. $dataGrid->setColumnURL('page', BackendModel::createURLForAction('detail_page') . '&amp;page_path=[page_encoded]');
  171. // parse the datagrid
  172. $this->tpl->assign('dgPages', $dataGrid->getContent());
  173. }
  174. }
  175. }
  176. ?>