You are here

function seotools_dashboard_map_box in Drupal SEO Tools 6

Same name and namespace in other branches
  1. 7 seotools.report.inc \seotools_dashboard_map_box()
1 call to seotools_dashboard_map_box()
seotools_dashboard_page in ./seotools.report.inc

File

./seotools.report.inc, line 1200

Code

function seotools_dashboard_map_box($gid, $siteURL, $date_range, $enabled_modules, &$mediums_data) {
  if (!module_exists('countries_api')) {
    $output .= '<h3>' . t('Map Overlay') . '</h3>';
    $output .= t('To enable map statistics, enable the Counties API module.');
    return $output;
  }

  // geographic data
  if (!($data = seotools_cache_get('data_ga_country', $date_range))) {
    $request = array(
      'dimensions' => array(
        'ga:pagePath',
        'ga:country',
      ),
      'metrics' => array(
        'ga:pageviews',
      ),
      'sort_metric' => array(
        'ga:country',
        'ga:pagePath',
      ),
      'start_date' => strtotime($date_range['start_date']),
      'end_date' => strtotime($date_range['end_date']),
    );
    $data = seotools_fetch_ga_data($request, 'country');
    seotools_cache_set('data_ga_country', $data, $date_range);
  }
  $name_to_iso = countries_api_get_array('name', 'iso2');
  $countries = array();
  $max_pageviews = 0;
  foreach ($data as $key => $value) {
    $match = $name_to_iso[strtoupper($key)];
    if ($match) {
      $max_pageviews = max($max_pageviews, $value['pageviews']);
      $countries[$match] = $value['pageviews'];
    }
  }

  // Find out proportion share from 1-100 for each country.
  $countries_share = array();
  foreach ($countries as $key => $value) {
    $countries_share[$key] = round($value / $max_pageviews * 100);
  }

  // Create the geographical chart.
  $chart = array(
    '#chart_id' => 'countries',
    '#type' => CHART_TYPE_MAP,
    '#size' => chart_size(440, 220),
    '#georange' => 'world',
    '#countries' => array_keys($countries_share),
    '#data' => array_values($countries_share),
    '#data_colors' => array(
      'ffffff',
      'edf0d4',
      '13390a',
    ),
  );
  $output .= '<h3>' . t('Map Overlay') . '</h3>';

  //$r_chart = chart_url($chart);
  $r_chart = chart_render($chart);
  $js['charts']['sources_map'] = array(
    'src' => $r_chart,
    'location' => 'chart-map',
  );

  // TODO figure out why js crashing

  //drupal_add_js( array('seotools' => $js), 'setting');
  $output .= '<div class="chart-map-wrapper">';
  $output .= $r_chart;
  $output .= '</div>';
  return $output;
}