From 6ae9d1514164c06f0d033d2b2dae8a364ebcc4d4 Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Tue, 14 Jul 2020 10:34:53 +0100 Subject: [PATCH] Add analytics error tracking to checkbox fields The existing macros added data attributes to any error message displayed which communicated the error to Google Analytics (if the user had given consent). This re-implements that functionality. --- app/main/forms.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index 567802fad..b04aed51b 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -568,7 +568,14 @@ class govukCheckboxField(govukCheckboxesMixin, BooleanField): # error messages error_message = None if field.errors: - error_message = {"text": " ".join(field.errors).strip()} + error_message = { + "attributes": { + "data-module": "track-error", + "data-error-type": field.errors[0], + "data-error-label": field.name + }, + "text": " ".join(field.errors).strip() + } params = { 'name': field.name, @@ -650,7 +657,14 @@ class govukCheckboxesField(govukCheckboxesMixin, SelectMultipleField): # error messages error_message = None if field.errors: - error_message = {"text": " ".join(field.errors).strip()} + error_message = { + "attributes": { + "data-module": "track-error", + "data-error-type": field.errors[0], + "data-error-label": field.name + }, + "text": " ".join(field.errors).strip() + } # returns either a list or a hierarchy of lists # depending on how get_items_from_options is implemented