def test_api_pageview(self, logger, rpost, aw): def mocked_queue(function, data, headers, success_cb, failure_cb): function(data, headers, success_cb, failure_cb) aw().queue.side_effect = mocked_queue request = RequestFactory().get('/api/SomeAPI/') request.user = AnonymousUser() with self.settings(GOOGLE_ANALYTICS_ID='XYZ-123'): track_api_pageview(request, client_id='mycid') params = { 'v': 1, 't': 'pageview', 'cid': 'mycid', 'dh': 'testserver', 'tid': 'XYZ-123', 'dt': 'API (/api/SomeAPI/)', 'ds': 'api', 'dp': '/api/SomeAPI/', 'dl': 'http://testserver/api/SomeAPI/', } logger.info.assert_called_with( 'Successfully attempted to send pageview to Google ' 'Analytics (%s)', params)
def test_api_pageview(self, logger, rpost, aw): def mocked_queue(function, data, headers, success_cb, failure_cb): function(data, headers, success_cb, failure_cb) aw().queue.side_effect = mocked_queue request = RequestFactory().get('/api/SomeAPI/') request.user = AnonymousUser() with self.settings(GOOGLE_ANALYTICS_ID='XYZ-123'): track_api_pageview(request, client_id='mycid') params = { 'v': 1, 't': 'pageview', 'cid': 'mycid', 'dh': 'testserver', 'tid': 'XYZ-123', 'dt': 'API (/api/SomeAPI/)', 'ds': 'api', 'dp': '/api/SomeAPI/', 'dl': 'http://testserver/api/SomeAPI/', } logger.info.assert_called_with( 'Successfully attempted to send pageview to Google ' 'Analytics (%s)', params )
def inner(request, *args, **kwargs): response = view(request, *args, **kwargs) if response.status_code < 500: referer = request.META.get('HTTP_REFERER') if referer: # If the referer host is the same as the request host # that implies that the API was used as an AJAX request # in our main webapp itself. If so, don't track. referer_host = urlparse.urlparse(referer).netloc if referer_host == request.META.get('HTTP_HOST'): return response ga.track_api_pageview(request) return response
def inner(request, *args, **kwargs): response = view(request, *args, **kwargs) if response.status_code < 500: referer = request.META.get("HTTP_REFERER") if referer: # If the referer host is the same as the request host # that implies that the API was used as an AJAX request # in our main webapp itself. If so, don't track. referer_host = urlparse.urlparse(referer).netloc if referer_host == request.META.get("HTTP_HOST"): return response ga.track_api_pageview(request) return response