コード例 #1
0
    def test_request(self):
        from pyga.requests import Tracker, Visitor, Session, Page
        from mock import Mock
        import urllib2

        mocked = urllib2.urlopen = Mock()

        meta = dict(
            REMOTE_ADDR='134.321.0.1',
            HTTP_USER_AGENT='Test User Agent 1.0',
            HTTP_ACCEPT_LANGUAGE='en-US,en;q=0.8,ru;q=0.6',
        )
        tracker = Tracker('UA-0000-0000', 'test.com')
        visitor = Visitor()
        visitor.extract_from_server_meta(meta)
        self.assertEqual(visitor.ip_address, '134.321.0.1')
        self.assertEqual(visitor.locale, 'en_US')
        self.assertEqual(visitor.user_agent, 'Test User Agent 1.0')
        session = Session()
        page = Page('/test_path')
        tracker.track_pageview(page, session, visitor)
        (request, ), _ = mocked.call_args_list.pop()
        self.assertEqual(request.headers.get('X-forwarded-for'), '134.321.0.1')
        self.assertEqual(request.headers.get('User-agent'),
                         'Test User Agent 1.0')
コード例 #2
0
def track_event(category=None, action=None, label=None, value=None, identifier=None, first_use=None, add=None,
                noninteraction=True):
    anonymousConfig = Config()
    anonymousConfig.anonimize_ip_address = True

    tracker = Tracker('UA-86466078-1', 'none', conf=anonymousConfig)
    visitor = Visitor()

    # convert the last 8 bytes of the machine identifier to an integer to get a "unique" user
    visitor.unique_id = struct.unpack("!I", binascii.unhexlify(identifier[32:]))[0]/2

    if add:
        # add visitor's ip address (will be anonymized)
        visitor.ip_address = add

    if first_use:
        visitor.first_visit_time = first_use

    session = Session()
    event = Event(category=category, action=action, label=label, value=value, noninteraction=noninteraction)
    path = u"/" + u"/".join([category, action, label])
    page = Page(path.lower())

    tracker.track_event(event, session, visitor)
    tracker.track_pageview(page, session, visitor)
コード例 #3
0
ファイル: ga_agent.py プロジェクト: LACMTA/sga
def alloc_ga_obj_by_name(name):
    """
    通过名字生成对应的obj
    """
    from pyga.requests import Tracker, Page, Session, Visitor, Transaction, Event, SocialInteraction, Campaign

    obj = None
    if name == 'tracker':
        obj = Tracker()
    elif name == 'campaign':
        obj = Campaign(Campaign.TYPE_REFERRAL)
    elif name == 'session':
        obj = Session()
    elif name == 'page':
        obj = Page(None)
    elif name == 'visitor':
        obj = Visitor()
    elif name == 'transaction':
        obj = Transaction()
    elif name == 'event':
        obj = Event()
    elif name == 'social_interaction':
        obj = SocialInteraction()

    return obj
コード例 #4
0
ファイル: ga.py プロジェクト: simudream/py-ga-mob
    def test_request(self):
        from pyga.requests import Tracker, Visitor, Session, Page
        from mock import Mock

        mocked = six.moves.urllib.request.urlopen = Mock()

        meta = dict(
            REMOTE_ADDR='134.321.0.1',
            HTTP_USER_AGENT='Test User Agent 1.0',
            HTTP_ACCEPT_LANGUAGE='en-US,en;q=0.8,ru;q=0.6',
        )
        tracker = Tracker('UA-0000-0000', 'test.com')
        visitor = Visitor()
        visitor.extract_from_server_meta(meta)
        self.assertEqual(visitor.ip_address, '134.321.0.1')
        self.assertEqual(visitor.locale, 'en_US')
        self.assertEqual(visitor.user_agent, 'Test User Agent 1.0')
        session = Session()
        page = Page('/test_path')
        tracker.track_pageview(page, session, visitor)
        (request, ), _ = mocked.call_args_list.pop()
        self.assertEqual(request.headers.get('X-forwarded-for'), '134.321.0.1')
        self.assertEqual(request.headers.get('User-agent'), 'Test User Agent 1.0')

        # Assert that &ua and &uip are passed along, and that &uip is properly
        # anonymized.
        qs = six.moves.urllib.parse.urlparse(request.get_full_url()).query
        params = six.moves.urllib.parse.parse_qs(qs)
        self.assertEqual(params['uip'][0], '134.321.0.0')
        self.assertEqual(params['ua'][0], 'Test User Agent 1.0')
