public function GoogleAnalyticsQuery::query in Google Analytics Reports 8.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 QueryPluginBase::query
1 call to GoogleAnalyticsQuery::query()
- GoogleAnalyticsQuery::build in src/
Plugin/ views/ query/ GoogleAnalyticsQuery.php - Builds the necessary info to execute the query.
File
- src/
Plugin/ views/ query/ GoogleAnalyticsQuery.php, line 261
Class
- GoogleAnalyticsQuery
- Defines a Views query class for Google Analytics Reports API.
Namespace
Drupal\google_analytics_reports\Plugin\views\queryCode
public function query($get_count = FALSE) {
$available_fields = google_analytics_reports_get_fields();
$query = [];
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 = [];
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') {
// Remove dot from begging of the string.
$field_name = ltrim($field_name, '.');
$query[$field_name] = intval($condition['value']);
}
elseif (!empty($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->groupOperator == '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 (!empty($this->options['reports_profile']) && !empty($this->options['profile_id'])) {
$query['profile_id'] = $this->options['profile_id'];
}
return $query;
}