Skip to content

Commit 01bc5df

Browse files
committed
feat: add GTM integration
1 parent a0e170f commit 01bc5df

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

lms/envs/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,9 @@ def _make_mako_template_dirs(settings):
16141614
GOOGLE_ANALYTICS_TRACKING_ID = None
16151615
GOOGLE_ANALYTICS_4_ID = None
16161616

1617+
######################## GOOGLE TAG MANAGER ###########################
1618+
GOOGLE_TAG_MANAGER_ID = None
1619+
16171620
######################## BRANCH.IO ###########################
16181621
BRANCH_IO_KEY = ''
16191622

lms/envs/production.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ def get_env_setting(setting):
598598

599599
}
600600

601+
##### GOOGLE TAG MANAGER IDS #####
602+
GOOGLE_TAG_MANAGER_ID = AUTH_TOKENS.get('GOOGLE_TAG_MANAGER_ID')
603+
601604
############## XBlock extra mixins ############################
602605
XBLOCK_MIXINS += tuple(XBLOCK_EXTRA_MIXINS)
603606

lms/templates/certificates/accomplishment-base.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
gtag('config', '${ga_4_id | n, js_escaped_string}');
4343
</script>
4444
% endif
45+
46+
<% gtm_id = static.get_value("GOOGLE_TAG_MANAGER_ID", getattr(settings, "GOOGLE_TAG_MANAGER_ID", None)) %>
47+
% if gtm_id:
48+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
49+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
50+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
51+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
52+
})(window,document,'script','dataLayer', '${gtm_id}');</script>
53+
% endif
4554
</head>
4655

4756
<body class="layout-accomplishment view-valid-accomplishment ${dir_rtl} certificate certificate-${course_mode_class}" data-view="valid-accomplishment">

lms/templates/main.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@
177177
</script>
178178
% endif
179179

180+
<% gtm_id = static.get_value("GOOGLE_TAG_MANAGER_ID", getattr(settings, "GOOGLE_TAG_MANAGER_ID", None)) %>
181+
% if gtm_id:
182+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
183+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
184+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
185+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
186+
})(window,document,'script','dataLayer', '${gtm_id}');</script>
187+
% endif
188+
180189
<% branch_key = static.get_value("BRANCH_IO_KEY", settings.BRANCH_IO_KEY) %>
181190
% if branch_key and not is_from_mobile_app:
182191
<script type="text/javascript">

lms/templates/main_django.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
</script>
4141
{% endif %}
4242

43+
{% if google_tag_manager_id %}
44+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
45+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
46+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
47+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
48+
})(window,document,'script','dataLayer', '{{ google_tag_manager_id }}');</script>
49+
{% endif %}
50+
4351
<meta name="path_prefix" content="{{EDX_ROOT_URL}}">
4452
</head>
4553

openedx/core/djangoapps/site_configuration/templatetags/configuration.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@ def google_analytics_4_id():
7474
{% google_analytics_4_id %}
7575
"""
7676
return configuration_helpers.get_value("GOOGLE_ANALYTICS_4_ID", settings.GOOGLE_ANALYTICS_4_ID)
77+
78+
79+
@register.simple_tag
80+
def google_tag_manager_id():
81+
"""
82+
Django template tag that outputs the GOOGLE_TAG_MANAGER_ID:
83+
{% google_tag_manager_id %}
84+
"""
85+
return configuration_helpers.get_value("GOOGLE_TAG_MANAGER_ID", settings.GOOGLE_TAG_MANAGER_ID)

0 commit comments

Comments
 (0)