Repository: mdn/kuma

Issue:
U - GA Instrumentation for new user flow (username/email)
Parent: #6283

- How many people clicked to edit their username?
- How many people clicked to change their email address?


Discussion:

Resolving patch:
diff --git a/kuma/core/ga_tracking.py b/kuma/core/ga_tracking.py
--- a/kuma/core/ga_tracking.py
+++ b/kuma/core/ga_tracking.py
@@ -44,6 +44,8 @@
 ACTION_PROFILE_AUDIT = "profile-audit"
 # Have completed the profile creation form.
 ACTION_PROFILE_CREATED = "profile-created"
+# Have changed a suggested default. E.g. not "peterbe2" but "peterbe_new"
+ACTION_PROFILE_EDIT = "profile-edit"
 # Have detected an error in the edited profile creation form.
 ACTION_PROFILE_EDIT_ERROR = "profile-edit-error"
 # Checked or didn't check the "Newsletter" checkbox on sign up.
diff --git a/kuma/users/views.py b/kuma/users/views.py
--- a/kuma/users/views.py
+++ b/kuma/users/views.py
@@ -45,6 +45,7 @@
 )
 from kuma.core.ga_tracking import (
     ACTION_PROFILE_AUDIT,
+    ACTION_PROFILE_EDIT,
     ACTION_PROFILE_EDIT_ERROR,
     CATEGORY_SIGNUP_FLOW,
     track_event,
@@ -625,7 +626,7 @@ def get_form(self, form_class=None):
             pass
 
         email = self.sociallogin.account.extra_data.get("email") or None
-        email_data = (self.sociallogin.account.extra_data.get("email_addresses")) or []
+        email_data = self.sociallogin.account.extra_data.get("email_addresses") or []
 
         # Discard email addresses that won't validate
         extra_email_addresses = []
@@ -695,7 +696,13 @@ def form_valid(self, form):
             get_adapter().stash_verified_email(self.request, data["email"])
 
         with transaction.atomic():
-            form.save(self.request)
+            saved_user = form.save(self.request)
+
+            if saved_user.username != form.initial["username"]:
+                track_event(
+                    CATEGORY_SIGNUP_FLOW, ACTION_PROFILE_EDIT, "username edit",
+                )
+
         return helpers.complete_social_signup(self.request, self.sociallogin)
 
     def form_invalid(self, form):


Test patch:
diff --git a/kuma/users/tests/test_views.py b/kuma/users/tests/test_views.py
--- a/kuma/users/tests/test_views.py
+++ b/kuma/users/tests/test_views.py
@@ -25,6 +25,7 @@
     ACTION_FREE_NEWSLETTER,
     ACTION_PROFILE_AUDIT,
     ACTION_PROFILE_CREATED,
+    ACTION_PROFILE_EDIT,
     ACTION_PROFILE_EDIT_ERROR,
     ACTION_RETURNING_USER_SIGNIN,
     CATEGORY_SIGNUP_FLOW,
@@ -1242,6 +1243,64 @@ def test_signup_github_email_manual_override(self):
         response = self.client.post(self.signup_url, data=data)
         assert response.status_code == 400
 
+    def test_signup_username_edit_event_tracking(self):
+        """
+        Tests that GA tracking events are sent for editing the default suggested
+        username when signing-up with a new account.
+        """
+        with self.settings(
+            GOOGLE_ANALYTICS_ACCOUNT="UA-XXXX-1",
+            GOOGLE_ANALYTICS_TRACKING_RAISE_ERRORS=True,
+        ):
+            p1 = mock.patch("kuma.users.signal_handlers.track_event")
+            p2 = mock.patch("kuma.users.views.track_event")
+            p3 = mock.patch("kuma.users.providers.google.views.track_event")
+            with p1, p2 as track_event_mock_views, p3:
+
+                response = self.google_login()
+
+                doc = pq(response.content)
+                # Just sanity check what's the defaults in the form.
+                # Remember, the self.google_login relies on the provider giving an
+                # email that is 'example@gmail.com'
+                assert doc.find('input[name="username"]').val() == "example"
+                assert doc.find('input[name="email"]').val() == "example@gmail.com"
+
+                data = {
+                    "website": "",
+                    "username": "better",
+                    "email": "example@gmail.com",
+                    "terms": False,  # Note!
+                    "is_newsletter_subscribed": True,
+                }
+                response = self.client.post(self.signup_url, data=data)
+                assert response.status_code == 200
+
+                track_event_mock_views.assert_has_calls(
+                    [
+                        mock.call(CATEGORY_SIGNUP_FLOW, ACTION_PROFILE_AUDIT, "google"),
+                        # Note the lack of 'ACTION_PROFILE_EDIT' because the form
+                        # submission was invalid and the save didn't go ahead.
+                    ]
+                )
+
+                # This time, the form submission will work.
+                data["terms"] = True
+                response = self.client.post(self.signup_url, data=data)
+                assert response.status_code == 302
+
+                # Sanity check that the right user got created
+                assert User.objects.get(username="better")
+
+                track_event_mock_views.assert_has_calls(
+                    [
+                        mock.call(CATEGORY_SIGNUP_FLOW, ACTION_PROFILE_AUDIT, "google"),
+                        mock.call(
+                            CATEGORY_SIGNUP_FLOW, ACTION_PROFILE_EDIT, "username edit"
+                        ),
+                    ]
+                )
+
     def test_signin_github_event_tracking(self):
         """Tests that kuma.core.ga_tracking.track_event is called when you
         sign in with GitHub a consecutive time."""