コード例 #5
0
    def test_request(self, mocked):
        from pyga.requests import Tracker, Visitor, Session, Page

        meta = dict(
            REMOTE_ADDR="134.321.0.1",
            HTTP_USER_AGENT="Test User Agent 1.0",
            HTTP_ACCEPT_LANGUAGE="en-US,en;q=0.8,ru;q=0.6",
        )
        tracker = Tracker("UA-0000-0000", "test.com")
        visitor = Visitor()
        visitor.extract_from_server_meta(meta)
        self.assertEqual(visitor.ip_address, "134.321.0.1")
        self.assertEqual(visitor.locale, "en_US")
        self.assertEqual(visitor.user_agent, "Test User Agent 1.0")
        session = Session()
        page = Page("/test_path")
        tracker.track_pageview(page, session, visitor)
        (request, ), _ = mocked.call_args_list.pop()
        self.assertEqual(request.headers.get("X-forwarded-for"), "134.321.0.1")
        self.assertEqual(request.headers.get("User-agent"),
                         "Test User Agent 1.0")

        # Assert that &ua and &uip are passed along, and that &uip is properly
        # anonymized.
        qs = urlparse(request.get_full_url()).query
        params = parse_qs(qs)
        self.assertEqual(params["uip"][0], "134.321.0.0")
        self.assertEqual(params["ua"][0], "Test User Agent 1.0")
コード例 #6
0
ファイル: ga.py プロジェクト: serseri23/Halowrepo
def get_ga():
    from pyga.requests import Tracker, Session, Config
    with plugin.get_storage("analytics") as ga:
        config = Config()
        tracker = Tracker(TRACKING_ID, 'steeve.io', conf=config)
        session = ga.setdefault("session", Session())
        if not ga.get("visitor"):
            ga["visitor"] = make_visitor()
        return tracker, session, ga["visitor"]
コード例 #7
0
ファイル: ga.py プロジェクト: salvopr/adrest
 def get(self, request, path=None, **resources):
     """ Proxy request to GA. """
     tracker = Tracker(self._meta.account_id, self._meta.domain
                       or request.META.get('SERVER_NAME'))
     visitor = Visitor()
     visitor.extract_from_server_meta(request.META)
     session = Session()
     page = Page(path)
     tracker.track_pageview(page, session, visitor)
コード例 #8
0
ファイル: ga_tracking.py プロジェクト: b-dev/plata
def on_order_paid(order, payment, request, **kwargs):
    if not request:
        return

    ga_data = order.data.get("google_analytics", {})
    tracker = Tracker(settings.GOOGLE_ANALYTICS_ID, request.get_host())

    # create visitor
    visitor = Visitor()
    try:
        visitor.extract_from_utma(ga_data.get("utma", ""))
    except ValueError:
        return  # utma cookie value is invalid
    visitor.extract_from_server_meta(ga_data.get("meta", {}))

    # create session
    session = Session()
    try:
        session.extract_from_utmb(ga_data.get("utmb", ""))
    except ValueError:
        return  # utmb cookie value is invalid

    # create transaction
    transaction = Transaction()
    transaction.order_id = order.order_id
    transaction.total = order.subtotal
    transaction.tax = order.tax
    transaction.shipping = order.shipping
    transaction.city = order.billing_city.encode("utf8")
    transaction.country = ("%s" % order.billing_country).encode("utf8")
    transaction.currency = order.currency
    for item in order.items.all():
        i = Item()
        i.sku = item.sku
        i.name = item.name.encode("utf8")
        i.price = item.unit_price
        i.quantity = item.quantity
        transaction.add_item(i)

    # tracker.setcurrencyCode
    tracker.track_transaction(transaction, session, visitor)

    order.data["google_analytics"]["tracked"] = now()
    order.save()
コード例 #9
0
ファイル: ga.py プロジェクト: mnjstwins/xbmctorrent
def get_ga():
    from pyga.requests import Tracker, Session, Config

    ga = plugin.get_storage("analytics")
    config = Config()
    config.anonimize_ip_address = True
    tracker = Tracker(TRACKING_ID, 'steeve.io', conf=config)
    session = ga.setdefault("session", Session())
    visitor = ensure_screen_res(ga.setdefault("visitor", make_visitor()))
    return tracker, session, visitor
