function google_analytics_reports_fields_import in Google Analytics Reports 7.3
Import Google Analytics fields to database using Metadata API.
See also
https://developers.google.com/analytics/devguides/reporting/metadata/v3/
2 calls to google_analytics_reports_fields_import()
- google_analytics_reports_install in ./
google_analytics_reports.install - Implements hook_install().
- google_analytics_reports_update_7304 in ./
google_analytics_reports.install - Import Google Analytics fields.
1 string reference to 'google_analytics_reports_fields_import'
File
- ./
google_analytics_reports.module, line 83 - Front-end interfaces that use the Google Analytics Reports API module.
Code
function google_analytics_reports_fields_import() {
$response = drupal_http_request('https://www.googleapis.com/analytics/v3/metadata/ga/columns');
if ($response->code == '200') {
// Remove old fields and clear cache.
if (db_table_exists('google_analytics_reports_fields')) {
db_truncate('google_analytics_reports_fields')
->execute();
}
cache_clear_all('google_analytics_reports_fields', 'cache');
$data = drupal_json_decode($response->data);
// Save current time as last executed time.
variable_set('google_analytics_reports_metadata_last_time', time());
// Save etag identifier. It is used to check updates for the fields.
// @see https://developers.google.com/analytics/devguides/reporting/metadata/v3/devguide#etag
if (!empty($data['etag'])) {
variable_set('google_analytics_reports_metadata_etag', $data['etag']);
}
if (!empty($data['items'])) {
$operations = array();
foreach ($data['items'] as $item) {
// Do not import deprecated fields.
if ($item['attributes']['status'] == 'PUBLIC') {
$operations[] = array(
'google_analytics_reports_fields_save',
array(
$item,
),
);
}
}
$batch = array(
'operations' => $operations,
'title' => t('Importing Google Analytics fields'),
'finished' => 'google_analytics_reports_import_fields_finished',
);
batch_set($batch);
}
}
else {
drupal_set_message(t('There is a error during request to Google Analytics Metadata API: @error', array(
'@error' => $response->error,
)), 'error');
}
}