Repository: hydroshare/hydroshare

Issue:
track users' full email domain
**Describe the feature you'd like and what it will do**
In HS v2.5.4, we don't track full user email domain in our metrics.


**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.



Discussion:

Resolving patch:
diff --git a/hs_tracking/middleware.py b/hs_tracking/middleware.py
--- a/hs_tracking/middleware.py
+++ b/hs_tracking/middleware.py
@@ -72,7 +72,7 @@ def process_response(self, request, response):
         # get user info that will be recorded in the visit log
         session = Session.objects.for_request(request)
         usertype = utils.get_user_type(session)
-        emaildomain = utils.get_user_email_domain(session)
+        email_tld = utils.get_user_email_tld(session)
         ip = utils.get_client_ip(request)
 
         # build the message string (key:value pairs)
@@ -81,7 +81,7 @@ def process_response(self, request, response):
                          'http_method=%s' % request.method,
                          'http_code=%s' % response.status_code,
                          'user_type=%s' % usertype,
-                         'user_email_domain=%s' % emaildomain,
+                         'user_email_domain=%s' % email_tld,
                          'request_url=%s' % request.path]])
 
         resource_id = get_resource_id_from_url(request.path)
diff --git a/hs_tracking/utils.py b/hs_tracking/utils.py
--- a/hs_tracking/utils.py
+++ b/hs_tracking/utils.py
@@ -21,10 +21,20 @@ def get_user_email_domain(session):
     try:
         user = session.visitor.user
         emaildomain = user.email.split('@')[-1]
-        shortdomain = '.'.join(emaildomain.split('.')[1:])
     except AttributeError:
-        shortdomain = None
-    return shortdomain
+        emaildomain = None
+    return emaildomain
+
+
+def get_user_email_tld(session, emaildomain=None):
+    try:
+        if not emaildomain:
+            emaildomain = get_user_email_domain(session)
+        if emaildomain:
+            shortdomain = '.'.join(emaildomain.split('.')[1:])
+            return shortdomain
+    except AttributeError:
+        return None
 
 
 def is_human(user_agent):
@@ -38,15 +48,18 @@ def get_std_log_fields(request, session=None):
     This ensures that all activities are reporting a consistent set of metrics
     """
     user_type = None
-    user_email = None
+    user_email_tld = None
+    full_domain = None
     if session is not None:
         user_type = get_user_type(session)
-        user_email = get_user_email_domain(session)
+        full_domain = get_user_email_domain(session)
+        user_email_tld = get_user_email_tld(session, full_domain)
 
     return {
         'user_ip': get_client_ip(request),
         'user_type': user_type,
-        'user_email_domain': user_email,
+        'user_email_domain': user_email_tld,
+        'user_email_domain_full': full_domain
     }
 
 


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
@@ -107,6 +107,7 @@ def test_get(self):
         self.assertTrue('res_type' in list(values.keys()))
         self.assertTrue('name' in list(values.keys()))
         self.assertTrue('user_email_domain' in list(values.keys()))
+        self.assertTrue('user_email_domain_full' in list(values.keys()))
         self.assertTrue('user_type' in list(values.keys()))
         self.assertTrue('user_ip' in list(values.keys()))
         self.assertTrue('res_id' in list(values.keys()))
@@ -114,6 +115,7 @@ def test_get(self):
         self.assertTrue(values['res_type'] == res_type)
         self.assertTrue(values['name'] == app_name)
         self.assertTrue(values['user_email_domain'] == self.user.email[-3:])
+        self.assertTrue(values['user_email_domain_full'] == self.user.email.split('@')[-1])
         self.assertTrue(values['user_type'] == 'Unspecified')
         self.assertTrue(values['user_ip'] == '198.84.193.157')
         self.assertTrue(values['res_id'] == res_id)
@@ -321,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())), 3)
+        self.assertEqual(len(list(kvp.keys())), 4)
 
         kvp = dict(tuple(pair.split('=')) for pair in var2.value.split('|'))
         self.assertEqual(var2.name, 'login')
-        self.assertEqual(len(list(kvp.keys())), 3)
+        self.assertEqual(len(list(kvp.keys())), 4)
 
         client.logout()
 
@@ -333,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())), 3)
+        self.assertEqual(len(list(kvp.keys())), 4)
 
     def test_activity_parsing(self):
 
@@ -345,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())), 3)
+        self.assertEqual(len(list(kvp.keys())), 4)
 
         client.logout()
 
@@ -375,10 +377,11 @@ 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())) == 3)
+        self.assertTrue(len(list(log_fields.keys())) == 4)
         self.assertTrue('user_ip' in log_fields)
         self.assertTrue('user_type' in log_fields)
         self.assertTrue('user_email_domain' in log_fields)
+        self.assertTrue('user_email_domain_full' in log_fields)
 
     def test_ishuman(self):
 
@@ -426,8 +429,8 @@ def test_get_user_email(self):
             visitor = Visitor.objects.create()
             visitor.user = user
             session = Session.objects.create(visitor=visitor)
-            emaildom = utils.get_user_email_domain(session)
-            self.assertTrue(emaildom == dom)
+            emailtld = utils.get_user_email_tld(session)
+            self.assertTrue(emailtld == dom)
             user.delete()
 
     def test_client_ip(self):