You are here

function drush_google_analytics_reports_fields in Google Analytics Reports 7.3

Same name and namespace in other branches
  1. 8.3 google_analytics_reports.drush.inc \drush_google_analytics_reports_fields()

Import Google Analytics fields.

1 call to drush_google_analytics_reports_fields()
drush_google_analytics_reports_post_pm_enable in ./google_analytics_reports.drush.inc
Implements drush_MODULE_post_pm_enable().
1 string reference to 'drush_google_analytics_reports_fields'
google_analytics_reports_drush_command in ./google_analytics_reports.drush.inc
Implements hook_drush_command().

File

./google_analytics_reports.drush.inc, line 40
Drush integration for Google Analytics Reports module.

Code

function drush_google_analytics_reports_fields() {
  $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'])) {
      $context = array();
      foreach ($data['items'] as $item) {

        // Do not import deprecated fields.
        if ($item['attributes']['status'] == 'PUBLIC') {
          google_analytics_reports_fields_save($item, $context);
        }
      }
      drush_log(dt('Imported @count Google Analytics fields.', array(
        '@count' => count($context['results']),
      )), 'success');
    }
    else {
      drush_log(dt('An error has occurred during importing Google Analytics fields.'), 'error');
    }
  }
  else {
    drush_log(dt('There is a error during request to Google Analytics Metadata API: @error', array(
      '@error' => $response->error,
    )), 'error');
  }
}