Exemplo n.º 1
0
    def test_build_ga_params_for_direct_referals(self):
        headers = {'HTTP_HOST': 'localhost:8000'}
        request = self.make_fake_request('/somewhere/', headers)
        ga_dict_without_referal = build_ga_params(
            request, 'ua-test-id', '/some/path/',)
        ga_dict_without_direct_referal = build_ga_params(
            request, 'ua-test-id', '/some/path/',
            referer='http://test.com/some/path/')

        ga_dict_with_direct_referal = build_ga_params(
            request, 'ua-test-id', '/some/path/',
            referer='http://localhost:8000/some/path/')

        # None: if referal is not set
        self.assertEqual(
            parse_qs(ga_dict_without_referal.get('utm_url')).get('dr'), None)
        # Include referals from another host
        self.assertEqual(
            parse_qs(
                ga_dict_without_direct_referal.get('utm_url')).get('dr'),
            ['http://test.com/some/path/'])
        # Exlcude referals from the same host
        self.assertEqual(
            parse_qs(
                ga_dict_with_direct_referal.get('utm_url')).get('dr'),
            ['/some/path/'])
Exemplo n.º 2
0
    def submit_tracking(self, account, request, response, custom_params={}):
        try:
            title = BeautifulSoup(
                response.content, "html.parser"
            ).html.head.title.text.encode('utf-8')
        except:
            title = None

        path = request.get_full_path()
        referer = request.META.get('HTTP_REFERER', '')
        params = build_ga_params(
            request, account, path=path, referer=referer, title=title)
        response = set_cookie(params, response)

        # send user unique id after cookie's been set
        if hasattr(request, 'user') and hasattr(request.user, 'profile')\
                and request.user.profile.uuid:
            uuid = request.user.profile.uuid
            params = build_ga_params(
                request, account, path=path,
                referer=referer, title=title,
                user_id=uuid, custom_params=custom_params)

        send_ga_tracking.delay(params)
        return response
Exemplo n.º 3
0
    def test_build_ga_params_for_user_id(self):
        request = self.make_fake_request('/somewhere/')

        ga_dict_without_uid = build_ga_params(
            request, 'ua-test-id', '/some/path/',)

        ga_dict_with_uid = build_ga_params(
            request, 'ua-test-id', '/some/path/', user_id='402-3a6')

        self.assertEqual(
            parse_qs(ga_dict_without_uid.get('utm_url')).get('uid'), None)
        self.assertEqual(
            parse_qs(ga_dict_with_uid.get('utm_url')).get('uid'), ['402-3a6'])
Exemplo n.º 4
0
    def test_build_ga_params_for_custom_params(self):
        request = self.make_fake_request('/somewhere/')

        ga_dict_without_custom = build_ga_params(
            request, 'ua-test-id', '/some/path/',)

        ga_dict_with_custom = build_ga_params(
            request, 'ua-test-id', '/some/path/',
            custom_params={'key': 'value'})

        self.assertEqual(
            parse_qs(ga_dict_without_custom.get('utm_url')).get('key'), None)
        self.assertEqual(
            parse_qs(ga_dict_with_custom.get('utm_url')).get('key'), ['value'])
Exemplo n.º 5
0
    def process_response(self, request, response):
        if hasattr(settings, 'GOOGLE_ANALYTICS_IGNORE_PATH'):
            exclude = [p for p in settings.GOOGLE_ANALYTICS_IGNORE_PATH
                       if request.path.startswith(p)]
            if any(exclude):
                return response

        # get the account id
        try:
            account = settings.GOOGLE_ANALYTICS['google_analytics_id']
        except:
            raise Exception("No Google Analytics ID configured")

        try:
            title = BeautifulSoup(
                response.content, "html.parser").html.head.title.text
        except:
            title = None

        path = request.path
        referer = request.META.get('HTTP_REFERER', '')
        params = build_ga_params(
            request, account, path=path, referer=referer, title=title)
        response = set_cookie(params, response)
        send_ga_tracking.delay(params)
        return response
