import urlparse
import cgi
import re
class SearchReferrerMiddleware(object):
SEARCH_PARAMS = {
'AltaVista': 'q',
'Ask': 'q',
'Google': 'q',
'Live': 'q',
'Lycos': 'query',
'MSN': 'q',
'Yahoo': 'p',
}
NETWORK_RE = r"""^
(?P<subdomain>[-.a-z\d]+\.)?
(?P<engine>%s)
(?P<top_level>(?:\.[a-z]{2,3}){1,2})
(?P<port>:\d+)?
$(?ix)"""
@classmethod
def parse_search(cls, url):
"""
Extract the search engine, domain, and search term from `url`
and return them as (engine, domain, term). For example,
('Google', 'www.google.co.uk', 'django framework'). Note that
the search term will be converted to lowercase and have normalized
spaces.
The first tuple item will be None if the referrer is not a
search engine.
"""
try:
parsed = urlparse.urlsplit(url)
network = parsed[1]
query = parsed[3]
except (AttributeError, IndexError):
return (None, None, None)
for engine, param in cls.SEARCH_PARAMS.iteritems():
match = re.match(cls.NETWORK_RE % engine, network)
if match and match.group(2):
term = cgi.parse_qs(query).get(param)
if term and term[0]:
term = ' '.join(term[0].split()).lower()
return (engine, network, term)
return (None, network, None)
# Here's where your code goes!
# It can be any middleware method that needs search engine detection
# functionality... this is just my example.
def process_view(self, request, view_func, view_args, view_kwargs):
from django.views.generic.date_based import object_detail
referrer = request.META.get('HTTP_REFERER')
engine, domain, term = self.parse_search(referrer)
if engine and view_func is object_detail:
# The client got to this object's page from a search engine.
# This might be useful for determining the object's popularity.
# Get the object using object_detail's queryset.
# Log this search using a custom Visit model or something.
Comments
On line 42 replace: match = re.match(NETWORK_RE % engine, network)
with: match = re.match(cls.NETWORK_RE % engine, network)
#
@zenx: Fixed, thanks!
#
Thanks, this worked nicely.
#
We've been using this and after a very long and painful bug hunt have discovered there are couple of tweaks you might want to make.
You'll probably want to edit line 44 to make it "parse_qs(unicode(query))" and line 46 to make it "u' '.join" otherwise you may spend a while trying to work out why your template is throwing a DjangoUnicodeError!
#
How to use this code ? And I need to google referrer code.
#
Yes this is a great piece of code thank for that. norgesautomaten
#
I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks [HTML_REMOVED]here[HTML_REMOVED]
#
I would like to thank you for your best written post, its informative and your writing style helped me to read it till end. Thanks man! [HTML_REMOVED]here[HTML_REMOVED]
#
I like the way you explained the difficult topic with such details. This is something I have been exploring about for a long time and you really provided all details of this topic. http://www.macrepairsmanchester.co.uk/
#
I am interested in this subject matter and would like to explore out some more information as my colleague need information on this topic. Do you have any other post on this? Cheers! [HTML_REMOVED]macbook repair manchester[HTML_REMOVED]
#
I added this website entry today for the reason that i'm trying to learn around I'm able to coping with this market. After reading through this online document I rapidly discovered how you presented the specifics the following is strictly things i was searching for today. You folks accomplish a remarkable job at generally posting information in regards to this particular subject. http://greenavis.com/
#
I love every publish within this blog. A real nice work has been doing. http://sleepcosy.com/
#
we have nuemerous users from other search engines. The page you have provided was a good read for me. We can find spammers and detect the tress passers. Thank you for sharing useful information keep sharing such updates. [HTML_REMOVED]Mens sex health[HTML_REMOVED]
#