Repository: hydroshare/hydroshare

Issue:
Track user agent for metrics
**Describe the feature you'd like and what it will do**
In HS v2.5.4, we don't track user_agent in our metrics. This makes it difficult to tell when requests to HS are occurring via direct UI interactions, or via other tools like hsclient.


**Why is this feature important?**
We need more insight into how HS' ecosystem of tools are being used. This information should drive our continued development on existing tools and our consideration of additions for future use.

**Is your feature request related to a problem? Please describe.**
It is difficult to make decisions without information.



Discussion:

Resolving patch:
diff --git a/hs_tracking/utils.py b/hs_tracking/utils.py
--- a/hs_tracking/utils.py
+++ b/hs_tracking/utils.py
@@ -47,6 +47,12 @@ def get_std_log_fields(request, session=None):
     """ returns a standard set of metadata that to each receiver function.
     This ensures that all activities are reporting a consistent set of metrics
     """
+    try:
+        user_agent = request.META['HTTP_USER_AGENT']
+        human = is_human(user_agent)
+    except KeyError:
+        user_agent = None
+        human = None
     user_type = None
     user_email_tld = None
     full_domain = None
@@ -59,7 +65,9 @@ def get_std_log_fields(request, session=None):
         'user_ip': get_client_ip(request),
         'user_type': user_type,
         'user_email_domain': user_email_tld,
-        'user_email_domain_full': full_domain
+        'user_email_domain_full': full_domain,
+        'is_human': human,
+        'user_agent': user_agent
     }
 
 


Test patch:
diff --git a/hs_tracking/tests/test_tracking.py b/hs_tracking/tests/test_tracking.py
--- a/hs_tracking/tests/test_tracking.py
+++ b/hs_tracking/tests/test_tracking.py
@@ -323,11 +323,11 @@ def test_capture_logins_and_logouts(self):
 
         kvp = dict(tuple(pair.split('=')) for pair in var1.value.split('|'))
         self.assertEqual(var1.name, 'begin_session')
-        self.assertEqual(len(list(kvp.keys())), 4)
+        self.assertEqual(len(list(kvp.keys())), 6)
 
         kvp = dict(tuple(pair.split('=')) for pair in var2.value.split('|'))
         self.assertEqual(var2.name, 'login')
-        self.assertEqual(len(list(kvp.keys())), 4)
+        self.assertEqual(len(list(kvp.keys())), 6)
 
         client.logout()
 
@@ -335,7 +335,7 @@ def test_capture_logins_and_logouts(self):
         var = Variable.objects.latest('timestamp')
         kvp = dict(tuple(pair.split('=')) for pair in var.value.split('|'))
         self.assertEqual(var.name, 'logout')
-        self.assertEqual(len(list(kvp.keys())), 4)
+        self.assertEqual(len(list(kvp.keys())), 6)
 
     def test_activity_parsing(self):
 
@@ -347,7 +347,7 @@ def test_activity_parsing(self):
 
         kvp = dict(tuple(pair.split('=')) for pair in var1.value.split('|'))
         self.assertEqual(var1.name, 'begin_session')
-        self.assertEqual(len(list(kvp.keys())), 4)
+        self.assertEqual(len(list(kvp.keys())), 6)
 
         client.logout()
 
@@ -377,7 +377,7 @@ def tearDown(self):
     def test_std_log_fields(self):
 
         log_fields = utils.get_std_log_fields(self.request, self.session)
-        self.assertTrue(len(list(log_fields.keys())) == 4)
+        self.assertTrue(len(list(log_fields.keys())) == 6)
         self.assertTrue('user_ip' in log_fields)
         self.assertTrue('user_type' in log_fields)
         self.assertTrue('user_email_domain' in log_fields)