You are here

function google_analytics_reports_plugin_query_google_analytics::add_field in Google Analytics Reports 7.3

Add a metric or dimension to the query.

Parameters

$table: NULL in most cases, we could probably remove this altogether.

$field: The name of the metric/dimension/field to add.

$alias: Probably could get rid of this too.

$params: Probably could get rid of this too.

Return value

$name The name that this field can be referred to as.

File

plugins/google_analytics_reports_plugin_query_google_analytics.inc, line 37
Defines the default query object which builds queries for the Google Analytics Reports API.

Class

google_analytics_reports_plugin_query_google_analytics
Object used to create a Google Analytics Core Reporting API query.

Code

function add_field($table, $field, $alias = '', $params = array()) {

  // We check for this specifically because it gets a special alias.
  if ($table == $this->base_table && $field == $this->base_field && empty($alias)) {
    $alias = $this->base_field;
  }
  if ($table && empty($this->table_queue[$table])) {
    $this
      ->ensure_table($table);
  }
  if (!$alias && $table) {
    $alias = $table . '_' . $field;
  }

  // Make sure an alias is assigned.
  $alias = $alias ? $alias : $field;

  // We limit the length of the original alias up to 60 characters
  // to get a unique alias later if its have duplicates.
  $alias = substr($alias, 0, 60);

  // Create a field info array.
  $field_info = array(
    'field' => $field,
    'table' => $table,
    'alias' => $alias,
  ) + $params;

  // Test to see if the field is actually the same or not. Due to
  // differing parameters changing the aggregation function, we need
  // to do some automatic alias collision detection:
  $base = $alias;
  $counter = 0;
  while (!empty($this->fields[$alias]) && $this->fields[$alias] != $field_info) {
    $field_info['alias'] = $alias = $base . '_' . ++$counter;
  }
  if (empty($this->fields[$alias])) {
    $this->fields[$alias] = $field_info;
  }

  // Keep track of all aliases used.
  $this->field_aliases[$table][$field] = $alias;
  return $alias;
}