Class: WebAnalyticsDiscovery::Alexa

Inherits:
Object
  • Object
show all
Includes:
GrabberUtils
Defined in:
lib/web_analytics_discovery/grabber/alexa.rb

Constant Summary

Constants included from GrabberUtils

GrabberUtils::AVG_DAYS_IN_MONTH, GrabberUtils::CACHE_DIR, GrabberUtils::USER_AGENT

Instance Method Summary collapse

Methods included from GrabberUtils

#download, #download_file, #mangle_url

Instance Method Details

#grab_certified_metrics(doc) ⇒ Object



26
27
28
29
30
31
# File 'lib/web_analytics_discovery/grabber/alexa.rb', line 26

def grab_certified_metrics(doc)
  r = []
  doc.gsub(/<strong class="metrics-data">([0-9,]+)<\/strong>/) { r << $1 }
  r.map! { |x| x.gsub(/,/, '').to_i }
  return r
end

#run(url) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/web_analytics_discovery/grabber/alexa.rb', line 10

def run(url)
  uri = URI.parse(url)
  host = uri.host
  r = {}
  doc = download("http://www.alexa.com/siteinfo/#{host}#trafficstats")

  # Try to extract certified metrics
  r[:visitors_day], r[:pv_day], r[:visitors_mon], r[:pv_mon] = grab_certified_metrics(doc)

  # Grab ID for clarity's sake
  if doc =~ /<img src="http:\/\/traffic\.alexa\.com\/graph\?.*&u=([^"]+)">/
    r[:id] = $1
  end
  return r
end