GoogleAnalyticsSummary

Creates a summary widget for Google Analytics stats.

Defined (2)

The class is defined in the following location(s).

/google-analytics-summary-widget.php  
  1. class GoogleAnalyticsSummary 
  2. var $api = false; 
  3. var $id = false; 
  4. var $qa_selecteddate, $date_before, $date_yesterday; 
  5. /** 
  6. * Start the process of including the widget 
  7. **/ 
  8. function __construct($shortcode = FALSE) 
  9. add_action('wp_dashboard_setup', array( 
  10. $this,  
  11. 'addDashboardWidget' 
  12. )); 
  13. add_action('admin_footer', array( 
  14. $this,  
  15. 'addJavascript' 
  16. )); 
  17. add_action('admin_footer-index.php', array( 
  18. $this,  
  19. 'addTopJs' 
  20. )); 
  21.  
  22. if ($shortcode) { 
  23. // For shortcode 
  24. add_action('wp_footer', array( 
  25. $this,  
  26. 'addJavascript' 
  27. )); 
  28.  
  29. add_action('wp_footer', array( 
  30. $this,  
  31. 'addTopJs' 
  32. )); 
  33.  
  34. add_action( 'wp_ajax_nopriv_ga_stats_widget', array( $this, 'ajaxWidget' ) ); 
  35.  
  36. $this->qa_selecteddate = isset( $_REQUEST['qa_selecteddate'] ) ? wp_filter_kses( $_REQUEST['qa_selecteddate'] ) : '31'; 
  37. $this->date_before = date('Y-m-d', strtotime( '-'.$this->qa_selecteddate.' days', strtotime( current_time( 'mysql' ) ) ) ); 
  38. $this->date_yesterday = date('Y-m-d', strtotime( '-1 days', strtotime( current_time( 'mysql' ) ) ) ); 
  39.  
  40. add_action( 'wp_ajax_ga_stats_widget', array( $this, 'ajaxWidget' ) ); 
  41.  
  42. /** 
  43. * Add the widget to the dashboard 
  44. **/ 
  45. function addDashboardWidget() 
  46. # Check if the user is an admin 
  47. if (ga_current_user_is(get_option(key_ga_dashboard_role))) { 
  48. wp_add_dashboard_widget('google-analytics-summary', __('Google Analytics Summary', 'google-analyticator'), array( 
  49. $this,  
  50. 'widget' 
  51. )); 
  52.  
  53. /** 
  54. * Add the scripts to the admin 
  55. **/ 
  56. function addJavascript() 
  57. # Include the Sparklines graphing library 
  58. wp_enqueue_script('flot', plugins_url('/google-analyticator/jquery.flot.min.js'), array( 
  59. 'jquery' 
  60. ), '1.5.1'); 
  61.  
  62. /** 
  63. * Add the Javascript to the top 
  64. **/ 
  65. function addTopJs() 
  66. wp_enqueue_script( 'jquery' ); 
  67. ?> 
  68. <style type="text/css"> 
  69. #google-analytics-summary .legend table {width:auto;border:0;margin:0;} 
  70. </style> 
  71. <script type="text/javascript"> 
  72. //Tooltip  
  73. jQuery.fn.UseTooltip = function () { 
  74. var previousPoint = null; 
  75.  
  76. jQuery(this).bind("plothover", function (event, pos, item) {  
  77. if (item) { 
  78. if (previousPoint != item.dataIndex) { 
  79. previousPoint = item.dataIndex; 
  80.  
  81. jQuery("#vumtooltip").remove(); 
  82.  
  83. var x = item.datapoint[0]; 
  84. var y = item.datapoint[1];  
  85. showTooltip(item.pageX, item.pageY, "<b>"+d2[ x- 1] + "</b><br/>" + item.series.label + ": <strong>" + y + "</strong>"); 
  86. else { 
  87. jQuery("#vumtooltip").remove(); 
  88. previousPoint = null; 
  89. }); 
  90. }; 
  91.  
  92. function showTooltip(x, y, contents) { 
  93. jQuery('<div id="vumtooltip">' + contents + '</div>').css({ 
  94. position: 'absolute',  
  95. display: 'none',  
  96. top: y + 5,  
  97. left: x + 20,  
  98. border: '1px solid #D0D0D0',  
  99. padding: '6px',  
  100. size: '9',  
  101. 'background-color': '#fff',  
  102. opacity: 0.80 
  103. }).appendTo("body").fadeIn(200); 
  104.  
  105. jQuery(document).ready(function($) { 
  106. // Add a link to see full stats on the Analytics website 
  107. jQuery('#google-analytics-summary h3.hndle span').append('<span class="postbox-title-action"><a href="http://google.com/analytics/" class="edit-box open-box"><?php _e('View Full Stat Report', 'google-analyticator');?></a></span>'); 
  108.  
  109. // Onload call analytics 
  110. getAnalytics(); 
  111.  
  112. //On date selection change 
  113. jQuery("#qa_selecteddate").change(function() { 
  114. getAnalytics(); 
  115. }); 
  116.  
  117. }); 
  118.  
  119. function getAnalytics() { 
  120.  
  121. // console.log( 'Start getAnalytics();' ); 
  122. // Grab the widget data 
  123. jQuery.ajax({ 
  124. type: 'post',  
  125. url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',  
  126. data: { 
  127. action: 'ga_stats_widget',  
  128. qa_selecteddate: jQuery("#qa_selecteddate :selected").val(),  
  129. _ajax_nonce: '<?php echo wp_create_nonce("google-analyticator-statsWidget_get");?>' 
  130. },  
  131. beforeSend: function() { 
  132. jQuery("#analyticsloading").html('<img src="<?php echo admin_url("images/wpspin_light.gif")?>" border="0" /> ').show(); 
  133. },  
  134. success: function(html) { 
  135. jQuery("#analyticsloading").hide(); 
  136. // Hide the loading message 
  137. jQuery('#google-analytics-summary .inside small').remove(); 
  138.  
  139. // console.log(html); 
  140.  
  141. // Place the widget data in the area 
  142. jQuery('#google-analytics-summary .inside .target').html(html); 
  143.  
  144. // Display the widget data 
  145. jQuery('#google-analytics-summary .inside .target').slideDown(); 
  146.  
  147. // Handle displaying the graph 
  148. var divElement = jQuery('div'); //log all div elements 
  149. var placeholder = jQuery(".flotcontainer"); 
  150.  
  151. //disable graph if the selected is yesterday or today 
  152. placeholder.show(); 
  153. if(jQuery("#qa_selecteddate :selected").val() == '0' || jQuery("#qa_selecteddate :selected").val() == '1') { 
  154. placeholder.hide(); 
  155. return false; 
  156.  
  157. //graph options 
  158. var options = { 
  159. grid: { 
  160. show: true,  
  161. aboveData: true,  
  162. color: "#3f3f3f" ,  
  163. labelMargin: 5,  
  164. axisMargin: 0,  
  165. borderWidth: 0,  
  166. borderColor:null,  
  167. minBorderMargin: 5 ,  
  168. clickable: true,  
  169. hoverable: true,  
  170. autoHighlight: true,  
  171. mouseActiveRadius: 10 
  172. },  
  173. series: { 
  174. grow: {active:false},  
  175. lines: { 
  176. show: true,  
  177. fill: true,  
  178. lineWidth: 2,  
  179. steps: false 
  180. },  
  181. points: {show:true} 
  182. },  
  183. legend: { position: "se" },  
  184. yaxis: { min: 0 },  
  185. xaxis: {ticks : datelabel, tickDecimals : 0},  
  186. colors: ['#88bbc8', '#ed7a53', '#9FC569', '#bbdce3', '#9a3b1b', '#5a8022', '#2c7282'],  
  187. shadowSize:1,  
  188. tooltip: false, //activate tooltip 
  189. };  
  190.  
  191. jQuery.plot(placeholder, [  
  192. label: "<?php _e('Visits', 'google-analyticator')?>",  
  193. data: d1,  
  194. lines: {fillColor: "#f2f7f9"},  
  195. points: {fillColor: "#88bbc8"} 
  196.  
  197. ], options); 
  198. jQuery(".flotcontainer").UseTooltip(); 
  199.  
  200. }); 
  201. }  
  202. jQuery(window).resize(function() { 
  203. getAnalytics(); 
  204. }); 
  205. </script> 
  206. <?php 
  207.  
  208. /** 
  209. * The widget display 
  210. **/ 
  211. function widget() 
  212. # Attempt to login and get the current account 
  213. echo '<p align="right"><span id="analyticsloading"></span> <select id="qa_selecteddate" name="qa_selecteddate"> 
  214. <option selected="selected" value="30">'. __("Past 30 days", 'google-analyticator'). '</option> 
  215. <option value="60">'. __("Past 60 days", 'google-analyticator'). '</option> 
  216. <option value="1">'. __("Yesterday", 'google-analyticator'). '</option> 
  217. </select></p>'; 
  218. echo '<div class="flotcontainer" style="height: 230px;width:100%;"> </div>'; 
  219. echo '<div class="target" style="display: none;"></div>'; 
  220.  
  221. /** 
  222. * AJAX data for the widget 
  223. **/ 
  224. function ajaxWidget() 
  225. # Check the ajax widget 
  226. check_ajax_referer('google-analyticator-statsWidget_get'); 
  227.  
  228. # If WP_DEBUG is true, . 
  229.  
  230. $doing_transient = false;  
  231.  
  232. if ( ( defined('WP_DEBUG') && WP_DEBUG ) || ( false === ( $ga_output = get_transient( 'ga_admin_dashboard_'. GOOGLE_ANALYTICATOR_VERSION . $this->qa_selecteddate) ) ) ) { 
  233. ob_start(); 
  234. # Attempt to login and get the current account 
  235. $account = $this->getAnalyticsAccount(); 
  236.  
  237. $this->id = $account; 
  238.  
  239. $this->api->setAccount($this->id); 
  240.  
  241. # Check that we can display the widget before continuing 
  242. if ($account == false || $this->id == false) { 
  243. # Output error message 
  244. echo '<p style="margin: 0;">' . __('No Analytics account selected. Double check you are authenticated with Google on Google Analyticator\'s settings page and make sure an account is selected.', 'google-analyticator') . '</p>'; 
  245. # Add Javascript variable to prevent breaking the Javascript 
  246. echo '<script type="text/javascript">var ga_visits = [];</script>'; 
  247. die; 
  248.  
  249. # Add the header information for the visits graph 
  250. // echo '<p class="ga_visits_title" style="font-style: italic; font-family: Georgia, \'Times New Roman\', \'Bitstream Charter\', Times, serif; margin-top: 0px; color: #777; font-size: 13px;">' . __('Visits Over the Past 30 Days', 'google-analyticator') . '</p>'; 
  251.  
  252. # Add the sparkline for the past 30 days 
  253. $this->getVisitsGraph(); 
  254.  
  255. # Add the summary header 
  256. echo '<p style="font-style: italic; font-family: Georgia, \'Times New Roman\', \'Bitstream Charter\', Times, serif; color: #777; font-size: 13px;">' . __('Site Usage', 'google-analyticator') . '</p>'; 
  257.  
  258. # Add the visitor summary 
  259. $this->getSiteUsage(); 
  260.  
  261. # Add the top 5 posts header 
  262. echo '<p style="font-style: italic; font-family: Georgia, \'Times New Roman\', \'Bitstream Charter\', Times, serif; color: #777; font-size: 13px;">' . __('Top Pages', 'google-analyticator') . '</p>'; 
  263.  
  264. # Add the top 5 posts 
  265. $this->getTopPages(); 
  266.  
  267. # Add the tab information 
  268. echo '<table width="100%"><tr><td width="50%" valign="top">'; 
  269.  
  270. # Add the top 5 referrers header 
  271. echo '<p style="font-style: italic; font-family: Georgia, \'Times New Roman\', \'Bitstream Charter\', Times, serif; color: #777; font-size: 13px;">' . __('Top Referrers', 'google-analyticator') . '</p>'; 
  272.  
  273. # Add the referrer information 
  274. $this->getTopReferrers(); 
  275.  
  276. # Add the second column 
  277. echo '</td><td valign="top">'; 
  278.  
  279. # Add the top 5 search header 
  280. echo '<p style="font-style: italic; font-family: Georgia, \'Times New Roman\', \'Bitstream Charter\', Times, serif; color: #777; font-size: 13px;">' . __('Top Searches', 'google-analyticator') . '</p>'; 
  281.  
  282. # Add the search information 
  283. $this->getTopSearches(); 
  284.  
  285. # Close the table 
  286. echo '</td></tr></table>'; 
  287.  
  288.  
  289. # Grab the above outputs and cache it! 
  290. $ga_output = ob_get_flush(); 
  291.  
  292. // Cache the admin dashboard for 6 hours at a time.  
  293. set_transient( 'ga_admin_dashboard_'. GOOGLE_ANALYTICATOR_VERSION . $this->qa_selecteddate , $ga_output, 60*60*6 ); 
  294. $doing_transient = true; 
  295.  
  296. } else { 
  297. $ga_output = get_transient( 'ga_admin_dashboard_'. GOOGLE_ANALYTICATOR_VERSION . $this->qa_selecteddate , $ga_output);  
  298.  
  299. if( ! $doing_transient ) 
  300. echo $ga_output; 
  301.  
  302. if( get_option( key_ga_show_ad ) ) { 
  303. echo '<p style="text-align:center"> 
  304. [Use the <a target="_blank" href="https://wordpress.org/plugins/sumome">SumoMe plugin</a> to get more traffic]</p>'; 
  305.  
  306. die(); 
  307.  
  308. /** 
  309. * Get the current analytics account 
  310. * @return the analytics account 
  311. **/ 
  312. function getAnalyticsAccount() 
  313. $accounts = array(); 
  314.  
  315. # Get the class for interacting with the Google Analytics 
  316. require_once('class.analytics.stats.php'); 
  317.  
  318. # Create a new Gdata call 
  319. $this->api = new GoogleAnalyticsStats(); 
  320.  
  321. # Check if Google sucessfully logged in 
  322. if (!$this->api->checkLogin()) 
  323. return false; 
  324.  
  325. # Get a list of accounts 
  326. //$accounts = $this->api->getAnalyticsAccounts(); 
  327. $accounts = $this->api->getSingleProfile(); 
  328.  
  329. # Check if we actually have accounts 
  330. if (!is_array($accounts)) 
  331. return false; 
  332.  
  333. # Check if we have a list of accounts 
  334. if (count($accounts) <= 0) 
  335. return false; 
  336.  
  337. # Loop throught the account and return the current account 
  338. foreach ($accounts AS $account) { 
  339. # Check if the UID matches the selected UID 
  340. if ($account['ga:webPropertyId'] == get_option('ga_uid')) 
  341. return $account['id']; 
  342.  
  343. return false; 
  344.  
  345. /** 
  346. * Get the visitors graph 
  347. **/ 
  348. function getVisitsGraph() 
  349. # Get the metrics needed to build the visits graph; 
  350. try { 
  351. $stats = $this->api->getMetrics('ga:visits', $this->date_before, $this->date_yesterday, 'ga:date', 'ga:date'); 
  352. catch (Exception $e) { 
  353. print 'GA Summary Widget - there was a service error ' . $e->getCode() . ':' . $e->getMessage(); 
  354.  
  355. # Create a list of the data points for graphing 
  356. $data = ''; 
  357. $data2 = ""; 
  358. $data3 = ""; 
  359. $max = 0; 
  360.  
  361. $rows = $stats->getRows(); 
  362.  
  363. # Check the size of the stats array 
  364. if (!isset($rows) || !is_array($rows) || count($rows) <= 0) { 
  365. $data = '0, 0'; 
  366. } else { 
  367. $counter = 0; 
  368. foreach ($rows AS $stat) { 
  369. $counter++; 
  370. # Verify the number is numeric 
  371. if (is_numeric($stat[1])) { 
  372. $datestat = $stat[0]; 
  373. $date_str = substr($datestat, 0, 4) .'-'. substr($datestat, 4, -2).'-'. substr($datestat, 6, 8); 
  374. $data .= '['.$counter.', '.$stat[1] . '], ';  
  375. $data3 .= '"'. date("l, M. d, Y", strtotime($date_str, strtotime( current_time( 'mysql' ) ))) . '", '; // 20130120 
  376. if($counter % 4 == 0) { 
  377. $data2 .= '['.$counter.', "'. date("M. d", strtotime($date_str, strtotime( current_time( 'mysql' ) ))) . '"], '; // 20130120 
  378.  
  379. }else 
  380. $data .= '0, '; 
  381.  
  382. # Update the max value if greater 
  383. if ($max < $stat[1]) 
  384. $max = $stat[1]; 
  385.  
  386.  
  387. $yesterday_count = $rows[count($rows) - 1][1]; 
  388.  
  389. # Shorten the string to remove the last comma 
  390. $data = substr($data, 0, -1); 
  391. $data2 = substr($data2, 0, -1); 
  392. $data3 = substr($data3, 0, -1); 
  393.  
  394. # Add a fake stat if need be 
  395. if (!isset($stat[1])) 
  396. $stat[1] = 0; 
  397.  
  398. # Output the graph 
  399. $output = '<script type="text/javascript">var datelabel = ['.$data2.'];var d1 = ['.$data.'];var d2 = ['.$data3.'];</script>'; 
  400. $output .= '<span class="ga_visits" title="' . sprintf(__('The most visits on a single day was %d. Yesterday there were %d visits.', 'google-analyticator'), $max, $yesterday_count) . '"></span>'; 
  401.  
  402. echo $output; 
  403.  
  404. /** 
  405. * Get the site usage 
  406. **/ 
  407. function getSiteUsage() 
  408. # Get the metrics needed to build the usage table 
  409. $stats = $this->api->getMetrics('ga:visits, ga:bounces, ga:entrances, ga:pageviews, ga:timeOnSite, ga:newVisits', $this->date_before, $this->date_yesterday); 
  410.  
  411. # Create the site usage table 
  412. if (isset($stats->totalsForAllResults)) { ?> 
  413. <table width="100%"> 
  414. <tr> 
  415. <td width=""><strong><?php echo number_format($stats->totalsForAllResults['ga:visits']); ?></strong></td> 
  416. <td width=""><?php _e('Visits', 'google-analyticator'); ?></td> 
  417. <?php if ($stats->totalsForAllResults['ga:entrances'] <= 0) { ?> 
  418. <td width="20%"><strong>0.00%</strong></td> 
  419. <?php } else { ?> 
  420. <td width="20%"><strong><?php echo number_format(round(($stats->totalsForAllResults['ga:bounces'] / $stats->totalsForAllResults['ga:entrances']) * 100, 2), 2); ?>%</strong></td> 
  421. <?php } ?> 
  422. <td width="30%"><?php _e('Bounce Rate', 'google-analyticator'); ?></td> 
  423. </tr> 
  424. <tr> 
  425. <td><strong><?php echo number_format($stats->totalsForAllResults['ga:pageviews']); ?></strong></td> 
  426. <td><?php _e('Pageviews', 'google-analyticator'); ?></td> 
  427. <?php if ($stats->totalsForAllResults['ga:visits'] <= 0) { ?> 
  428. <td><strong>00:00:00</strong></td> 
  429. <?php } else { ?> 
  430. <td><strong><?php echo $this->sec2Time($stats->totalsForAllResults['ga:timeOnSite'] / $stats->totalsForAllResults['ga:visits']); ?></strong></td> 
  431. <?php } ?> 
  432. <td><?php _e('Avg. Time on Site', 'google-analyticator'); ?></td> 
  433. </tr> 
  434. <tr> 
  435. <?php if ($stats->totalsForAllResults['ga:visits'] <= 0) { ?> 
  436. <td><strong>0.00</strong></td> 
  437. <?php } else { ?> 
  438. <td><strong><?php echo number_format(round($stats->totalsForAllResults['ga:pageviews'] / $stats->totalsForAllResults['ga:visits'], 2), 2); ?></strong></td> 
  439. <?php } ?> 
  440. <td><?php _e('Pages/Visit', 'google-analyticator'); ?></td> 
  441. <?php if ($stats->totalsForAllResults['ga:visits'] <= 0) { ?> 
  442. <td><strong>0.00%</strong></td> 
  443. <?php } else { ?> 
  444. <td><strong><?php echo number_format(round(($stats->totalsForAllResults['ga:newVisits'] / $stats->totalsForAllResults['ga:visits']) * 100, 2), 2); ?>%</strong></td> 
  445. <td><?php _e('% New Visits', 'google-analyticator'); ?></td> 
  446. <?php } ?> 
  447. </tr> 
  448. </table> 
  449. <?php } 
  450.  
  451.  
  452. /** 
  453. * Get the top pages 
  454. **/ 
  455. function getTopPages() 
  456. # Get the metrics needed to build the top pages 
  457. $stats = $this->api->getMetrics('ga:pageviews', $this->date_before, $this->date_yesterday, 'ga:pageTitle, ga:pagePath', '-ga:pageviews', 'ga:pagePath!=/', 10); //'ga:pagePath!%3D%2F' 
  458. $rows = $stats->getRows(); 
  459.  
  460. # Check the size of the stats array 
  461. if (count($rows) <= 0 || !is_array($rows)) { 
  462. $return = '<p>' . __('There is no data for view.', 'google-analyticator') . '</p>'; 
  463. } else { 
  464. # Build the top pages list 
  465. $return = '<ol>'; 
  466.  
  467. # Set variables needed to correct (not set) bug 
  468. $new_stats = array(); 
  469. $notset_stats = array(); 
  470.  
  471. # Loop through each stat and create a new array 
  472. foreach ($rows AS $stat) { 
  473. # If the stat is not set 
  474. if ($stat[0] == '(not set)') { 
  475. # Add it to separate array 
  476. $notset_stats[] = $stat; 
  477. } else { 
  478. # Add it to new array with index set 
  479. $new_stats[$stat[1]] = $stat; 
  480.  
  481. # Loop through all the (not set) stats and attempt to add them to their correct stat 
  482. foreach ($notset_stats AS $stat) { 
  483. # If the stat has a "partner" 
  484. if ($new_stats[$stat[1]] != NULL) { 
  485. # Add the pageviews to the stat 
  486. $new_stats[$stat[1]][2] = $new_stats[$stat[1]][2] + $stat[2]; 
  487. } else { 
  488. # Stat goes to the ether since we couldn't find a partner (if anyone reads this and has a suggestion to improve, let me know) 
  489.  
  490. # Renew new_stats back to stats 
  491. $stats = $new_stats; 
  492.  
  493. # Sort the stats array, since adding the (not set) items may have changed the order 
  494. usort($stats, array( 
  495. $this,  
  496. 'statSort' 
  497. )); 
  498.  
  499. # Since we can no longer rely on the API as a limiter, we need to keep track of this ourselves 
  500. $stat_count = 0; 
  501.  
  502. # Loop through each stat for display 
  503. foreach ($stats AS $stat) { 
  504. $return .= '<li><a href="' . esc_url($stat[1]) . '">' . esc_html($stat[0]) . '</a> - ' . number_format($stat[2]) . ' ' . __('Views', 'google-analyticator') . '</li>'; 
  505.  
  506. # Increase the stat counter 
  507. $stat_count++; 
  508.  
  509. # Stop at 5 
  510. if ($stat_count >= 5) 
  511. break; 
  512.  
  513. # Finish the list 
  514. $return .= '</ol>'; 
  515. echo $return;  
  516.  
  517. /** 
  518. * Get the top referrers 
  519. **/ 
  520. function getTopReferrers() 
  521. # Get the metrics needed to build the top referrers 
  522. $stats = $this->api->getMetrics('ga:visits', $this->date_before, $this->date_yesterday, 'ga:source, ga:medium', '-ga:visits', 'ga:medium==referral', '5'); 
  523. $rows = $stats->getRows(); 
  524.  
  525. # Check the size of the stats array 
  526. if (count($rows) <= 0 || !is_array($rows)) { 
  527. echo '<p>' . __('There is no data for view.', 'google-analyticator') . '</p>'; 
  528. } else { 
  529. # Build the top pages list 
  530. echo '<ol>'; 
  531.  
  532. # Loop through each stat 
  533. foreach ($rows AS $stat) { 
  534. echo '<li><strong>' . esc_html($stat[0]) . '</strong> - ' . number_format($stat[2]) . ' ' . __('Visits', 'google-analyticator') . '</li>'; 
  535.  
  536. # Finish the list 
  537. echo '</ol>'; 
  538.  
  539. /** 
  540. * Get the top searches 
  541. **/ 
  542. function getTopSearches() 
  543. # Get the metrics needed to build the top searches 
  544. $stats = $this->api->getMetrics('ga:visits', $this->date_before, $this->date_yesterday, 'ga:keyword', '-ga:visits', 'ga:keyword!=(not set)', '5'); //'ga:keyword!=(not_set)' 
  545. $rows = $stats->getRows(); 
  546.  
  547. # Check the size of the stats array 
  548. if (count($rows) <= 0 || !is_array($rows)) { 
  549. echo '<p>' . __('There is no data for view.', 'google-analyticator') . '</p>'; 
  550. } else { 
  551. # Build the top pages list 
  552. echo '<ol>'; 
  553. # Loop through each stat 
  554. foreach ($rows AS $stat) { 
  555. echo '<li><strong>' . esc_html($stat[0]) . '</strong> - ' . number_format($stat[1]) . ' ' . __('Visits', 'google-analyticator') . '</li>'; 
  556. # Finish the list 
  557. echo '</ol>'; 
  558.  
  559. /** 
  560. * Sort a set of stats in descending order 
  561. * @return how the stat should be sorted 
  562. **/ 
  563. function statSort($x, $y) 
  564. if ($x[2] == $y[2]) 
  565. return 0; 
  566. elseif ($x[2] < $y[2]) 
  567. return 1; 
  568. else 
  569. return -1; 
  570.  
  571. /** 
  572. * Convert second to a time format 
  573. **/ 
  574. function sec2Time($time) 
  575. # Check if numeric 
  576. if (is_numeric($time)) { 
  577. $value = array( 
  578. "years" => '00',  
  579. "days" => '00',  
  580. "hours" => '00',  
  581. "minutes" => '00',  
  582. "seconds" => '00' 
  583. ); 
  584. if ($time >= 31556926) { 
  585. $value["years"] = floor($time / 31556926); 
  586. $time = ($time % 31556926); 
  587. if ($time >= 86400) { 
  588. $value["days"] = floor($time / 86400); 
  589. $time = ($time % 86400); 
  590. if ($time >= 3600) { 
  591. $value["hours"] = str_pad(floor($time / 3600), 2, 0, STR_PAD_LEFT); 
  592. $time = ($time % 3600); 
  593. if ($time >= 60) { 
  594. $value["minutes"] = str_pad(floor($time / 60), 2, 0, STR_PAD_LEFT); 
  595. $time = ($time % 60); 
  596. $value["seconds"] = str_pad(floor($time), 2, 0, STR_PAD_LEFT); 
  597.  
  598. # Get the hour:minute:second version 
  599. return $value['hours'] . ':' . $value['minutes'] . ':' . $value['seconds']; 
  600. } else { 
  601. return false; 
  602. } // END class 
/google-api-php-client/src/contrib/Google_UrlshortenerService.php  
  1. class Google_AnalyticsSummary extends Google_Model { 
  2. protected $__allTimeType = 'Google_AnalyticsSnapshot'; 
  3. protected $__allTimeDataType = ''; 
  4. public $allTime; 
  5. protected $__dayType = 'Google_AnalyticsSnapshot'; 
  6. protected $__dayDataType = ''; 
  7. public $day; 
  8. protected $__monthType = 'Google_AnalyticsSnapshot'; 
  9. protected $__monthDataType = ''; 
  10. public $month; 
  11. protected $__twoHoursType = 'Google_AnalyticsSnapshot'; 
  12. protected $__twoHoursDataType = ''; 
  13. public $twoHours; 
  14. protected $__weekType = 'Google_AnalyticsSnapshot'; 
  15. protected $__weekDataType = ''; 
  16. public $week; 
  17. public function setAllTime(Google_AnalyticsSnapshot $allTime) { 
  18. $this->allTime = $allTime; 
  19. public function getAllTime() { 
  20. return $this->allTime; 
  21. public function setDay(Google_AnalyticsSnapshot $day) { 
  22. $this->day = $day; 
  23. public function getDay() { 
  24. return $this->day; 
  25. public function setMonth(Google_AnalyticsSnapshot $month) { 
  26. $this->month = $month; 
  27. public function getMonth() { 
  28. return $this->month; 
  29. public function setTwoHours(Google_AnalyticsSnapshot $twoHours) { 
  30. $this->twoHours = $twoHours; 
  31. public function getTwoHours() { 
  32. return $this->twoHours; 
  33. public function setWeek(Google_AnalyticsSnapshot $week) { 
  34. $this->week = $week; 
  35. public function getWeek() { 
  36. return $this->week;