Repository: mdn/kuma

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

- How many people saw a username error?
- How many people saw an email error?


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 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.
 ACTION_FREE_NEWSLETTER = "free-newsletter"
 # When logging in with one provider and benefitting from a verified email
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_ERROR,
     CATEGORY_SIGNUP_FLOW,
     track_event,
 )
@@ -707,6 +708,19 @@ def form_valid(self, form):
             form.save(self.request)
         return helpers.complete_social_signup(self.request, self.sociallogin)
 
+    def form_invalid(self, form):
+        """
+        This is called on POST but only when the form is invalid. We're
+        overriding this method simply to send GA events when we find an
+        error in the username and/or email fields.
+        """
+        for name in ("username", "email"):
+            if form.errors.get(name) is not None:
+                track_event(
+                    CATEGORY_SIGNUP_FLOW, ACTION_PROFILE_EDIT_ERROR, name,
+                )
+        return super().form_invalid(form)
+
     def get_context_data(self, **kwargs):
         context = super(SignupView, self).get_context_data(**kwargs)
 


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_ERROR,
     ACTION_RETURNING_USER_SIGNIN,
     CATEGORY_SIGNUP_FLOW,
 )
@@ -1267,6 +1268,51 @@ def test_signup_github_event_tracking(self):
                     CATEGORY_SIGNUP_FLOW, ACTION_PROFILE_AUDIT, "github"
                 )
 
+    def test_signup_username_and_email_errors_event_tracking(self):
+        """
+        Tests that GA tracking events are sent for errors in the username
+        and/or email fields submitted when signing-up with a new account.
+        """
+        user(username="octocat", save=True)
+        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 as track_event_mock_signals, p2 as track_event_mock_views, p3 as track_event_mock_google:
+
+                self.google_login()
+
+                data = {
+                    "website": "",
+                    "username": "octocat",
+                    "email": "invalid@email",
+                    "terms": True,
+                    "is_newsletter_subscribed": True,
+                }
+                response = self.client.post(self.signup_url, data=data)
+                assert response.status_code == 200
+
+                track_event_mock_signals.assert_called_with(
+                    CATEGORY_SIGNUP_FLOW, ACTION_AUTH_SUCCESSFUL, "google"
+                )
+                track_event_mock_google.assert_called_with(
+                    CATEGORY_SIGNUP_FLOW, ACTION_AUTH_STARTED, "google"
+                )
+                track_event_mock_views.assert_has_calls(
+                    [
+                        mock.call(CATEGORY_SIGNUP_FLOW, ACTION_PROFILE_AUDIT, "google"),
+                        mock.call(
+                            CATEGORY_SIGNUP_FLOW, ACTION_PROFILE_EDIT_ERROR, "username"
+                        ),
+                        mock.call(
+                            CATEGORY_SIGNUP_FLOW, ACTION_PROFILE_EDIT_ERROR, "email"
+                        ),
+                    ]
+                )
+
     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."""