コード例 #10
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)
コード例 #11
0
ファイル: ga.py プロジェクト: rapharacing/microweb
    def process_request(self, request):
        if settings.GA_ENABLED:
            ip = request.META["REMOTE_ADDR"]
            if request.META.has_key("CF-Connecting-IP"):
                ip = request.META["CF-Connecting-IP"]

            tracker = Tracker(settings.GA_KEY, request.META["HTTP_HOST"])
            visitor = Visitor()
            visitor.ip_address = ip
            session = Session()
            page = Page(request.path)
            tracker.track_pageview(page, session, visitor)

        return None
コード例 #12
0
    def start_session(self, caller_id):
        self.caller_id = caller_id

        if self.account_id:
            self.tracker = Tracker(self.account_id, self.domain_name)

            self.visitor = Visitor()
            self.visitor.user_agent = self.caller_id
            self.visitor.unique_id = hash(caller_id) & 0x7fffffff

            #            print self.visitor.unique_id
            #            print self.visitor.__getstate__()

            self.session = Session()
コード例 #13
0
def track_event(category=None, action=None, label=None):
    if not settings.analytics.getboolean('enabled'):
        return

    anonymousConfig = Config()
    anonymousConfig.anonimize_ip_address = True

    tracker = Tracker('UA-138214134-3', 'none', conf=anonymousConfig)

    try:
        if os.path.isfile(os.path.normpath(os.path.join(args.config_dir, 'config', 'analytics.dat'))):
            with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'analytics.dat')), 'r') as handle:
                visitor_text = handle.read()
            visitor = pickle.loads(codecs.decode(visitor_text.encode(), "base64"))
            if visitor.user_agent is None:
                visitor.user_agent = os.environ.get("SZ_USER_AGENT")
            if visitor.unique_id > int(0x7fffffff):
                visitor.unique_id = random.randint(0, 0x7fffffff)
        else:
            visitor = Visitor()
            visitor.unique_id = random.randint(0, 0x7fffffff)
    except:
        visitor = Visitor()
        visitor.unique_id = random.randint(0, 0x7fffffff)

    session = Session()
    event = Event(category=category, action=action, label=label, value=1)

    tracker.add_custom_variable(CustomVariable(index=1, name='BazarrVersion',
                                               value=os.environ["BAZARR_VERSION"].lstrip('v'), scope=1))
    tracker.add_custom_variable(CustomVariable(index=2, name='PythonVersion', value=platform.python_version(), scope=1))
    if settings.general.getboolean('use_sonarr'):
        tracker.add_custom_variable(CustomVariable(index=3, name='SonarrVersion', value=sonarr_version, scope=1))
    else:
        tracker.add_custom_variable(CustomVariable(index=3, name='SonarrVersion', value='unused', scope=1))
    if settings.general.getboolean('use_radarr'):
        tracker.add_custom_variable(CustomVariable(index=4, name='RadarrVersion', value=radarr_version, scope=1))
    else:
        tracker.add_custom_variable(CustomVariable(index=4, name='RadarrVersion', value='unused', scope=1))
    tracker.add_custom_variable(CustomVariable(index=5, name='OSVersion', value=platform.platform(), scope=1))

    try:
        tracker.track_event(event, session, visitor)
    except:
        logging.debug("BAZARR unable to track event.")
        pass
    else:
        with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'analytics.dat')), 'w+') as handle:
            handle.write(codecs.encode(pickle.dumps(visitor), "base64").decode())
コード例 #14
0
ファイル: ga.py プロジェクト: AshitaSNM/myvariant.info
    def ga_track(self, event={}):
        _req_list = []
        no_tracking = self.get_argument('no_tracking', None)
        is_prod = getattr(config, 'RUN_IN_PROD', False)
        if not no_tracking and is_prod and hasattr(config, "GA_ACCOUNT"):
            _req = self.request
            remote_ip = _req.headers.get("X-Real-Ip",
                        _req.headers.get("X-Forwarded-For",
                        _req.remote_ip))
            user_agent = _req.headers.get("User-Agent", None)
            visitor = Visitor()
            visitor.ip_address = remote_ip
            visitor.user_agent = user_agent
            #get visitor.locale
            visitor.extract_from_server_meta(
                {"HTTP_ACCEPT_LANGUAGE": _req.headers.get("Accept-Language", None)}
            )
            session = Session()
            page = Page(_req.path)
            tracker = Tracker(config.GA_ACCOUNT, 'MyVariant.info')
            # tracker.track_pageview(page, session, visitor)  #this is non-async request
            pvr = PageViewRequest(config=tracker.config,
                                  tracker=tracker,
                                  visitor=visitor,
                                  session=session,
                                  page=page)
            r = pvr.build_http_request()
            _req_list.append(HTTPRequest(r.get_full_url(),
                                         "POST" if (r.data) else "GET",
                                         headers=r.headers,
                                         body=r.data))
            if event:
                evt = Event(**event)
                #tracker.track_event(evt, session, visitor)  #this is non-async request
                er = EventRequest(config=tracker.config,
                                  tracker=tracker,
                                  visitor=visitor,
                                  session=session,
                                  event=evt)
                r = er.build_http_request()
                _req_list.append(HTTPRequest(r.get_full_url(),
                                             "POST" if (r.data) else "GET",
                                             headers=r.headers,
                                             body=r.data))

            #now send actual async requests
            http_client = AsyncHTTPClient()
            for _req in _req_list:
                http_client.fetch(_req)
