PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Controller/GastatsReportsController.php

https://github.com/markvince/CakePHP-GAStats-Plugin
PHP | 256 lines | 181 code | 27 blank | 48 comment | 43 complexity | 4e0f67c23d3720ac14c98dff04125d03 MD5 | raw file
  1. <?php
  2. class GastatsReportsController extends GastatsAppController {
  3. var $name = "GastatsReports";
  4. var $uses = array('Gastats.GastatsAd','Gastats.GastatsCountry',
  5. 'Gastats.GastatsWebchannel','Gastats.GastatsWebstat','Gastats.GastatsRaw',
  6. );
  7. //var $layout = 'blank';
  8. public $metrics = array(
  9. 'avgTimeOnPage' => array('display'=>true,'header' => 'Avg Time On Page (h:m:s)','uom'=>'time'),
  10. 'exists' => array('display'=>false,'header' => 'Exits',),
  11. 'pageviews' => array('display'=>true,'header' => 'Page Views',),
  12. 'timeOnpage' => array('display'=>false,'header' => 'Time On Page',),
  13. 'uniquePageviews' => array('display'=>true,'header' => 'Unique Page Views',),
  14. 'avgTimeOnSite' => array('display'=>true,'header' => 'Avg Time On Site (h:m:s)','uom'=>'time'),
  15. 'timeOnSite' => array('display'=>false,'header' => 'Time On Site (h:m:s)','uom'=>'time'),
  16. 'visitors' => array('display'=>true,'header' => 'Visitors',),
  17. 'visits' => array('display'=>true,'header' => 'Visits',),
  18. );
  19. public function index() {
  20. die();
  21. }
  22. /**
  23. * Query and display ad records by corp_id and date range.
  24. * @param corp_id
  25. * @param start_date
  26. * @param end_date
  27. */
  28. public function ads($corp_id=0,$start_date=null,$end_date=null) {
  29. if ($corp_id == 0) {
  30. $conditions = compact('start_date','end_date');
  31. } else {
  32. $conditions = compact('corp_id','start_date','end_date');
  33. }
  34. $ads_array = $this->GastatsAd->find('all',compact('conditions'));
  35. $ads = array();
  36. $corps=array();
  37. //prep data for display
  38. foreach ($ads_array as $ad) {
  39. $ad = $ad['GastatsAd'];
  40. $ads['unique'][$ad['ad_id']][$ad['location']][$ad['ad_slot']][$ad['ad_stat_type']] = $ad['value'];
  41. $corps[$ad['ad_id']] = $ad['corp_id'];
  42. //breakdown
  43. //Total by stat type only (click/view)
  44. if (isset($ads['group'][$ad['ad_id']][$ad['ad_stat_type']])) {
  45. $ads['group'][$ad['ad_id']][$ad['ad_stat_type']]['total'] += $ad['value'];
  46. } else {
  47. $ads['group'][$ad['ad_id']][$ad['ad_stat_type']]['total'] = $ad['value'];
  48. $ads['group'][$ad['ad_id']]['ad_stat_types'][$ad['ad_stat_type']] = 1;
  49. }
  50. //Total by location and ad stat type
  51. if (isset($ads['group'][$ad['ad_id']][$ad['ad_stat_type']][$ad['location']]['total'])) {
  52. $ads['group'][$ad['ad_id']][$ad['ad_stat_type']][$ad['location']]['total'] += $ad['value'];
  53. } else {
  54. $ads['group'][$ad['ad_id']][$ad['ad_stat_type']][$ad['location']]['total'] = $ad['value'];
  55. $ads['group'][$ad['ad_id']]['ad_locations'][$ad['location']] = 1;
  56. }
  57. }
  58. $this->set(compact('corp_id','start_date','end_date','ads','corps'));
  59. }
  60. /**
  61. * Query and display webchannel records by corp_id and date range.
  62. * @param corp_id
  63. * @param start_date
  64. * @param end_date
  65. */
  66. public function webchannels ($corp_id=0,$start_date=null,$end_date=null) {
  67. if ($corp_id == 0) {
  68. $conditions = compact('start_date','end_date');
  69. } else {
  70. $conditions = compact('corp_id','start_date','end_date');
  71. }
  72. $order = 'channel ASC, metric ASC';
  73. $channels_array = $this->GastatsWebchannel->find('all',compact('conditions','order'));
  74. foreach ($channels_array as $webchannel) {
  75. $webchannel = $webchannel['GastatsWebchannel'];
  76. if (isset($this->metrics[$webchannel['metric']]) && $this->metrics[$webchannel['metric']]['display'] == true) {
  77. $webchannels[$webchannel['corp_id']]['channel'] = $webchannel['channel'];
  78. if (isset($this->metrics[$webchannel['metric']]['uom'])) {
  79. if (in_array($this->metrics[$webchannel['metric']]['uom'],array('time'))) {
  80. //GA defaults to seconds, convert to hms
  81. $webchannels[$webchannel['corp_id']]['metrics'][$this->metrics[$webchannel['metric']]['header']] = $this->_secondsDisplay($webchannel['value'],$this->metrics[$webchannel['metric']]['uom']);
  82. }
  83. } else {
  84. $webchannels[$webchannel['corp_id']]['metrics'][$this->metrics[$webchannel['metric']]['header']] = $webchannel['value'];
  85. }
  86. }
  87. }
  88. $this->set(compact('corp_id','start_date','end_date','webchannels'));
  89. }
  90. /**
  91. * Query and display site-wide stats by date range.
  92. * @param start_date
  93. * @param end_date
  94. */
  95. public function webstats($start_date=null,$end_date=null) {
  96. $conditions = compact('start_date','end_date');
  97. $order = 'metric ASC';
  98. $stats_array = $this->GastatsWebstat->find('all',compact('conditions','order'));
  99. foreach ($stats_array as $stat) {
  100. $stat = $stat['GastatsWebstat'];
  101. if (isset($this->metrics[$stat['metric']]) && $this->metrics[$stat['metric']]['display'] == true) {
  102. if (isset($this->metrics[$stat['metric']]['uom'])) {
  103. if (in_array($this->metrics[$stat['metric']]['uom'],array('time'))) {
  104. //GA defaults to seconds, convert to hms
  105. $stats[$this->metrics[$stat['metric']]['header']] = $this->_secondsDisplay($stat['value'],$this->metrics[$stat['metric']]['uom']);
  106. }
  107. } else {
  108. $stats[$this->metrics[$stat['metric']]['header']] = $stat['value'];
  109. }
  110. } elseif (isset($this->metrics[$stat['metric']]) && $this->metrics[$stat['metric']]['display'] == false) {
  111. //flagged to not display
  112. } else {
  113. //unknown, display by default
  114. $stats[$stat['metric']] = $stat['value'];
  115. }
  116. }
  117. $this->set(compact('start_date','end_date','stats'));
  118. }
  119. /**
  120. * Query and display country visit stats date range.
  121. * @param start_date
  122. * @param end_date
  123. */
  124. public function countries($start_date=null,$end_date=null, $limit=null) {
  125. $conditions = compact('start_date','end_date');
  126. $conditions[] = 'country <> "(not set)"'; //GA result with no set country name
  127. $order = "visits DESC";
  128. $country_array = $this->GastatsCountry->find('all',compact('conditions','order','limit'));
  129. foreach ($country_array as $country) {
  130. $country = $country['GastatsCountry'];
  131. $countries[$country['country']] = $country['visits'];
  132. }
  133. $this->set(compact('start_date','end_date','countries'));
  134. }
  135. /**
  136. * Query Raw data by date and/or path, will display path and page views
  137. * @param start_date
  138. * @param end_date
  139. * path (optional) - will query with 'like' match
  140. * wildcard (optional) - if true then will add wildcard % to end of path
  141. */
  142. public function content($start_date=null, $end_date=null, $limit=null) {
  143. if (isset($this->params['url']['path'])) {
  144. $path = urldecode($this->params['url']['path']);
  145. } else {
  146. $path = '';
  147. }
  148. //Check for wildcard
  149. if (isset($this->params['url']['wildcard']) && $this->params['url']['wildcard'] == true) {
  150. $wildcard = true;
  151. }
  152. if (!empty($path)) {
  153. $conditions['GastatsRaw.key LIKE'] = "$path$wildcard";
  154. }
  155. $content_array = $this->GastatsRaw->getContent($start_date, $end_date, $limit, $path, $wildcard);
  156. $contents = array();
  157. foreach ($content_array as $item) {
  158. $contents[$item['key']] = $item['value'];
  159. }
  160. $this->set(compact('start_date','end_date','contents'));
  161. }
  162. /**
  163. * @param content_type_slug - content_type-slug //articles-my-article-xxyy
  164. *
  165. */
  166. public function contentBySlug($content_type_slug, $start_date=null, $end_date=null) {
  167. $content_type_slug = explode("-",$content_type_slug);
  168. $content_type = array_shift($content_type_slug);
  169. $slug = (isset($content_type_slug[0]) ? implode("-",$content_type_slug) : '');
  170. $wildcard=false;
  171. if (isset($this->params['url']['wildcard']) && $this->params['url']['wildcard'] == true) {
  172. $wildcard = true;
  173. }
  174. if ($wildcard == false && empty($slug)) {
  175. $wildcard = true;
  176. }
  177. $contents_array = $this->GastatsRaw->getContent($start_date, $end_date, null, "/$content_type/$slug", $wildcard);
  178. foreach ($contents_array as $item) {
  179. $item = $item['GastatsRaw'];
  180. $path = explode("?",$item['key']);
  181. $path = $path[0];
  182. if (isset($contents[$path])) {
  183. $contents[$path] += $item['value'];
  184. } else {
  185. $contents[$path] = $item['value'];
  186. }
  187. }
  188. $this->set(compact('start_date','end_date','contents'));
  189. $this->render('content');
  190. }
  191. /**
  192. * @param content_id - id at end of slug
  193. *
  194. */
  195. public function contentById($content_id, $start_date=null, $end_date=null) {
  196. $content_id = explode("-",$content_id);
  197. $content_type = '';
  198. if (count($content_id) == 2) {
  199. $content_type = array_shift($content_id);
  200. $content_type = "/$content_type/";
  201. }
  202. $content_id = array_shift($content_id);
  203. $slug = "$content_type%-$content_id";
  204. $contents_array = $this->GastatsRaw->getContent($start_date, $end_date, null, "$slug", false);
  205. foreach ($contents_array as $item) {
  206. $item = $item['GastatsRaw'];
  207. $path = explode("?",$item['key']);
  208. $path = $path[0];
  209. if (isset($contents[$path])) {
  210. $contents[$path] += $item['value'];
  211. } else {
  212. $contents[$path] = $item['value'];
  213. }
  214. }
  215. $this->set(compact('start_date','end_date','contents'));
  216. $this->render('content');
  217. }
  218. // -------------------
  219. function _secondsDisplay($sec) {
  220. $hour = intval($sec/3600); //hours = 3600 per hour
  221. $min = intval(($sec/60)%60); //minutes = 60 sec per minute, then take remainder not used up by the hours
  222. $sec = intval($sec%60);
  223. $hour = str_pad($hour,2,"0",STR_PAD_LEFT);
  224. $min = str_pad($min,2,"0",STR_PAD_LEFT);
  225. $sec = str_pad($sec,2,"0",STR_PAD_LEFT);
  226. return "$hour:$min:$sec";
  227. }
  228. }
  229. ?>