Exemplo n.º 6
0
def google_analytics_request(request, response, path=None, event=None):
    # get the account id
    account = request.GET.get('tracking_code')

    params = build_ga_params(request, account, event=event)

    set_cookie(params, response)

    utm_url = params.get('utm_url')
    user_agent = params.get('user_agent')
    language = params.get('language')

    # send the request
    resp = requests.get(utm_url,
                        headers={
                            'User-Agent': user_agent,
                            'Accepts-Language': language
                        })
    # send debug headers if debug mode is set
    if request.GET.get('utmdebug', False):
        response['X-GA-MOBILE-URL'] = utm_url
        response['X-GA-RESPONSE'] = resp

    # return the augmented response
    return response
Exemplo n.º 7
0
def google_analytics_request(request, response, path=None, event=None):
    # get the account id
    account = request.GET.get('tracking_code')

    params = build_ga_params(request, account, event=event)

    set_cookie(params, response)

    utm_url = params.get('utm_url')
    user_agent = params.get('user_agent')
    language = params.get('language')

    # send the request
    resp = requests.get(
        utm_url,
        headers={
            'User-Agent': user_agent,
            'Accepts-Language': language
        }
    )
    # send debug headers if debug mode is set
    if request.GET.get('utmdebug', False):
        response['X-GA-MOBILE-URL'] = utm_url
        response['X-GA-RESPONSE'] = resp

    # return the augmented response
    return response
Exemplo n.º 8
0
    def test_build_ga_params_for_title_encoding(self):
        responses.add(responses.GET,
                      'http://www.google-analytics.com/collect',
                      body='',
                      status=200)

        headers = {
            'HTTP_X_IORG_FBS_UIP': '100.100.200.10',
            'HTTP_X_DCMGUID': '0000-0000-0000-0000'
        }
        request = self.make_fake_request(
            '/sections/deep-soul/ما-مدى-جاهزيتك-للإنترنت/', headers)

        middleware = GoogleAnalyticsMiddleware()
        html = "<html><head><title>title</title></head></html>"
        response = middleware.process_response(request, HttpResponse(html))

        ga_dict = build_ga_params(request,
                                  response,
                                  'ua-test-id',
                                  '/some/path/',
                                  referer='/some/path/',
                                  title='ما-مدى-جاهزيتك-للإنترنت')
        self.assertEqual(
            parse_qs(ga_dict.get('utm_url')).get('dt'), [
                '%D9%85%D8%A7-%D9%85%D8%AF%D9%89-%D8%AC%D8%A7%D9%87%D8%B2%D9%8A%D8'
                '%AA%D9%83-%D9%84%D9%84%D8%A5%D9%86%D8%AA%D8%B1%D9%86%D8%AA'
            ])
Exemplo n.º 9
0
    def process_response(self, request, response):
        if hasattr(settings, 'GOOGLE_ANALYTICS_IGNORE_PATH'):
            exclude = [p for p in settings.GOOGLE_ANALYTICS_IGNORE_PATH
                       if request.path.startswith(p)]
            if any(exclude):
                return response

        # get the account id
        try:
            account = settings.GOOGLE_ANALYTICS['google_analytics_id']
        except (KeyError, TypeError):
            raise Exception("No Google Analytics ID configured")

        try:
            title = BeautifulSoup(
                response.content, "html.parser").html.head.title.text
        except AttributeError:
            title = None

        path = request.path
        referer = request.META.get('HTTP_REFERER', '')
        params = build_ga_params(
            request, account, path=path, referer=referer, title=title)
        response = set_cookie(params, response)
        send_ga_tracking.delay(params)
        return response
Exemplo n.º 10
0
def google_analytics_request(request, response, path=None, event=None):
    params = build_ga_params(request, event=event)

    set_cookie(params, response)

    utm_url = params.get('utm_url')
    user_agent = params.get('user_agent')
    language = params.get('language')

    # send the request
    http = httplib2.Http()
    try:
        resp, content = http.request(
            utm_url, 'GET',
            headers={
                'User-Agent': user_agent,
                'Accept-Language:': language
            }
        )
        # send debug headers if debug mode is set
        if request.GET.get('utmdebug', False):
            response['X-GA-MOBILE-URL'] = utm_url
            response['X-GA-RESPONSE'] = resp

        # return the augmented response
        return response
    except httplib2.HttpLib2Error:
        raise Exception("Error opening: %s" % utm_url)
