You are here

function getclicky_footer in Clicky - Web Analytics in Real Time 6

Same name and namespace in other branches
  1. 5 getclicky.module \getclicky_footer()

Implementation of hook_footer() to insert Javascript at the end of the page

File

./getclicky.module, line 93

Code

function getclicky_footer($main = 0) {
  global $user;
  $user = user_load($user->uid);
  $site_id_number = variable_get('getclicky_site_id_number', '');
  $segmentation = '';
  $script = '';
  $arg = arg();
  $arg = implode('/', $arg);
  if ($site_id_number && __getclicky_should_we_track_path($arg) && __getclicky_should_we_track_user($user)) {

    // Add User profile segmentation values
    if (is_array($profile_fields = variable_get('getclicky_segmentation', '')) && $user->uid > 0) {

      // pass by ref in node_invoke NOT possible!
      //      $p_bool = module_invoke('profile', 'load_profile', &$user);

      //$fields = array();
      if (count($profile_fields) > 0) {
        $segmentation .= <<<HERE
<script type="text/javascript">
  var clicky_custom = {};
  clicky_custom.session = {

HERE;
        foreach ($profile_fields as $field_drupal_name => $title) {
          $field_value = $user->{$field_drupal_name};
          if (is_array($field_value)) {
            foreach ($field_value as $key => $value) {
              $field_value[$key] = "'" . $field_value[$key] . "'";
            }
            $field_value = "[" . implode(',', $field_value) . "]";
          }
          else {
            $field_value = "'" . $field_value . "'";
          }
          $mapping = unserialize(GETCLICKY_FIELDS_TO_CLICKY_DEFAULT_MAPPINGS);
          $field_js_name = !empty($mapping[$field_drupal_name]) ? $mapping[$field_drupal_name] : $field_drupal_name;
          $segmentation .= <<<HERE
    '{<span class="php-variable">$field_js_name</span>}' : {<span class="php-variable">$field_value</span>},

HERE;
        }

        // Only show segmentation variable if there are specified fields.
        $segmentation .= <<<HERE
  };
</script>
HERE;
      }
      $script .= $segmentation;
    }

    // Site search tracking support.
    $url_custom = '';
    if (module_exists('search') && variable_get('getclicky_site_search', FALSE) && arg(0) == 'search') {
      $keys = search_get_keys();
      $url_custom = url('search/' . arg(1), 'search=' . trim($keys));
    }

    // Surround custom urls with single quotes.
    if (!empty($url_custom)) {
      $url_custom = drupal_to_js($url_custom);
    }

    //    print_r($url_custom);
    // todo add code for url custom
    $script .= __getclicky_output_js_code($site_id_number);

    //    print_r($script);
    return $script;
  }
}