From 7213f27ca56a21b6c2d8d8e6b4be5c60d4d97ce9 Mon Sep 17 00:00:00 2001 From: Sagirov Eugeniy Date: Mon, 27 Mar 2023 14:12:18 +0300 Subject: [PATCH 1/4] feat: Add GA 4 support to edX platform --- lms/envs/common.py | 1 + lms/envs/production.py | 1 + lms/templates/certificates/accomplishment-base.html | 13 +++++++++++++ lms/templates/main.html | 12 ++++++++++++ 4 files changed, 27 insertions(+) diff --git a/lms/envs/common.py b/lms/envs/common.py index 59730af797a5..730cbf8dce1f 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -1515,6 +1515,7 @@ def _make_mako_template_dirs(settings): GOOGLE_SITE_VERIFICATION_ID = '' GOOGLE_ANALYTICS_LINKEDIN = 'GOOGLE_ANALYTICS_LINKEDIN_DUMMY' GOOGLE_ANALYTICS_TRACKING_ID = None +GOOGLE_ANALYTICS_4_ID = None ######################## BRANCH.IO ########################### BRANCH_IO_KEY = '' diff --git a/lms/envs/production.py b/lms/envs/production.py index c76ca3e8591a..342877206b0b 100644 --- a/lms/envs/production.py +++ b/lms/envs/production.py @@ -694,6 +694,7 @@ def get_env_setting(setting): GOOGLE_ANALYTICS_TRACKING_ID = AUTH_TOKENS.get('GOOGLE_ANALYTICS_TRACKING_ID') GOOGLE_ANALYTICS_LINKEDIN = AUTH_TOKENS.get('GOOGLE_ANALYTICS_LINKEDIN') GOOGLE_SITE_VERIFICATION_ID = ENV_TOKENS.get('GOOGLE_SITE_VERIFICATION_ID') +GOOGLE_ANALYTICS_4_ID = AUTH_TOKENS.get('GOOGLE_ANALYTICS_4_ID') ##### BRANCH.IO KEY ##### BRANCH_IO_KEY = AUTH_TOKENS.get('BRANCH_IO_KEY') diff --git a/lms/templates/certificates/accomplishment-base.html b/lms/templates/certificates/accomplishment-base.html index 2f49154d62e8..52ca4efa63d1 100644 --- a/lms/templates/certificates/accomplishment-base.html +++ b/lms/templates/certificates/accomplishment-base.html @@ -19,6 +19,19 @@ ${document_title} <%static:css group='style-certificates'/> + + + <% ga_4_id = static.get_value("GOOGLE_ANALYTICS_4_ID", settings.GOOGLE_ANALYTICS_4_ID) %> + % if ga_4_id: + + + % endif diff --git a/lms/templates/main.html b/lms/templates/main.html index 9ee3b6d5b13e..32be3607d34f 100644 --- a/lms/templates/main.html +++ b/lms/templates/main.html @@ -162,6 +162,18 @@ % endif +<% ga_4_id = static.get_value("GOOGLE_ANALYTICS_4_ID", settings.GOOGLE_ANALYTICS_4_ID) %> +% if ga_4_id: + + +% endif + <% branch_key = static.get_value("BRANCH_IO_KEY", settings.BRANCH_IO_KEY) %> % if branch_key and not is_from_mobile_app: % endif +<% ga_4_id = static.get_value("GOOGLE_ANALYTICS_4_ID", settings.GOOGLE_ANALYTICS_4_ID) %> +% if ga_4_id: + + +% endif + <% branch_key = static.get_value("BRANCH_IO_KEY", settings.BRANCH_IO_KEY) %> % if branch_key and not is_from_mobile_app: + + {% endif %} + diff --git a/openedx/core/djangoapps/site_configuration/templatetags/configuration.py b/openedx/core/djangoapps/site_configuration/templatetags/configuration.py index b5242a53b5b9..9de10d3bb07b 100644 --- a/openedx/core/djangoapps/site_configuration/templatetags/configuration.py +++ b/openedx/core/djangoapps/site_configuration/templatetags/configuration.py @@ -65,3 +65,12 @@ def microsite_template_path(template_name): """ template_name = theming_helpers.get_template_path(template_name) return template_name[1:] if template_name[0] == '/' else template_name + + +@register.simple_tag +def google_analytics_4_id(): + """ + Django template tag that outputs the GOOGLE_ANALYTICS_4_ID: + {% google_analytics_4_id %} + """ + return configuration_helpers.get_value("GOOGLE_ANALYTICS_4_ID", settings.GOOGLE_ANALYTICS_4_ID) From 18b8044fdfeefa9601f33d296eba95e182e6df7f Mon Sep 17 00:00:00 2001 From: Sagirov Eugeniy Date: Thu, 6 Apr 2023 16:49:50 +0300 Subject: [PATCH 4/4] fix: add js_escaped_string filter for ga_4_id --- lms/templates/certificates/accomplishment-base.html | 2 +- lms/templates/main.html | 2 +- lms/templates/public_video_share_embed.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lms/templates/certificates/accomplishment-base.html b/lms/templates/certificates/accomplishment-base.html index 52ca4efa63d1..38c306708f48 100644 --- a/lms/templates/certificates/accomplishment-base.html +++ b/lms/templates/certificates/accomplishment-base.html @@ -29,7 +29,7 @@ function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); - gtag('config', '${ga_4_id}'); + gtag('config', '${ga_4_id | n, js_escaped_string}'); % endif diff --git a/lms/templates/main.html b/lms/templates/main.html index 32be3607d34f..ac43e6eba25f 100644 --- a/lms/templates/main.html +++ b/lms/templates/main.html @@ -170,7 +170,7 @@ function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); - gtag('config', '${ga_4_id}'); + gtag('config', '${ga_4_id | n, js_escaped_string}'); % endif diff --git a/lms/templates/public_video_share_embed.html b/lms/templates/public_video_share_embed.html index c040bb839116..cb81c7e15cce 100644 --- a/lms/templates/public_video_share_embed.html +++ b/lms/templates/public_video_share_embed.html @@ -94,7 +94,7 @@ function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); - gtag('config', '${ga_4_id}'); + gtag('config', '${ga_4_id | n, js_escaped_string}'); % endif