You are here

function google_analytics_counter_display in Google Analytics Counter 7

Same name and namespace in other branches
  1. 6.2 google_analytics_counter.module \google_analytics_counter_display()
  2. 6 google_analytics_counter.module \google_analytics_counter_display()
  3. 7.3 google_analytics_counter.module \google_analytics_counter_display()
  4. 7.2 google_analytics_counter.module \google_analytics_counter_display()

@todo Please document this function.

See also

http://drupal.org/node/1354

2 calls to google_analytics_counter_display()
google_analytics_counter_block_view in ./google_analytics_counter.module
Implements hook_block_view().
replace_google_analytics_counter_tags in ./google_analytics_counter.module

File

./google_analytics_counter.module, line 713

Code

function google_analytics_counter_display($path = '') {
  if ($path == '') {
    $path = google_analytics_counter_get_searched_path();
  }
  $refreshinseconds = variable_get('google_analytics_counter_ajax_refresh', '');
  if (!is_numeric($refreshinseconds)) {
    $refreshinseconds = 0;
  }
  $msec = $refreshinseconds * 1000;
  $block_content = '';
  $block_content .= '<span id="google-analytics-counter-' . md5($path) . '">';

  //$block_content .= '<span id="google-analytics-counter">';
  $sumarray = google_analytics_counter_get_sum_per_path($path);
  if (@$sumarray[0] == 0) {
    $block_content .= 0;
  }
  else {
    $block_content .= google_analytics_counter_construct_content($sumarray);
  }
  $block_content .= '</span>';
  $js_code = '';

  /*
   // If the page is cached, then this makes a quick counter refresh shortly after loading the page.
   // Or, if the initially calculated count is zero, it may either be a new page, or a retrieval mistake.
   // Esp. for the latter, we want to retry, if only to hide the block from view.
   // For that we need to run the AJAX call shorly after page loads up.
   // Only if this is not already an AJAX call!
   if (arg(0) <> 'google_analytics_counter' AND arg(1) <> 'gac_get_count') {
   if (@$sumarray[0] == 0) {
   $retrytimeseconds = 7;
   $retrytime = $retrytimeseconds * 1000;
   $js_code .= <<<EOT
   jQuery(document).ready(function(){
   setTimeout("gac_request_new_count()",$retrytime);
   });
   EOT;
   }
   }
  */

  // If refresh is set to zero, then do not refresh at all.
  if ($msec != 0) {
    $js_code .= <<<EOT
var refreshId = setInterval( "gac_request_new_count()", {<span class="php-variable">$msec</span>});
EOT;
  }

  // Common code.
  $filename = "google_analytics_counter/gac_get_count";
  $js_code .= <<<EOT
function gac_request_new_count(){
  jQuery.getJSON(Drupal.settings.basePath + "{<span class="php-variable">$filename</span>}" + "/" + "{<span class="php-variable">$path</span>}", {nocache: "1"}, function(response) {
    jQuery.each(response, function(id, contents) {
      if (contents == '') {
        jQuery(id).parent().parent().parent().hide();
      }
      else {
        jQuery(id).html(contents);
      }
    });
  });
}
EOT;
  drupal_add_js($js_code, array(
    'type' => 'inline',
    'scope' => 'footer',
  ));
  return $block_content;
}