Skip to content

Commit

Permalink
Add custom gtag event (#1460)
Browse files Browse the repository at this point in the history
  • Loading branch information
petechd authored Sep 17, 2024
1 parent 81bdf26 commit ee84c7f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 30 deletions.
10 changes: 3 additions & 7 deletions app/helpers/template_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
5 changes: 0 additions & 5 deletions app/survey_config/survey_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
5 changes: 2 additions & 3 deletions templates/layouts/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@
{% block head %}
{# djlint:off #}
{% if google_tag_id %}
<script nonce="{{ csp_nonce }}">
dataLayer = {{ data_layer | tojson }}
</script>
<!-- Google tag (gtag.js) -->
<script nonce="{{ csp_nonce }}">
function loadGoogleTagScript() {
Expand All @@ -98,7 +95,9 @@
loadGoogleTagScript()
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('set', {{ data_layer }});
gtag('config', '{{ google_tag_id }}');
gtag('event', 'survey_data', {{ data_layer | tojson }});
}
</script>
{% endif %}
Expand Down
26 changes: 13 additions & 13 deletions tests/app/helpers/test_template_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,85 +1029,85 @@ 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,
"en",
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,
"en",
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,
"en",
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"},
),
],
)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_application_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit ee84c7f

Please sign in to comment.