Background:I've noticed Google does a nice job of highlighting your keywords on Googles cached 'HTML' version of indexed PDF documents eg.
tutorial pdfThis could performed by checking the
Referer HTTP Headereg.
<%= @request.env["HTTP_REFERER"] %> in Ruby on Rails
$_SERVER["HTTP_REFERER"] in PHP
Problem Description:I want to get a handle on any Google search query that a user has performed to visit a page on my site so that I can customize content specifically to their interest
eg.
A user searches on 'streetball blog' on Google
eg.
http://www.google.com/search?q=streetball+blogThey click on "Jason's Streetball Blog" - a page I administer
I want to customize this page based on their referring keywords.
PHP/Ruby on Rails Problem Solution (ie. getting a handle on referring keywords):PHP:
$ref = $_SERVER["HTTP_REFERER"]
if (strstr($ref, "google.com/search?")) {
$search_query = "";
$query_arg = strtok($ref, "&");
while ($query_arg !== false) {
if (strstr($query_arg, "q=")) {
$search_query = substr($query_arg, 2);
break;
}
$query_arg = strtok("&");
}
echo $search_query;
}
Ruby on Rails:
# 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&amp;amp;amp;amp;amp;amp;amp;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']
]
search_referers.each do |search_engine|
# Check if the referrer is a search engine we are targetting
pattern = search_engine[0]
reg = Regexp.new(pattern)
if (reg.match(referer))
# Create a globally scoped variable (@referring_search) containing the referring Search Engine Query
query_args = URI.split(referer)[7]
unless query_args.nil_or_empty?
query_param_name = search_engine[1]
query_args.split("&").each do |arg|
if arg.split("=")[0] == query_param_name
unstopped_keywords = CGI.unescape(arg.split("=")[1])
stop_words = Regexp.new(/\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}")
break
end
end
end
break
end
end
end
end