You are here

function seotools_dashboard_analytics_box in Drupal SEO Tools 7

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

File

./seotools.report.inc, line 79

Code

function seotools_dashboard_analytics_box($gid, $siteURL, $date_range, $enabled_modules) {
  if (!$enabled_modules['google_analytics_api']) {
    return '';
  }
  if ($output = seotools_cache_get('output_analytics_box', $date_range)) {
    $js = seotools_cache_get('js_analytics_box', $date_range);
    drupal_add_js(array(
      'seotools' => $js,
    ), 'setting');
    return $output;
  }

  // Grab the data.
  if ($rows = seotools_cache_get('data_ga_summary', $date_range, 6)) {
    $rows = unserialize($rows);
  }
  else {

    // For help with query see: https://ga-dev-tools.appspot.com/query-explorer.
    $request = array(
      '#dimensions' => array(
        'date',
      ),
      //'#metrics' => array('visits', 'pageviews', 'bounces', 'uniquePageviews'),
      '#metrics' => array(
        'visits',
        'pageviews',
        'timeOnSite',
        'bounces',
        'uniquePageviews',
        'newVisits',
        'goalCompletionsAll',
      ),
      '#sort_metric' => array(
        'date',
      ),
      '#start_date' => $date_range['start_date'],
      '#end_date' => $date_range['end_date'],
    );
    $rows = seotools_fetch_ga_data($request);
    seotools_cache_set('data_ga_summary', $rows, $date_range);
  }

  // Check for data.
  if (!$rows) {
    return '<p>No analytics data is currently available.</p>';
  }

  // Format and perform calculations to display charts.
  $totals = array(
    'visits' => 0,
    'goals' => 0,
    'pageviews' => 0,
    'average_pageviews' => 0,
    'bounces' => 0,
    'time_on_site' => 0,
    'vistor_type' => 0,
    'goals_per_visit' => 0,
  );
  $day_counts = array();
  foreach ($rows as $date => $row) {
    $day = array();
    $day_counts['visits'][] = $row['visits'];
    $totals['visits'] += $row['visits'];
    $day_counts['pageviews'][] = $row['pageviews'];
    $totals['pageviews'] += $row['pageviews'];
    $day_counts['average_pageviews'][] = $row['pageviews'] / ($row['visits'] ? $row['visits'] : 1);
    $day_counts['bounce_rate'][] = 100 * $row['bounces'] / ($row['uniquePageviews'] ? $row['uniquePageviews'] : 1);
    $totals['bounces'] += $row['bounces'];
    $day_counts['time_on_site'][] = $row['timeOnSite'] / ($row['visits'] ? $row['visits'] : 1);
    $totals['time_on_site'] += $row['timeOnSite'];
    $day_counts['vistor_type'][] = 100 * $row['newVisits'] / ($row['visits'] ? $row['visits'] : 1);
    $totals['vistor_type'] += $row['newVisits'];
    $day_counts['goals'][] = $row['goalCompletionsAll'];
    $totals['goals'] += $row['goalCompletionsAll'];
    $day_counts['goal_conversion_rate'][] = 100 * $row['goalCompletionsAll'] / ($row['visits'] ? $row['visits'] : 1);
    $chart_dates[] = date('d', strtotime($date));
  }
  $output .= '<h4 id="chart-main-title">' . t('Visits') . '</h4>';
  $output .= '<div class="chart-main-wrapper">';

  //$output .= '<img src="' . $chart . '" />';
  $output .= '</div>';
  $output .= '<h3>' . t('Site Usage') . '</h3>';
  $links[] = l(t('analytics'), 'https://www.google.com/analytics/reporting/dashboard', array(
    'query' => "id={$gid}",
    'attributes' => array(
      'target' => 'googleanalytics',
    ),
  ));
  $links[] = l(t('webmaster tools'), 'https://www.google.com/webmasters/tools/dashboard', array(
    'query' => "siteUrl={$siteURL}#",
    'attributes' => array(
      'target' => 'googlewebmastertools',
    ),
  ));
  $output .= '<div class="dashboard-links">';
  $output .= implode(' | ', $links);
  $output .= '</div>';
  $totals['time_on_site'] = seotools_format_time_on($totals['time_on_site'] / $totals['visits']);
  $totals['bounce_rate'] = number_format($totals['bounces'] / ($totals['visits'] ? $totals['visits'] : 1) * 100, 2) . "%";
  $totals['vistor_type'] = number_format($totals['vistor_type'] / ($totals['visits'] ? $totals['visits'] : 1) * 100, 2) . "%";
  $totals['visits'] = number_format($totals['visits']);
  $totals['pageviews'] = number_format($totals['pageviews']);
  $totals['average_pageviews'] = number_format($totals['pageviews'] / ($totals['visits'] ? $totals['visits'] : 1), 2);
  $totals['goals'] = number_format($totals['goals']);
  $totals['goal_conversion_rate'] = number_format($totals['goals'] / ($totals['visits'] ? $totals['visits'] : 1) / 10, 2) . "%";
  $cells = array(
    'visits' => t('Visits'),
    'bounce_rate' => t('Bounce Rate'),
    'pageviews' => t('Pageviews'),
    'time_on_site' => t('Avg. Time on Site'),
    'average_pageviews' => t('Pages/Visit'),
    'vistor_type' => t('% New Visits'),
    'goals' => t('Goals'),
    'goal_conversion_rate' => t('Goal Conversion'),
  );
  foreach ($cells as $key => $title) {
    $output .= '<div id="' . $key . '-cell" class="dashboard-ga-cell">' . "\n";
    $output .= '<a href="#" onclick="seotools_show_main_chart(\'' . $key . '_main\')">';
    $output .= chart_render(_seotools_reports_micro_chart($key . '_micro', '', $day_counts[$key], $chart_dates));
    $output .= '</a>';
    $output .= ' ' . $totals[$key];
    $queryadd = '';
    if ($key == 'goal_conversion_rate') {
      $queryadd = '&goal=-1';
    }
    $output .= ' ' . l($title, 'https://www.google.com/analytics/reporting/' . $key, array(
      'query' => "id={$gid}{$queryadd}",
      'attributes' => array(
        'target' => 'googleanalytics',
      ),
    ));
    $output .= '</div>' . "\n";
    $id = $key . '_main';
    $chart = chart_url(_seotools_reports_liquid_chart($id, '', $day_counts[$key], $chart_dates));
    $js['charts'][$id] = array(
      'src' => $chart,
      'location' => 'chart-main',
      'title' => $title,
    );
    if ($id != 'visits_main') {
      $js['charts'][$id]['load_only'] = 1;
    }
  }
  drupal_add_js(array(
    'seotools' => $js,
  ), 'setting');
  seotools_cache_set('js_analytics_box', $js, $date_range);
  seotools_cache_set('output_analytics_box', $output, $date_range);
  return $output;
}