You are here

function seotools_generate_report_top_and_trends in Drupal SEO Tools 6

Same name and namespace in other branches
  1. 7 seotools.report.inc \seotools_generate_report_top_and_trends()

valid ids: keywords

Parameters

unknown_type $id:

unknown_type $label:

unknown_type $mode:

unknown_type $gid:

unknown_type $siteURL:

unknown_type $date_range:

unknown_type $enabled_modules:

6 calls to seotools_generate_report_top_and_trends()
seotools_dashboard_entrances_box in ./seotools.report.inc
seotools_dashboard_keywords_box in ./seotools.report.inc
seotools_dashboard_page in ./seotools.report.inc
seotools_dashboard_pageviews_box in ./seotools.report.inc
seotools_dashboard_referrers_box in ./seotools.report.inc

... See full list

File

./seotools.report.inc, line 551

Code

function seotools_generate_report_top_and_trends($id, $label, $mode, $gid, $siteURL, $date_range, $enabled_modules) {
  $page_link_icon = '<img src="' . base_path() . drupal_get_path('module', 'seotools') . '/images/url_icon.gif">';
  if ($mode == 'full') {
    $top_style = variable_get('seotools_report_top_' . $id . '_style', SEOTOOLS_REPORT_STYLE_DEFAULT);
    $top_row_count = variable_get('seotools_report_top_' . $id . '_rows', SEOTOOLS_REPORT_ROWS_DEFAULT);
    $top_row_count_ext = $top_row_count + 10;
    $trend_style = variable_get('seotools_report_top_' . $id . '_style', SEOTOOLS_REPORT_STYLE_DEFAULT);
    $trend_row_count = variable_get('seotools_report_top_' . $id . '_rows', SEOTOOLS_REPORT_ROWS_DEFAULT);
    $trend_row_count_ext = $trend_row_count + 10;
  }
  else {
    $top_style = variable_get('seotools_report_top_' . $id . '_dashboard_style', SEOTOOLS_REPORT_DASHBOARD_STYLE_DEFAULT);
    $top_row_count = variable_get('seotools_report_top_' . $id . '_dashboard_rows', SEOTOOLS_REPORT_DASHBOARD_ROWS_DEFAULT);
    $top_row_count_ext = $top_row_count + 200;
    $trend_style = variable_get('seotools_report_top_' . $id . '_dashboard_style', SEOTOOLS_REPORT_DASHBOARD_STYLE_DEFAULT);
    $trend_row_count = variable_get('seotools_report_top_' . $id . '_dashboard_rows', SEOTOOLS_REPORT_DASHBOARD_ROWS_DEFAULT);
    $trend_row_count_ext = $trend_row_count + 200;
  }
  $style = 'simple';
  if ($top_row_count && $top_style != 'simple' || $trend_row_count && $trend_style != 'simple') {
    $style = 'full';
  }
  $row_count_ext = $top_row_count_ext;
  if ($trend_row_count && $trend_row_count_ext > $row_count_ext) {
    $row_count_ext = $trend_row_count_ext;
  }
  if ($row_count_ext > 1000) {
    $row_count_ext = 1000;
  }

  // configure ga request
  $metrics = array(
    'ga:visits',
  );
  $sort_metric = array(
    '-ga:visits',
  );
  $count = 'visits';
  if ($id == 'keywords') {
    $dimensions = array(
      'ga:keyword',
    );
  }
  else {
    if ($id == 'entrances') {
      $dimensions = array(
        'ga:pagePath',
      );
    }
    else {
      if ($id == 'sources') {
        $dimensions = array(
          'ga:source',
        );
      }
      else {
        if ($id == 'referrers') {
          $dimensions = array(
            'ga:source',
            'ga:referralPath',
          );
        }
        else {
          if ($id == 'pageviews') {
            $dimensions = array(
              'ga:pagePath',
            );
            $metrics = array(
              'ga:pageviews',
            );
            $sort_metric = array(
              '-ga:pageviews',
            );
            $count = 'pageviews';
          }
        }
      }
    }
  }
  if (!($data = seotools_cache_get('data_ga_' . $id, $date_range, $row_count_ext))) {

    // for help with query see: http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
    $request = array(
      'dimensions' => $dimensions,
      'metrics' => $metrics,
      'sort_metric' => $sort_metric,
      'max_results' => $row_count_ext,
      'start_date' => strtotime($date_range['start_date']),
      'end_date' => strtotime($date_range['end_date']),
    );
    $data = seotools_fetch_ga_data($request, $dimensions);
    seotools_cache_set('data_ga_' . $id, $data, $date_range);
  }
  if ($style == 'full') {

    // get yesterday's data
    if (!($yesterdays_data = seotools_cache_get('data_ga_' . $id . '_yesterday', $date_range, $row_count_ext))) {

      // for help with query see: http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
      $request = array(
        'dimensions' => $dimensions,
        'metrics' => $metrics,
        'sort_metric' => $sort_metric,
        'max_results' => $row_count_ext,
        'start_date' => strtotime(date("Y-m-d", strtotime('-1 days'))),
        'end_date' => strtotime(date("Y-m-d", strtotime('-1 days'))),
      );
      $yesterdays_data = seotools_fetch_ga_data($request, $dimensions);
      seotools_cache_set('data_ga_' . $id . '_yesterday', $yesterdays_data, $date_range);
    }

    // get last weeks data
    if (!($aweekago_data = seotools_cache_get('data_ga_' . $id . '_aweekago', $date_range, $row_count_ext))) {

      // for help with query see: http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
      $request = array(
        'dimensions' => $dimensions,
        'metrics' => $metrics,
        'sort_metric' => $sort_metric,
        'max_results' => $row_count_ext,
        'start_date' => strtotime(date("Y-m-d", strtotime('-7 days'))),
        'end_date' => strtotime(date("Y-m-d", strtotime('-7 days'))),
      );
      $aweekago_data = seotools_fetch_ga_data($request, $dimensions);
      seotools_cache_set('data_ga_' . $id . '_aweekago', $aweekago_data, $date_range);
    }
  }
  if ($style == 'full' || $trend_row_count) {

    // get today's data
    $request = array(
      'dimensions' => $dimensions,
      'metrics' => $metrics,
      'sort_metric' => $sort_metric,
      'max_results' => $row_count_ext,
      'start_date' => strtotime(date("Y-m-d")),
    );
    $todays_data = seotools_fetch_ga_data($request, $dimensions);
  }
  $site_domain = $_SERVER['HTTP_HOST'];

  //$kw_priority_options = kwresearch_get_priority_options();
  $c = 1;
  foreach ($data as $key => $value) {
    $row = array();
    if ($id == 'keywords') {
      if ($key == '(not set)') {
        continue;
      }
      $row[] = l(seotools_format_text_data($key), 'https://www.google.com/analytics/reporting/keyword_detail', array(
        'query' => "id={$gid}&d1={$keyword}",
        'attributes' => array(
          'target' => 'googleanalytics',
        ),
      ));
      if ($enabled_modules['kwresearch']) {
        $kw_obj = kwresearch_load_site_keyword($key);
        $row[] = array(
          'data' => l($kw_obj->priority ? $kw_obj->priority . '%' : 'ns', 'admin/content/kwresearch/keyword_list/edit', array(
            'query' => "keyword={$key}",
            'attributes' => array(
              'target' => 'kwreserach',
              'title' => $kw_priority_options[$kw_obj->priority],
            ),
          )),
          'class' => 'text-cell',
        );
      }
    }
    else {
      if ($id == 'entrances') {
        $path = str_replace($base_path, '', $key);
        if ($path == '') {
          $path = '<front>';
        }
        $row[] = l(seotools_format_text_data($key), 'https://www.google.com/analytics/reporting/entrances', array(
          'query' => "id={$gid}&d1={$key}",
          'attributes' => array(
            'target' => 'googleanalytics',
          ),
        ));
        if ($enabled_modules['linkintel']) {
          $page_settings = linkintel_load_page_settings($path);
          $row[] = array(
            'data' => l($page_settings->priority ? $page_settings->priority . '%' : 'ns', 'admin/content/linkintel/page/edit', array(
              'query' => "pid={$path}",
              'attributes' => array(
                'target' => 'linkintel',
              ),
            )),
            'class' => 'text-cell',
          );
        }
      }
      else {
        if ($id == 'sources') {
          $fsource = $key;
          if ($source == '(none)') {
            $fsource = 'direct';
          }
          $row[] = l(seotools_format_text_data($fsource), 'https://www.google.com/analytics/reporting/referring_source_detail', array(
            'query' => "id={$gid}&d1={$key}",
            'attributes' => array(
              'target' => 'googleanalytics',
            ),
          ));
        }
        else {
          if ($id == 'referrers') {
            if (strpos($key, '(not set)') !== FALSE) {
              continue;
            }
            $row[] = l($page_link_icon, 'http://' . $key, array(
              'html' => TRUE,
              'attributes' => array(
                'target' => '_blank',
              ),
            )) . ' ' . l(seotools_format_text_data($key), 'https://www.google.com/analytics/reporting/referring_link_detail', array(
              'query' => "id={$gid}&d1={$key}",
              'attributes' => array(
                'target' => 'googleanalytics',
              ),
            ));
          }
          else {
            if ($id == 'pageviews') {
              $row[] = l($page_link_icon, 'http://' . $site_domain . $key, array(
                'html' => TRUE,
                'attributes' => array(
                  'target' => '_blank',
                ),
              )) . ' ' . l(seotools_format_text_data($key), 'https://www.google.com/analytics/reporting/content_detail', array(
                'query' => "id={$gid}&d1={$key}",
                'attributes' => array(
                  'target' => 'googleanalytics',
                ),
              ));
            }
          }
        }
      }
    }
    $row[] = array(
      'data' => number_format($value[$count]),
      'class' => 'numeric-cell',
    );
    if ($top_style == 'full') {
      $today = '-';
      if ($todays_data[$key]) {
        $today = number_format($todays_data[$key][$count]);
      }
      $yesterday = '-';
      if ($yesterdays_data[$key]) {
        $yesterday = number_format($yesterdays_data[$key][$count]);
      }
      $aweekago = '-';
      if ($aweekago_data[$key]) {
        $aweekago = number_format($aweekago_data[$key][$count]);
      }
      $row[] = array(
        'data' => number_format($value[$count] / $date_range['days'], 0),
        'class' => 'numeric-cell',
      );
      $row[] = array(
        'data' => $aweekago,
        'class' => 'numeric-cell',
      );
      $row[] = array(
        'data' => $yesterday,
        'class' => 'numeric-cell',
      );
      $row[] = array(
        'data' => $today,
        'class' => 'numeric-cell',
      );
    }
    $rows[] = $row;
    $c++;
    if ($c > $top_row_count) {
      break;
    }
  }
  $header = array();
  $header[] = array(
    'data' => t('Top') . ' ' . $label,
    'class' => 'label-header',
  );
  if ($id == 'keywords' && $enabled_modules['kwresearch'] || $id == 'entrances' && $enabled_modules['linkintel']) {
    $header[] = array(
      'data' => t('Prty'),
      'class' => 'text-header',
    );
  }
  $header[] = array(
    'data' => t('Visits'),
    'class' => 'numeric-header',
  );
  if ($top_style == 'full') {
    $header[] = array(
      'data' => t('/day'),
      'class' => 'numeric-header',
    );
    $header[] = array(
      'data' => t('Wkago'),
      'class' => 'numeric-header',
    );
    $header[] = array(
      'data' => t('Yda'),
      'class' => 'numeric-header',
    );
    $header[] = array(
      'data' => t('Today'),
      'class' => 'numeric-header',
    );
  }
  $output .= theme('table', $header, $rows, array(
    'id' => 'seotools-report-top-' . $id . '-' . $mode,
  ));
  if (!is_array($todays_data)) {
    return $output;
  }

  // Trending data
  $c = 1;
  $rows = array();
  foreach ($todays_data as $key => $value) {
    $row = array();
    if ($id == 'keywords') {
      if ($key == '(not set)') {
        continue;
      }
      $row[] = l(seotools_format_text_data($key), 'https://www.google.com/analytics/reporting/keyword_detail', array(
        'query' => "id={$gid}&d1={$source}",
        'attributes' => array(
          'target' => 'googleanalytics',
        ),
      ));
      if ($enabled_modules['kwresearch']) {
        $kw_obj = kwresearch_load_site_keyword($key);
        $row[] = array(
          'data' => l($kw_obj->priority ? $kw_obj->priority . '%' : 'ns', 'admin/content/kwresearch/keyword_list/edit', array(
            'query' => "keyword={$key}",
            'attributes' => array(
              'target' => 'kwreserach',
              'title' => $kw_priority_options[$kw_obj->priority],
            ),
          )),
          'class' => 'text-cell',
        );
      }
    }
    else {
      if ($id == 'entrances') {
        $path = str_replace($base_path, '', $key);
        if ($path == '') {
          $path = '<front>';
        }
        $row[] = l(seotools_format_text_data($key), 'https://www.google.com/analytics/reporting/entrances', array(
          'query' => "id={$gid}&d1={$key}",
          'attributes' => array(
            'target' => 'googleanalytics',
          ),
        ));
        if ($enabled_modules['linkintel']) {
          $page_settings = linkintel_load_page_settings($path);
          $row[] = array(
            'data' => l($page_settings->priority ? $page_settings->priority . '%' : 'ns', 'admin/content/linkintel/page/edit', array(
              'query' => "pid={$path}",
              'attributes' => array(
                'target' => 'linkintel',
              ),
            )),
            'class' => 'text-cell',
          );
        }
      }
      else {
        if ($id == 'sources') {
          $fsource = $key;
          if ($source == '(none)') {
            $fsource = 'direct';
          }
          $row[] = l(seotools_format_text_data($fsource), 'https://www.google.com/analytics/reporting/referring_source_detail', array(
            'query' => "id={$gid}&d1={$key}",
            'attributes' => array(
              'target' => 'googleanalytics',
            ),
          ));
        }
        else {
          if ($id == 'referrers') {
            if (strpos($key, '(not set)') !== FALSE) {
              continue;
            }
            $row[] = l($page_link_icon, 'http://' . $key, array(
              'html' => TRUE,
              'attributes' => array(
                'target' => '_blank',
              ),
            )) . ' ' . l(seotools_format_text_data($key), 'https://www.google.com/analytics/reporting/referring_link_detail', array(
              'query' => "id={$gid}&d1={$key}",
              'attributes' => array(
                'target' => 'googleanalytics',
              ),
            ));
          }
          else {
            if ($id == 'pageviews') {
              $row[] = l($page_link_icon, 'http://' . $site_domain . $key, array(
                'html' => TRUE,
                'attributes' => array(
                  'target' => '_blank',
                ),
              )) . ' ' . l(seotools_format_text_data($key), 'https://www.google.com/analytics/reporting/content_detail', array(
                'query' => "id={$gid}&d1={$key}",
                'attributes' => array(
                  'target' => 'googleanalytics',
                ),
              ));
            }
          }
        }
      }
    }
    $row[] = array(
      'data' => number_format($value[$count]),
      'class' => 'numeric-cell',
    );
    if ($trend_style == 'full') {
      $yesterday = '-';
      if ($yesterdays_data[$key]) {
        $yesterday = number_format($yesterdays_data[$key][$count]);
      }
      $aweekago = '-';
      if ($aweekago_data[$key]) {
        $aweekago = number_format($aweekago_data[$key][$count]);
      }
      $histavg = '-';
      if ($data[$key]) {
        $histavg = number_format($data[$key][$count] / $date_range['days'], 0);
      }
      $row[] = array(
        'data' => $histavg,
        'class' => 'numeric-cell',
      );
      $row[] = array(
        'data' => $aweekago,
        'class' => 'numeric-cell',
      );
      $row[] = array(
        'data' => $yesterday,
        'class' => 'numeric-cell',
      );
    }
    $rows[] = $row;
    $c++;
    if ($c > $trend_row_count) {
      break;
    }
  }
  $header = array();
  $header[] = array(
    'data' => t('Trending') . ' ' . $label,
    'class' => 'label-header',
  );
  if ($id == 'keywords' && $enabled_modules['kwresearch'] || $id == 'entrances' && $enabled_modules['linkintel']) {
    $header[] = array(
      'data' => t('Prty'),
      'class' => 'text-header',
    );
  }
  $header[] = array(
    'data' => t('Today'),
    'class' => 'numeric-header',
  );
  if ($top_style == 'full') {
    $header[] = array(
      'data' => t('/day'),
      'class' => 'numeric-header',
    );
    $header[] = array(
      'data' => t('Wkago'),
      'class' => 'numeric-header',
    );
    $header[] = array(
      'data' => t('Yda'),
      'class' => 'numeric-header',
    );
  }
  $output .= theme('table', $header, $rows, array(
    'id' => 'seotools-report-trend-' . $id . '-' . $mode,
  ));
  return $output;
}