You are here

function google_analytics_counter_display in Google Analytics Counter 6

Same name and namespace in other branches
  1. 6.2 google_analytics_counter.module \google_analytics_counter_display()
  2. 7.3 google_analytics_counter.module \google_analytics_counter_display()
  3. 7 google_analytics_counter.module \google_analytics_counter_display()
  4. 7.2 google_analytics_counter.module \google_analytics_counter_display()
2 calls to google_analytics_counter_display()
google_analytics_counter_block in ./google_analytics_counter.module
replace_google_analytics_counter_tags in ./google_analytics_counter.module

File

./google_analytics_counter.module, line 592

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 = '';

  /*
   $js_tooltip = '';
   $tooltip = "this is tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip";
   $js_tooltip .= <<<EOT
   $("#gact").tooltip({ effect: 'slide'});
   EOT;
   drupal_add_js($js_tooltip, 'inline', 'footer');
   $block_content .= '<a id="gact" href="/" title="'.$tooltip.'">*</a>';
  */
  $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);
  $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
\$(document).ready(function(){
setTimeout("gac_request_new_count()",{<span class="php-variable">$retrytime</span>});
});
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(){
  \$.getJSON(Drupal.settings.basePath + "{<span class="php-variable">$filename</span>}" + "/" + "{<span class="php-variable">$path</span>}", {nocache: "1"}, function(response) {
    \$.each(response, function(id, contents) {
      if (contents == '') {
        \$(id).parent().parent().parent().hide();
      }
      else {
        \$(id).html(contents);
      }
    });
  });
}
EOT;
  drupal_add_js($js_code, 'inline', 'footer');
  return $block_content;
}