コード例 #15
0
ファイル: ga.py プロジェクト: desholmes/covid-19-uk-api
    def middleware(request):
        if ga_id is not False:
            tracker = Tracker(ga_id, request.headers['Host'])
            visitor = Visitor()
            visitor.ip_address = request.META.get('REMOTE_ADDR')
            if request.headers['User-Agent']:
                visitor.user_agent = request.headers['User-Agent']
            session = Session()
            path = request.path
            if request.META.get('QUERY_STRING'):
                path = f"{request.path}?{request.META.get('QUERY_STRING')}"
            page = Page(path)
            page.title = request.path
            tracker.track_pageview(page, session, visitor)

        response = get_response(request)
        return response
コード例 #16
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())
コード例 #17
0
ファイル: analytics.py プロジェクト: davideagle/bazarr
def track_event(category=None, action=None, label=None):
    if not settings.analytics.getboolean('enabled'):
        return

    anonymousConfig = Config()
    anonymousConfig.anonimize_ip_address = True

    tracker = Tracker('UA-138214134-3', 'none', conf=anonymousConfig)

    try:
        visitor = pickle.loads(base64.b64decode(settings.analytics.visitor))
    except:
        visitor = Visitor()
        unique_id = long(random.getrandbits(32))
        visitor.unique_id = unique_id

    session = Session()
    event = Event(category=category, action=action, label=label, value=1)
    path = u"/" + u"/".join([category, action, label])
    page = Page(path.lower())

    tracker.add_custom_variable(CustomVariable(index=1, name='BazarrVersion', value=os.environ["BAZARR_VERSION"], scope=1))
    tracker.add_custom_variable(CustomVariable(index=2, name='PythonVersion', value=platform.python_version(), scope=1))
    if settings.general.getboolean('use_sonarr'):
        tracker.add_custom_variable(CustomVariable(index=3, name='SonarrVersion', value=sonarr_version, scope=1))
    if settings.general.getboolean('use_radarr'):
        tracker.add_custom_variable(CustomVariable(index=4, name='RadarrVersion', value=radarr_version, scope=1))
    tracker.add_custom_variable(CustomVariable(index=5, name='OSVersion', value=platform.platform(), scope=1))

    try:
        tracker.track_event(event, session, visitor)
        tracker.track_pageview(page, session, visitor)
    except:
        pass
    else:
        settings.analytics.visitor = base64.b64encode(pickle.dumps(visitor))
        with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle:
            settings.write(handle)
コード例 #18
0
ファイル: ivi2xbmc.py プロジェクト: ypol67/seppius-xbmc-repo
    visitor = Visitor()
    visitor.user_agent=get_user_agent()
    visitor.locale = xbmc.getLanguage()
    info = lambda x: xbmc.getInfoLabel("System.%s" % x)
    visitor.screen_resolution = "%sx%s" % (info("ScreenWidth"), info("ScreenHeight"))
    visitor.unique_id=random.randint(0, 0x7fffffff)

    with open(vis_file, 'wb') as f:
        pickle.dump(visitor, f)
       
try:
    with open(ses_file, 'rb') as f:
        session = pickle.load(f)

except: 
    session = Session()
    with open(ses_file, 'wb') as f:
        pickle.dump(session, f)



genres_data = []
genres_dat_file = os.path.join(addon_path, 'genres.dat')

if os.path.isfile(genres_dat_file):
    try:
        gdf = open(genres_dat_file, 'r')
        genres_data = json.loads(gdf.read())
        gdf.close()
    except: pass
    
