You are here

function kwresearch_get_keyword_stats_data in Keyword Research 7

Same name and namespace in other branches
  1. 6 includes/stats.inc \kwresearch_get_keyword_stats_data()

Generates analysis data for keyword stats report. Data is generated by

Parameters

string|array $keywords - list of keywords for which to generate data:

array $msgs - used to return error messages:

array $params - extra parmaters:

5 calls to kwresearch_get_keyword_stats_data()
kwresearch_analyze_js in ./kwresearch.module
AJAX handler to generate keyword stats report for content analysis display
kwresearch_google_keywords_import_form_submit in modules/kwresearch_google/kwresearch_google.module
Processes Google keywords import form
kwresearch_keyword_stats_report_page in includes/stats_report.inc
Displays keyword stats report
kwresearch_save_site_keyword in ./kwresearch.module
Saves site keyword user settings data to database
kwresearch_stats_report_form_submit in includes/stats_report.inc
Stats report form submit handler.

File

includes/stats.inc, line 16
Functions for generating data for keyword stats reports. Implements a kwresearch_sources API.

Code

function kwresearch_get_keyword_stats_data($keywords, &$msgs, $params = array()) {

  //dsm($params);

  // make api calls
  $sources = module_invoke_all('kwresearch_sources');
  foreach ($sources as $aid => $source) {
    $data[$aid] = call_user_func($source['stats_callback'], $keywords, $msgs, $params);
  }

  //dsm($data);
  $ret = array();
  if (!empty($data) && is_array($data)) {
    foreach ($data as $aid => $d) {
      if (!empty($d) && is_array($d)) {
        foreach ($d as $kw => $d2) {
          if (!empty($d2) && is_array($d2)) {
            foreach ($d2 as $stat => $v) {
              if (!isset($ret[$kw]) || !$ret[$kw]) {
                $ret[$kw] = array(
                  'term' => $kw,
                  $aid . '_' . $stat => $v,
                );
              }
              else {
                $ret[$kw][$aid . '_' . $stat] = $v;
              }
            }
          }
        }
      }
    }

    // calculate total count
    $source_count = count($sources);
    $srch_max = 0;
    $cmptn_max = 0;
    $bd_max = 0;
    foreach ($ret as $kw => $v) {
      $srch = 0;
      $cmptn = 0;
      $bd = 0;
      $source_cmptn_entries = 0;
      $source_bd_entries = 0;
      $source_count = 0;
      foreach ($sources as $aid => $s) {
        if (isset($v[$aid . '_searches'])) {
          $srch += $s['searches_ratio'] * $v[$aid . '_searches'];
        }
        else {
          continue;
        }
        $source_count++;
        if (isset($v[$aid . '_competition'])) {
          $source_cmptn_entries++;
          if ($v[$aid . '_competition'] < 0) {
            $cmptn += 0;
          }
          elseif ($v[$aid . '_competition'] > 100) {
            $cmptn += 100;
          }
          else {
            $cmptn += $v[$aid . '_competition'];
          }
        }
        if (isset($v[$aid . '_bid'])) {
          $source_bd_entries++;
          if ($v[$aid . '_bid'] < 0) {
            $bd += 0;
          }
          elseif ($v[$aid . '_bid'] > 100) {
            $bd += 100;
          }
          else {
            $bd += $v[$aid . '_bid'];
          }
        }
      }
      $ret[$kw]['_searches'] = $srch / $source_count;
      if ($source_cmptn_entries > 0) {
        $ret[$kw]['_competition'] = $cmptn / $source_cmptn_entries;
      }
      if ($source_bd_entries > 0) {
        $ret[$kw]['_bid'] = $bd / $source_bd_entries;
      }
      if (isset($ret[$kw]['_searches']) && $ret[$kw]['_searches'] > $srch_max) {
        $srch_max = $ret[$kw]['_searches'];
      }
      if (isset($ret[$kw]['_competition']) && $ret[$kw]['_competition'] > $cmptn_max) {
        $cmptn_max = $ret[$kw]['_competition'];
      }
      if (isset($ret[$kw]['_bid']) && $ret[$kw]['_bid'] > $bd_max) {
        $bd_max = $ret[$kw]['_bid'];
      }
    }
    uasort($ret, 'kwresearch_count_sort');
  }
  $ret = array_slice($ret, 0, variable_get('kwresearch_stats_report_item_limit', KWARESEARCH_STATS_REPORT_ITEM_LIMIT_DEFAULT));
  $ret = kwresearch_save_keyword_record_stats($ret, $params);
  $ret['_meta'] = array(
    'searches_max' => $srch_max,
    'competition_max' => isset($cmptn_max) ? $cmptn_max : 0,
    'bid_max' => isset($bd_max) ? $bd_max : 0,
  );
  return $ret;
}