From 6ca46ad590c4a9e7d84c89460d692af4aea434cd Mon Sep 17 00:00:00 2001 From: "oleksandr.buhaienko" Date: Thu, 5 Jun 2025 13:19:09 +0300 Subject: [PATCH] feat: add GTM integration --- lms/envs/common.py | 12 ++++++++++++ lms/templates/certificates/accomplishment-base.html | 9 +++++++++ lms/templates/main.html | 9 +++++++++ lms/templates/main_django.html | 8 ++++++++ .../site_configuration/templatetags/configuration.py | 9 +++++++++ 5 files changed, 47 insertions(+) diff --git a/lms/envs/common.py b/lms/envs/common.py index 3ace69ab0632..8e5c000dcc30 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -1614,6 +1614,18 @@ def _make_mako_template_dirs(settings): GOOGLE_ANALYTICS_TRACKING_ID = None GOOGLE_ANALYTICS_4_ID = None +################### GOOGLE TAG MANAGER ################## +# .. setting_name: GOOGLE_TAG_MANAGER_ID +# .. setting_default: None +# .. setting_description: The container ID for Google Tag Manager. +# Set this in the site configuration, if you want to enable Google Tag Manager (GTM) on your site. +# GTM allows you to manage marketing and analytics tags (like Google Analytics). +# When set, the GTM script will be injected into pages. +# Example: "GOOGLE_TAG_MANAGER_ID": "GTM-XXXXXXX" or with environment-specific parameters: +# "GOOGLE_TAG_MANAGER_ID": "GTM-XXXXXXX>m_auth=abc>m_preview=env-1>m_cookies_win=x" +# If you don’t use GTM, you can leave this setting as None. +GOOGLE_TAG_MANAGER_ID = None + ######################## BRANCH.IO ########################### BRANCH_IO_KEY = '' diff --git a/lms/templates/certificates/accomplishment-base.html b/lms/templates/certificates/accomplishment-base.html index c44baff1c2ae..e6cf7b274c1d 100644 --- a/lms/templates/certificates/accomplishment-base.html +++ b/lms/templates/certificates/accomplishment-base.html @@ -42,6 +42,15 @@ gtag('config', '${ga_4_id | n, js_escaped_string}'); % endif + + <% gtm_id = static.get_value("GOOGLE_TAG_MANAGER_ID", settings.GOOGLE_TAG_MANAGER_ID) %> + % if gtm_id: + + % endif diff --git a/lms/templates/main.html b/lms/templates/main.html index 1a11900dae58..38b1d308c09d 100644 --- a/lms/templates/main.html +++ b/lms/templates/main.html @@ -177,6 +177,15 @@ % endif +<% gtm_id = static.get_value("GOOGLE_TAG_MANAGER_ID", settings.GOOGLE_TAG_MANAGER_ID) %> +% if gtm_id: + +% endif + <% branch_key = static.get_value("BRANCH_IO_KEY", settings.BRANCH_IO_KEY) %> % if branch_key and not is_from_mobile_app: {% endif %} + {% if google_tag_manager_id %} + + {% endif %} + diff --git a/openedx/core/djangoapps/site_configuration/templatetags/configuration.py b/openedx/core/djangoapps/site_configuration/templatetags/configuration.py index 9de10d3bb07b..dd087bda998d 100644 --- a/openedx/core/djangoapps/site_configuration/templatetags/configuration.py +++ b/openedx/core/djangoapps/site_configuration/templatetags/configuration.py @@ -74,3 +74,12 @@ def google_analytics_4_id(): {% google_analytics_4_id %} """ return configuration_helpers.get_value("GOOGLE_ANALYTICS_4_ID", settings.GOOGLE_ANALYTICS_4_ID) + + +@register.simple_tag +def google_tag_manager_id(): + """ + Django template tag that outputs the GOOGLE_TAG_MANAGER_ID: + {% google_tag_manager_id %} + """ + return configuration_helpers.get_value("GOOGLE_TAG_MANAGER_ID", settings.GOOGLE_TAG_MANAGER_ID)