diff --git a/hexa/analytics/api.py b/hexa/analytics/api.py index 3b6dc155a..7e2f2f216 100644 --- a/hexa/analytics/api.py +++ b/hexa/analytics/api.py @@ -40,7 +40,7 @@ def track( if can_track is False: return - if request: + if request and "User-Agent" in request.headers: # Add request related properties parsed = user_agent_parser.Parse(request.headers["User-Agent"]) properties.update( diff --git a/hexa/analytics/tests/test_analytics.py b/hexa/analytics/tests/test_analytics.py index cab781257..dcdb38161 100644 --- a/hexa/analytics/tests/test_analytics.py +++ b/hexa/analytics/tests/test_analytics.py @@ -151,7 +151,7 @@ def test_create_user_profile( with self.settings(MIXPANEL_TOKEN=mixpanel_token): set_user_properties(self.USER) - mock_mixpanel.people_set_once.assert_called_once_with( + mock_mixpanel.people_set.assert_called_once_with( distinct_id=str(self.USER.id), properties={ "$email": self.USER.email, diff --git a/hexa/analytics/tests/test_views.py b/hexa/analytics/tests/test_views.py index aff031ef7..b9671e34a 100644 --- a/hexa/analytics/tests/test_views.py +++ b/hexa/analytics/tests/test_views.py @@ -1,3 +1,5 @@ +from unittest import mock + from django.urls import reverse from hexa.core.test import TestCase @@ -35,16 +37,18 @@ def test_track_event(self): ) self.assertEqual(r.status_code, 200) - def test_track_event_analytics_not_enabled(self): + @mock.patch("hexa.analytics.api.mixpanel") + def test_track_event_analytics_not_enabled(self, mixpanel_mock): self.USER.analytics_enabled = False self.USER.save() self.client.force_login(self.USER) - r = self.client.post( - reverse( - "analytics:track", - ), - {"event": "page_viewed", "properties": {"page": "database"}}, - content_type="application/json", - ) - self.assertEqual(r.status_code, 401) - self.assertEqual(r.json(), {"error": "Analytics not enabled."}) + with self.settings(MIXPANEL_TOKEN="123"): + r = self.client.post( + reverse( + "analytics:track", + ), + {"event": "page_viewed", "properties": {"page": "database"}}, + content_type="application/json", + ) + self.assertEqual(r.status_code, 200) + mixpanel_mock.track.assert_not_called()