You are here

stats.inc in Keyword Research 6

Same filename and directory in other branches
  1. 7 includes/stats.inc

Functions for generating data for keyword stats reports. Implements a kwresearch_sources API.

File

includes/stats.inc
View source
<?php

/**
 * @file 
 * Functions for generating data for keyword stats reports. 
 * Implements a kwresearch_sources API.
 */

/**
 * Generates analysis data for keyword stats report. Data is generated by
 * 
 * @param string|array $keywords - list of keywords for which to generate data
 * @param array $msgs - used to return error messages
 * @param array $params - extra parmaters
 */
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 (!$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;
    $comptn_max = 0;
    $bd_max = 0;
    foreach ($ret as $kw => $v) {
      $srch = 0;
      $cmptn = 0;
      $bd = 0;
      $source_cmptn_entries = 0;
      $source_bd_entries = 0;
      foreach ($sources as $aid => $s) {
        $srch += $s['searches_ratio'] * $v[$aid . '_searches'];
        if (!is_null($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 (!is_null($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 ($ret[$kw]['_searches'] > $srch_max) {
        $srch_max = $ret[$kw]['_searches'];
      }
      if ($ret[$kw]['_competition'] > $cmptn_max) {
        $cmptn_max = $ret[$kw]['_competition'];
      }
      if ($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' => $cmptn_max,
    'bid_max' => $bd_max,
  );
  return $ret;
}

/**
 * Saves keyword stats data to site keywords table
 * @param $keywords_data
 * @param $params
 */
function kwresearch_save_keyword_record_stats($keywords_data, $params) {
  if (is_array($keywords_data)) {
    foreach ($keywords_data as $k => $data) {
      if (substr($k, 0, 1) == '_') {
        continue;
      }
      $kw_obj = kwresearch_load_site_keyword($data['term']);
      if (!$kw_obj) {
        $kid = kwresearch_save_site_keyword($data['term']);
        $kw_obj = kwresearch_load_site_keyword($kid);
      }
      else {
        $kid = $kw_obj->kid;
      }
      $keywords_data[$k]['kid'] = (int) $kw_obj->kid;
      $keywords_data[$k]['priority'] = (int) $kw_obj->priority;
      $keywords_data[$k]['value'] = (double) $kw_obj->value;
      $keywords_data[$k]['page_count'] = (int) $kw_obj->page_count;
      $sql = '
        UPDATE {kwresearch_keyword}
        SET
          stats_update = %d,
          daily_volume = %d,
          competition = %d,
          bid = %f
        WHERE kid = "%d"
      ';
      db_query($sql, time(), $data['_searches'], $data['_competition'], $data['_bid'], $kid);
    }
  }
  return $keywords_data;
}

Functions

Namesort descending Description
kwresearch_get_keyword_stats_data Generates analysis data for keyword stats report. Data is generated by
kwresearch_save_keyword_record_stats Saves keyword stats data to site keywords table