You are here

function google_analytics_reports_plugin_query_google_analytics::query in Google Analytics Reports 7.3

Generate a query and a countquery from all of the information supplied to the object.

Parameters

$get_count: Provide a countquery if this is true, otherwise provide a normal query.

Overrides views_plugin_query::query

1 call to google_analytics_reports_plugin_query_google_analytics::query()
google_analytics_reports_plugin_query_google_analytics::build in plugins/google_analytics_reports_plugin_query_google_analytics.inc
Builds the necessary info to execute the query.

File

plugins/google_analytics_reports_plugin_query_google_analytics.inc, line 146
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 query($get_count = FALSE) {
  $available_fields = google_analytics_reports_get_fields();
  $query = array();
  foreach ($this->fields as $field) {
    $field_name = google_analytics_reports_variable_to_custom_field($field['field']);
    if ($available_fields[$field_name]) {
      $type = $available_fields[$field_name]->type;
      $type = $type == 'dimension' ? 'dimensions' : 'metrics';
      $query[$type][] = 'ga:' . $field['field'];
    }
  }
  $filters = array();
  if (isset($this->where)) {
    foreach ($this->where as $where_group => $where) {
      foreach ($where['conditions'] as $condition) {
        $field_name = google_analytics_reports_variable_to_custom_field($condition['field']);
        if ($field_name == 'start_date' || $field_name == 'end_date' || $field_name == 'profile_id') {
          $query[$field_name] = intval($condition['value']);
        }
        elseif ($available_fields[$field_name]) {
          $filters[$where_group][] = 'ga:' . $condition['field'] . $condition['operator'] . $condition['value'];
        }
      }
      if (!empty($filters[$where_group])) {
        $glue = $where['type'] == 'AND' ? ';' : ',';
        $filters[$where_group] = implode($glue, $filters[$where_group]);
      }
    }
  }
  if (!empty($filters)) {
    $glue = $this->group_operator == 'AND' ? ';' : ',';
    $query['filters'] = implode($glue, $filters);
  }
  if (isset($this->orderby)) {
    foreach ($this->orderby as $field) {
      $query['sort_metric'][] = $field['direction'] . 'ga:' . $field['field'];
    }
  }

  // Change reports profile.
  if (isset($this->options['reports_profile']) && !empty($this->options['profile_id'])) {
    $query['profile_id'] = $this->options['profile_id'];
  }
  return $query;
}