You are here

function seotools_dashboard_sources_box in Drupal SEO Tools 6

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

File

./seotools.report.inc, line 1035

Code

function seotools_dashboard_sources_box($gid, $siteURL, $date_range, $enabled_modules, &$mediums_data) {

  // Query for browser usage information.
  $output = '<h3 class="referrers">' . t('Sources') . '</h3>';
  $links[] = l(t('report'), 'admin/content/seotools/sources');
  $links[] = l(t('analytics'), 'https://www.google.com/analytics/reporting/sources', array(
    'query' => "id={$gid}",
    'attributes' => array(
      'target' => 'googleanalytics',
    ),
  ));
  $links[] = l(t('subscriber stats'), 'https://www.google.com/webmasters/tools/subscribers', array(
    'query' => "siteUrl={$siteURL}#",
    'attributes' => array(
      'target' => 'googlewebmastertools',
    ),
  ));
  $output .= '<div class="dashboard-links">';
  $output .= implode(' | ', $links);
  $output .= '</div>';
  if (!($data = seotools_cache_get('data_ga_mediums', $date_range))) {
    $request = array(
      'dimensions' => array(
        'ga:medium',
      ),
      'metrics' => array(
        'ga:visits',
      ),
      'sort_metric' => array(
        '-ga:visits',
      ),
      'max_results' => 20,
      //'start_date' => $date_range['start_date'],

      //'end_date' => $date_range['end_date']
      'start_date' => strtotime($date_range['start_date']),
      'end_date' => strtotime($date_range['end_date']),
    );
    $data = seotools_fetch_ga_data($request, 'medium');
    seotools_cache_set('data_ga_mediums', $data, $date_range);
  }
  $mediums = array();
  $total_visits = 0;
  foreach ($data as $key => $value) {
    if ($key == '(none)') {
      $key = 'direct';
    }
    $total_visits += $value['visits'];
    $mediums[$key] = $value['visits'];
  }

  // Any browsers with a marketshare below 0.1% don't get shown.
  $filtered_mediums = array();
  $filtered_mediums_legend = array();
  $total_filtered = 0;
  $threshold = $total_visits * 0.01;
  arsort($mediums);
  $segments = 0;
  foreach ($mediums as $key => $value) {
    if ($segments >= 3) {
      break;
    }
    if ($value > $threshold) {
      $percent = $value / $total_visits;
      $filtered_mediums[$key] = $percent;
      $filtered_mediums_legend[$key] = $key . " (" . number_format($value) . ")";
      $total_filtered += $value;
      $segments++;
    }
  }
  $filtered_mediums['other'] = ($total_visits - $total_filtered) / $total_visits;
  $filtered_mediums_legend['other'] = "other (" . number_format($total_visits - $total_filtered) . ")";

  // Create browser chart.
  $colors = seotools_get_data_colors();
  arsort($filtered_mediums);
  $mediums_data = array();
  $i = 0;
  foreach ($filtered_mediums as $medium => $value) {
    $mediums_data[$medium] = $colors[$i];
    $i++;
  }
  $chart = array(
    '#chart_id' => 'medium_box',
    '#title' => '',
    '#type' => CHART_TYPE_PIE,
    '#size' => chart_size(300, 150),
    '#data_colors' => chart_data_colors($mediums_data),
    '#data' => $filtered_mediums,
    '#legends' => $filtered_mediums_legend,
  );
  $r_chart = chart_url($chart);
  $js['charts']['sources_pie'] = array(
    'src' => $r_chart,
    'location' => 'chart-sources',
  );
  drupal_add_js(array(
    'seotools' => $js,
  ), 'setting');
  $output .= '<div class="chart-sources-wrapper">';

  //$output .= $r_chart;
  $output .= '</div>';
  $output .= seotools_generate_report_top_and_trends('sources', t('sources'), $mode, $gid, $siteURL, $date_range, $enabled_modules);
  return $output;
}