def extract_from_server_meta(self, meta): ''' Will extract information for the "ip_address", "user_agent" and "locale" properties from the given WSGI REQUEST META variable or equivalent. ''' if 'REMOTE_ADDR' in meta and meta['REMOTE_ADDR']: ip = None for key in ('HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR'): if key in meta and not ip: ips = meta.get(key, '').split(',') ip = ips[-1].strip() if not utils.is_valid_ip(ip): ip = '' if utils.is_private_ip(ip): ip = '' if ip: self.ip_address = ip if 'HTTP_USER_AGENT' in meta and meta['HTTP_USER_AGENT']: self.user_agent = meta['HTTP_USER_AGENT'] if 'HTTP_ACCEPT_LANGUAGE' in meta and meta['HTTP_ACCEPT_LANGUAGE']: user_locals = [] matched_locales = utils.validate_locale(meta['HTTP_ACCEPT_LANGUAGE']) if matched_locales: lang_lst = map((lambda x: x.replace('-', '_')), (i[1] for i in matched_locales)) quality_lst = map((lambda x: x and x or 1), (float(i[4] and i[4] or '0') for i in matched_locales)) lang_quality_map = map((lambda x, y: (x, y)), lang_lst, quality_lst) user_locals = [x[0] for x in sorted(lang_quality_map, key=itemgetter(1), reverse=True)] if user_locals: self.locale = user_locals[0] return self
def extract_from_server_meta(self, meta): ''' Will extract information for the "ip_address", "user_agent" and "locale" properties from the given WSGI REQUEST META variable or equivalent. ''' if 'REMOTE_ADDR' in meta and meta['REMOTE_ADDR']: ip = None for key in ('HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR'): if key in meta and not ip: ips = meta.get(key, '').split(',') ip = ips[len(ips) - 1].strip() if not utils.is_valid_ip(ip): ip = '' if utils.is_private_ip(ip): ip = '' if ip: self.ip_address = ip if 'HTTP_USER_AGENT' in meta and meta['HTTP_USER_AGENT']: self.user_agent = meta['HTTP_USER_AGENT'] if 'HTTP_ACCEPT_LANGUAGE' in meta and meta['HTTP_ACCEPT_LANGUAGE']: user_locals = [] matched_locales = utils.validate_locale(meta['HTTP_ACCEPT_LANGUAGE']) if matched_locales: lang_lst = map((lambda x: x.replace('-', '_')), (i[1] for i in matched_locales)) quality_lst = map((lambda x: x and x or 1), (float(i[4] and i[4] or '0') for i in matched_locales)) lang_quality_map = map((lambda x, y: (x, y)), lang_lst, quality_lst) user_locals = [x[0] for x in sorted(lang_quality_map, key=itemgetter(1), reverse=True)] if user_locals: self.locale = user_locals[0] return self
def log_traffic(request): if not google_analytics: return url = urlparse(request.base_url) pyga_tracker = Tracker(google_analytics, url.hostname) pyga_visitor = Visitor() pyga_visitor.ip_address = request.access_route[0] pyga_visitor.user_agent = request.headers.get('User-Agent') user_locals = [] if 'Accept-Language' in request.headers: al = request.headers.get('Accept-Language') if al is not None: matched_locales = utils.validate_locale(al) if matched_locales: lang_lst = map((lambda x: x.replace('-', '_')), (i[1] for i in matched_locales)) quality_lst = map( (lambda x: x and x or 1), (float(i[4] and i[4] or '0') for i in matched_locales)) lang_quality_map = map((lambda x, y: (x, y)), lang_lst, quality_lst) user_locals = [ x[0] for x in sorted( lang_quality_map, key=itemgetter(1), reverse=True) ] if user_locals: pyga_visitor.locale = user_locals[0] pyga_session = Session() pyga_page = Page(url.path) pyga_page.referrer = request.headers.get('Referer') logger.info('Logging GA traffic from %s to host %s with page %s', pyga_visitor.ip_address, url.hostname, url.path) try: pyga_tracker.track_pageview(pyga_page, pyga_session, pyga_visitor) except URLError: logger.warn('Unable to connect to analytics') except: logger.error('Analytics logging failed') logger.error(sys.exc_info())