feat: add google_analytics_4_id template tag for main_django.html

This commit is contained in:
Sagirov Eugeniy
2023-04-03 13:30:38 +03:00
parent 64238e5931
commit 4a551aa1bb
2 changed files with 21 additions and 0 deletions

View File

@@ -28,6 +28,18 @@
{% optional_include "head-extra.html"|microsite_template_path %}
{% google_analytics_4_id as ga_4_id %}
{% if ga_4_id %}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ ga_4_id }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ ga_4_id }}');
</script>
{% endif %}
<meta name="path_prefix" content="{{EDX_ROOT_URL}}">
</head>

View File

@@ -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)