Get Referring Search Engine Keywords from HTTP_REFERER in Ruby on Rails
Sometimes you may want to customize a page based on a referring search query for the purpose of Adsense optimization, keyword highlighting or custom logging.
A good example of this in action is Google's highlighting of search terms in cached pages and HTML versions of indexed PDF documents eg.

A good example of this in action is Google's highlighting of search terms in cached pages and HTML versions of indexed PDF documents eg.

Rails application.rb function:
# Creates @referring_search containing any referring search engine query minus stop words
#
# eg. If the HTTP_REFERER header indicates page referer as:
# http://www.google.com/search?q=Most+Calories+iN+a+Cheesesteak+at+Belmont&start=0&ie=utf-8&oe=utf-8&client=firefox-a&rls=org.mozilla:en-US:official
#
# then this function will create:
# @referring_search = "Most Calories Cheesesteak Belmont"
#
def setup_referring_keywords
# Check whether referring URL was a search engine result
referer = @request.env["HTTP_REFERER"]
unless referer.nil_or_empty?
search_referers = [
[/^http:\/\/(www\.)?google.*/, 'q'],
[/^http:\/\/search\.yahoo.*/, 'p'],
[/^http:\/\/search\.msn.*/, 'q'],
[/^http:\/\/search\.aol.*/, 'userQuery'],
[/^http:\/\/(www\.)?altavista.*/, 'q'],
[/^http:\/\/(www\.)?feedster.*/, 'q'],
[/^http:\/\/search\.lycos.*/, 'query'],
[/^http:\/\/(www\.)?alltheweb.*/, 'q']
]
query_args =
begin
URI.split(referer)[7]
rescue URI::InvalidURIError
nil
end
search_referers.each do |reg, query_param_name|
# Check if the referrer is a search engine we are targetting
if (reg.match(referer))
# Highlight the Search Term Keywords on the page
#@javascripts.push('keyword_highlighter')
# Create a globally scoped variable (@referring_search) containing the referring Search Engine Query
unless query_args.nil_or_empty?
query_args.split("&").each do |arg|
pieces = arg.split('=')
if pieces.length == 2 && pieces.first == query_param_name
unstopped_keywords = CGI.unescape(pieces.last)
stop_words = /\b(\d+|\w|about|after|also|an|and|are|as|at|be|because|before|between|but|by|can|com|de|do|en|for|from|has|how|however|htm|html|if|i|in|into|is|it|la|no|of|on|or|other|out|since|site|such|than|that|the|there|these|this|those|to|under|upon|vs|was|what|when|where|whether|which|who|will|with|within|without|www|you|your)\b/i
@referring_search = unstopped_keywords.gsub(stop_words, '').squeeze(' ')
logger.info("Referring Search Keywords: #{@referring_search}")
return true
end
end
end
return true
end
end
end
true
end
Labels: http_referer, keyword highlight, request.env["HTTP_REFERER"]


0 Comments:
Post a Comment
<< Home