Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom gtag events #1460

Merged
merged 20 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/helpers/template_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ 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))
Expand All @@ -130,9 +130,14 @@ def data_layer_context(
key: value for key in DATA_LAYER_KEYS if (value := cookie_session.get(key))
}
context = [*additional_context, schema_context]
berroar marked this conversation as resolved.
Show resolved Hide resolved

if tx_id_context:
context.append(tx_id_context)
return context

flattened_context: dict[str, str] = {}
for d in context:
berroar marked this conversation as resolved.
Show resolved Hide resolved
flattened_context |= d
return flattened_context

@property
def footer_context(self) -> dict[str, Any]:
Expand Down
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
Loading