Exemplo n.º 11
0
def google_analytics_request(request, response, path=None, event=None):
    params = build_ga_params(request, event=event)

    set_cookie(params, response)

    utm_url = params.get('utm_url')
    user_agent = params.get('user_agent')
    language = params.get('language')

    # send the request
    http = httplib2.Http()
    try:
        resp, content = http.request(utm_url,
                                     'GET',
                                     headers={
                                         'User-Agent': user_agent,
                                         'Accepts-Language:': language
                                     })
        # send debug headers if debug mode is set
        if request.GET.get('utmdebug', False):
            response['X-GA-MOBILE-URL'] = utm_url
            response['X-GA-RESPONSE'] = resp

        # return the augmented response
        return response
    except httplib2.HttpLib2Error:
        raise Exception("Error opening: %s" % utm_url)
Exemplo n.º 12
0
    def test_build_ga_params_for_campaign_tracking_params(self):
        '''
        Test that the  correct GA campaign
        tracking params are tracked correctly
        '''
        request = self.make_fake_request(
            '/somewhere/?utm_campaign=campaign name&utm_term=campaign keyword')
        ga_dict_with_campaign_params = build_ga_params(
            request, 'ua-test-id', '/compaign/path/')
        self.assertEqual(
            parse_qs(ga_dict_with_campaign_params.get(
                'utm_url')).get('cn'), ['campaign name'])
        self.assertEqual(
            parse_qs(ga_dict_with_campaign_params.get(
                'utm_url')).get('ck'), ['campaign keyword'])

        # params that aren't in the request should be excluded from the utm_url
        self.assertEqual(
            parse_qs(
                ga_dict_with_campaign_params.get(
                    'utm_url')).get('cs'), None)
        self.assertEqual(
            parse_qs(
                ga_dict_with_campaign_params.get(
                    'utm_url')).get('cm'), None)
Exemplo n.º 13
0
    def test_build_ga_params_for_custom_params(self):
        request = self.make_fake_request('/somewhere/')

        ga_dict_without_custom = build_ga_params(
            request,
            'ua-test-id',
            '/some/path/',
        )

        ga_dict_with_custom = build_ga_params(request,
                                              'ua-test-id',
                                              '/some/path/',
                                              custom_params={'key': 'value'})

        self.assertEqual(
            parse_qs(ga_dict_without_custom.get('utm_url')).get('key'), None)
        self.assertEqual(
            parse_qs(ga_dict_with_custom.get('utm_url')).get('key'), ['value'])
Exemplo n.º 14
0
 def test_ga_logging_enabled(self):
     request = self.make_fake_request('/somewhere/')
     ga_with_logging = build_ga_params(
         request,
         'ua-test-id',
         '/some/path/',
         user_id='402-3a6',
     )
     self.assertEqual(True, ga_with_logging['ga_logging_enabled'])
Exemplo n.º 15
0
    def test_build_ga_params_for_user_id(self):
        request = self.make_fake_request('/somewhere/')

        ga_dict_without_uid = build_ga_params(
            request,
            'ua-test-id',
            '/some/path/',
        )

        ga_dict_with_uid = build_ga_params(request,
                                           'ua-test-id',
                                           '/some/path/',
                                           user_id='402-3a6')

        self.assertEqual(
            parse_qs(ga_dict_without_uid.get('utm_url')).get('uid'), None)
        self.assertEqual(
            parse_qs(ga_dict_with_uid.get('utm_url')).get('uid'), ['402-3a6'])
Exemplo n.º 16
0
 def test_ga_logging_enabled(self):
     request = self.make_fake_request('/somewhere/')
     ga_with_logging = build_ga_params(
         request,
         'ua-test-id',
         '/some/path/',
         user_id='402-3a6',
     )
     self.assertEqual(True, ga_with_logging['ga_logging_enabled'])
