Beispiel #1
0
 def track_pageview(self, path, **kwargs):
     """
     @variable str page
     @variable dict kwargs (optional) {'title', 'charset', 'referrer', 'load_time'}
     Sends page hit to GA with given path as the page
     """
     page = Page(path)
     page.title = kwargs.get('title', None)
     page.charset = kwargs.get('charset', None)
     page.referrer = kwargs.get('referrer', None)
     page.load_time = kwargs.get('load_time', None)
     self.ga_tracker.track_pageview(page=page,session=self.ga_session,visitor=self.ga_visitor)
Beispiel #2
0
def ping():
    """ping back to ga"""
    if not ga_account:
        return

    tracker = Tracker(ga_account, ga_domain)
    visitor = Visitor()
    visitor.ip_address = request.remote_addr
    session = Session()
    page = Page(request.path)
    if 'referer' in request.headers:
        page.referrer = request.headers['referer']
    tracker.track_pageview(page, session, visitor)
Beispiel #3
0
def ping():
    """ping back to ga"""
    if not ga_account:
        return

    tracker = Tracker(ga_account, ga_domain)
    visitor = Visitor()
    visitor.ip_address = request.remote_addr
    session = Session()
    page = Page(request.path)
    if "referer" in request.headers:
        page.referrer = request.headers["referer"]
    tracker.track_pageview(page, session, visitor)
Beispiel #4
0
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())