From ee84c7f133f558f00427bc3eee5f17a11bcdcdf2 Mon Sep 17 00:00:00 2001 From: petechd <53475968+petechd@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:47:51 +0100 Subject: [PATCH] Add custom gtag event (#1460) --- app/helpers/template_helpers.py | 10 +++---- app/survey_config/survey_config.py | 5 ---- templates/layouts/_base.html | 5 ++-- tests/app/helpers/test_template_helpers.py | 26 +++++++++---------- .../integration/test_application_variables.py | 4 +-- 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/app/helpers/template_helpers.py b/app/helpers/template_helpers.py index 75c9c68244..b5bc4d3751 100644 --- a/app/helpers/template_helpers.py +++ b/app/helpers/template_helpers.py @@ -119,20 +119,16 @@ def service_links_context( @property def data_layer_context( self, - ) -> list[dict]: + ) -> dict[str, str]: tx_id_context = ( {"tx_id": metadata.tx_id} if (metadata := get_metadata(current_user)) - else None + else {} ) - additional_context = self._survey_config.get_additional_data_layer_context() schema_context = { key: value for key in DATA_LAYER_KEYS if (value := cookie_session.get(key)) } - context = [*additional_context, schema_context] - if tx_id_context: - context.append(tx_id_context) - return context + return tx_id_context | schema_context @property def footer_context(self) -> dict[str, Any]: diff --git a/app/survey_config/survey_config.py b/app/survey_config/survey_config.py index c9cd640d84..36aeb5ae58 100644 --- a/app/survey_config/survey_config.py +++ b/app/survey_config/survey_config.py @@ -70,8 +70,3 @@ def get_footer_legal_links( # pylint: disable=unused-argument, no-self-use self, cookie_has_theme: bool ) -> Optional[list[dict]]: return None - - def get_additional_data_layer_context( # pylint: disable=no-self-use - self, - ) -> list: - return [] diff --git a/templates/layouts/_base.html b/templates/layouts/_base.html index 40b21c128f..fa7c601f73 100644 --- a/templates/layouts/_base.html +++ b/templates/layouts/_base.html @@ -73,9 +73,6 @@ {% block head %} {# djlint:off #} {% if google_tag_id %} - {% endif %} diff --git a/tests/app/helpers/test_template_helpers.py b/tests/app/helpers/test_template_helpers.py index f622be9cd5..d9e277cdbe 100644 --- a/tests/app/helpers/test_template_helpers.py +++ b/tests/app/helpers/test_template_helpers.py @@ -1029,13 +1029,13 @@ def test_use_default_survey_title_in_context_when_no_cookie( SurveyType.DEFAULT, "en", QuestionnaireSchema({"survey_id": "001"}), - [{"survey_id": "001"}], + {"survey_id": "001"}, ), ( SurveyType.DEFAULT, "en", QuestionnaireSchema({"survey_id": "001", "form_type": "test"}), - [{"form_type": "test", "survey_id": "001"}], + {"form_type": "test", "survey_id": "001"}, ), ( SurveyType.BUSINESS, @@ -1043,7 +1043,7 @@ def test_use_default_survey_title_in_context_when_no_cookie( QuestionnaireSchema( {"survey_id": "001", "form_type": "test", "title": "test_title"} ), - [{"form_type": "test", "survey_id": "001", "title": "test_title"}], + {"form_type": "test", "survey_id": "001", "title": "test_title"}, ), ( SurveyType.HEALTH, @@ -1051,7 +1051,7 @@ def test_use_default_survey_title_in_context_when_no_cookie( QuestionnaireSchema( {"survey_id": "001", "form_type": "test", "title": "test_title"} ), - [{"form_type": "test", "survey_id": "001", "title": "test_title"}], + {"form_type": "test", "survey_id": "001", "title": "test_title"}, ), ( SurveyType.SOCIAL, @@ -1059,55 +1059,55 @@ def test_use_default_survey_title_in_context_when_no_cookie( QuestionnaireSchema( {"survey_id": "001", "form_type": "test", "title": "test_title"} ), - [{"form_type": "test", "survey_id": "001", "title": "test_title"}], + {"form_type": "test", "survey_id": "001", "title": "test_title"}, ), ( SurveyType.NORTHERN_IRELAND, "en", QuestionnaireSchema({"survey_id": "001"}), - [{"survey_id": "001"}], + {"survey_id": "001"}, ), ( SurveyType.DBT_DSIT, "en", QuestionnaireSchema({"survey_id": "001"}), - [{"survey_id": "001"}], + {"survey_id": "001"}, ), ( SurveyType.DBT_DSIT_NI, "en", QuestionnaireSchema({"survey_id": "001"}), - [{"survey_id": "001"}], + {"survey_id": "001"}, ), ( SurveyType.DBT, "en", QuestionnaireSchema({"survey_id": "001"}), - [{"survey_id": "001"}], + {"survey_id": "001"}, ), ( SurveyType.DBT_NI, "en", QuestionnaireSchema({"survey_id": "001"}), - [{"survey_id": "001"}], + {"survey_id": "001"}, ), ( SurveyType.ORR, "en", QuestionnaireSchema({"survey_id": "001"}), - [{"survey_id": "001"}], + {"survey_id": "001"}, ), ( SurveyType.DESNZ, "en", QuestionnaireSchema({"survey_id": "001"}), - [{"survey_id": "001"}], + {"survey_id": "001"}, ), ( SurveyType.DESNZ_NI, "en", QuestionnaireSchema({"survey_id": "001"}), - [{"survey_id": "001"}], + {"survey_id": "001"}, ), ], ) diff --git a/tests/integration/test_application_variables.py b/tests/integration/test_application_variables.py index 5ba3b54cea..62a5398f33 100644 --- a/tests/integration/test_application_variables.py +++ b/tests/integration/test_application_variables.py @@ -24,7 +24,7 @@ def test_google_analytics_code_and_credentials_are_present(self): self.get("/questionnaire/feedback/") self.assertStatusOK() self.assertInHead( - f'dataLayer = [{{"form_type": "H", "survey_id": "0", "title": "Feedback test schema"}}, {{"tx_id": "{actual["METADATA"]["tx_id"]}"}}]' + f'"form_type": "H", "survey_id": "0", "title": "Feedback test schema", "tx_id": "{actual["METADATA"]["tx_id"]}"' ) self.assertInHead("https://www.googletagmanager.com") self.assertInHead(settings.EQ_GOOGLE_TAG_ID) @@ -40,7 +40,7 @@ def test_google_analytics_data_layer_has_no_null_fields(self): self.assertStatusOK() # form_type is empty so should not be present self.assertInHead( - f'dataLayer = [{{"survey_id": "001", "title": "Other input fields"}}, {{"tx_id": "{actual["METADATA"]["tx_id"]}"}}]' + f'"survey_id": "001", "title": "Other input fields", "tx_id": "{actual["METADATA"]["tx_id"]}"' ) def test_livereload_script_rendered(self):