Exemplo n.º 17
0
    def submit_tracking(self, account, request, response):
        try:
            title = BeautifulSoup(
                response.content,
                "html.parser").html.head.title.text.encode('utf-8')
        except Exception:
            title = None

        path = request.get_full_path()
        referer = request.META.get('HTTP_REFERER', '')
        params = build_ga_params(request,
                                 account,
                                 path=path,
                                 referer=referer,
                                 title=title)
        response = set_cookie(params, response)

        def calculate_age(dob):
            today = get_today()
            return (today.year - dob.year - ((today.month, today.day) <
                                             (dob.month, dob.day)))

        # send user unique id and details after cookie's been set
        if hasattr(request, 'user') and hasattr(request.user, 'profile'):
            profile = request.user.profile

            custom_params = {}
            if profile.gender:
                gender_key = settings.GOOGLE_ANALYTICS_GENDER_KEY
                custom_params[gender_key] = profile.gender
            if profile.date_of_birth:
                age_key = settings.GOOGLE_ANALYTICS_AGE_KEY
                custom_params[age_key] = calculate_age(profile.date_of_birth)

            params = build_ga_params(request,
                                     account,
                                     path=path,
                                     referer=referer,
                                     title=title,
                                     custom_params=custom_params)

        send_ga_tracking.delay(params)
        return response
Exemplo n.º 18
0
    def process_response(self, request, response):
        if hasattr(settings, 'GOOGLE_ANALYTICS_IGNORE_PATH'):
            exclude = [p for p in settings.GOOGLE_ANALYTICS_IGNORE_PATH\
                        if request.path.startswith(p)]
            if any(exclude):
                return response

        path = request.path
        referer = request.META.get('HTTP_REFERER', '')
        params = build_ga_params(request, path=path, referer=referer)
        response = set_cookie(params, response)
        send_ga_tracking.delay(params)
        return response
Exemplo n.º 19
0
def google_analytics_request(request, response, path=None, event=None):
    params = build_ga_params(request, event=event)

    set_cookie(params, response)

    utm_url = params.get("utm_url")
    user_agent = params.get("user_agent")
    language = params.get("language")

    # send the request
    http = httplib2.Http()
    try:
        resp, content = http.request(utm_url, "GET", headers={"User-Agent": user_agent, "Accepts-Language:": language})
        # send debug headers if debug mode is set
        if request.GET.get("utmdebug", False):
            response["X-GA-MOBILE-URL"] = utm_url
            response["X-GA-RESPONSE"] = resp

        # return the augmented response
        return response
    except httplib2.HttpLib2Error:
        raise Exception("Error opening: %s" % utm_url)
Exemplo n.º 20
0
    def test_build_ga_params_for_title_encoding(self):
        responses.add(
            responses.GET, 'http://www.google-analytics.com/collect',
            body='',
            status=200)

        headers = {
            'HTTP_X_IORG_FBS_UIP': '100.100.200.10',
            'HTTP_X_DCMGUID': '0000-0000-0000-0000'}
        request = self.make_fake_request(
            '/sections/deep-soul/ما-مدى-جاهزيتك-للإنترنت/', headers)

        html = "<html><head><title>title</title></head></html>"
        middleware = GoogleAnalyticsMiddleware(lambda req: HttpResponse(html))
        response = middleware(request)

        ga_dict = build_ga_params(
            request, response, 'ua-test-id', '/some/path/',
            referer='/some/path/', title='ما-مدى-جاهزيتك-للإنترنت')
        self.assertEqual(parse_qs(ga_dict.get('utm_url')).get('dt'), [
            '%D9%85%D8%A7-%D9%85%D8%AF%D9%89-%D8%AC%D8%A7%D9%87%D8%B2%D9%8A%D8'
            '%AA%D9%83-%D9%84%D9%84%D8%A5%D9%86%D8%AA%D8%B1%D9%86%D8%AA'])
Exemplo n.º 21
0
    def test_build_ga_params_for_campaign_tracking_params(self):
        '''
        Test that the  correct GA campaign
        tracking params are tracked correctly
        '''
        request = self.make_fake_request(
            '/somewhere/?utm_campaign=campaign name&utm_term=campaign keyword')
        ga_dict_with_campaign_params = build_ga_params(request, 'ua-test-id',
                                                       '/compaign/path/')
        self.assertEqual(
            parse_qs(ga_dict_with_campaign_params.get('utm_url')).get('cn'),
            ['campaign name'])
        self.assertEqual(
            parse_qs(ga_dict_with_campaign_params.get('utm_url')).get('ck'),
            ['campaign keyword'])

        # params that aren't in the request should be excluded from the utm_url
        self.assertEqual(
            parse_qs(ga_dict_with_campaign_params.get('utm_url')).get('cs'),
            None)
        self.assertEqual(
            parse_qs(ga_dict_with_campaign_params.get('utm_url')).get('cm'),
            None)