コード例 #19
0
ファイル: views.py プロジェクト: dragonx/django-social-auth
def complete_process(request, backend, *args, **kwargs):
    """Authentication complete process"""
    # pop redirect value before the session is trashed on login()
    redirect_value = request.session.get(REDIRECT_FIELD_NAME, '') or \
                     request.REQUEST.get(REDIRECT_FIELD_NAME, '')

    if GA_ID:
        ga_tracker = Tracker(GA_ID, request.META.get('HTTP_HOST', ""))
        utmz = request.COOKIES.get("__utmz")
        if utmz:
            try:
                campaign = Campaign()
                campaign.extract_from_utmz(utmz)
                ga_tracker.campaign = campaign
            except:
                pass
        ga_visitor = Visitor()
        try:
            ga_visitor.extract_from_utma(request.COOKIES.get("__utma",""))
        except:
            pass
        ga_visitor.ip_address = request.META.get('REMOTE_ADDR')
        ga_session = Session()
        try:
            ga_session.extract_from_utmb(request.COOKIES.get("__utmb"))
        except:
            pass
    else:
        ga_tracker = None

    # Django 1.5 allow us to define custom User Model, so integrity errors
    # can be raised.
    try:
        user = auth_complete(request, backend, *args, **kwargs)
    except IntegrityError:
        try:
            if ga_tracker:
                ga_tracker.track_event(Event("Logins",backend.AUTH_BACKEND.name+"CompleteError",
                                       request.build_absolute_uri()),
                                       ga_session, ga_visitor)
        except:
            pass
        url = setting('SIGNUP_ERROR_URL', setting('LOGIN_ERROR_URL'))
        return HttpResponseRedirect(url)

    if isinstance(user, HttpResponse):
        return user

    if not user and request.user.is_authenticated():
        return HttpResponseRedirect(redirect_value)

    msg = None
    

    if user:
        if getattr(user, 'is_active', True):
            # catch is_new flag before login() might reset the instance
            is_new = getattr(user, 'is_new', False)
            login(request, user)
            # user.social_user is the used UserSocialAuth instance defined
            # in authenticate process
            social_user = user.social_user
            if redirect_value:
                request.session[REDIRECT_FIELD_NAME] = redirect_value or \
                                                       DEFAULT_REDIRECT

            if setting('SOCIAL_AUTH_SESSION_EXPIRATION', True):
                # Set session expiration date if present and not disabled by
                # setting. Use last social-auth instance for current provider,
                # users can associate several accounts with a same provider.
                expiration = social_user.expiration_datetime()
                if expiration:
                    try:
                        request.session.set_expiry(expiration)
                    except OverflowError:
                        # Handle django time zone overflow, set default expiry.
                        request.session.set_expiry(None)

            # store last login backend name in session
            key = setting('SOCIAL_AUTH_LAST_LOGIN',
                          'social_auth_last_login_backend')
            request.session[key] = social_user.provider

            # Remove possible redirect URL from session, if this is a new
            # account, send him to the new-users-page if defined.
            new_user_redirect = backend_setting(backend,
                                           'SOCIAL_AUTH_NEW_USER_REDIRECT_URL')
            if new_user_redirect and is_new:
                url = new_user_redirect
            else:
                url = redirect_value or \
                      backend_setting(backend,
                                      'SOCIAL_AUTH_LOGIN_REDIRECT_URL') or \
                      DEFAULT_REDIRECT
            if ga_tracker:
                ga_tracker.track_event(Event("Logins",backend.AUTH_BACKEND.name+"Complete",
                                   request.build_absolute_uri()),
                                   ga_session, ga_visitor)
        else:
            msg = setting('SOCIAL_AUTH_INACTIVE_USER_MESSAGE', None)
            url = backend_setting(backend, 'SOCIAL_AUTH_INACTIVE_USER_URL',
                                  LOGIN_ERROR_URL)
    else:
        msg = setting('LOGIN_ERROR_MESSAGE', None)
        url = backend_setting(backend, 'LOGIN_ERROR_URL', LOGIN_ERROR_URL)
        if ga_tracker:
            ga_tracker.track_event(Event("Logins",backend.AUTH_BACKEND.name+"CompleteError",
                               request.build_absolute_uri()),
                               ga_session, ga_visitor)
    if msg:
        messages.error(request, msg)

    if redirect_value and redirect_value != url:
        redirect_value = quote(redirect_value)
        if '?' in url:
            url += '&%s=%s' % (REDIRECT_FIELD_NAME, redirect_value)
        else:
            url += '?%s=%s' % (REDIRECT_FIELD_NAME, redirect_value)
    return HttpResponseRedirect(url)