Skip to content

Commit

Permalink
feat: Add GA 4 support to edX platform (#762)
Browse files Browse the repository at this point in the history
Merge pull request #32032 from raccoongang/sagirov/tCRIL_GA-18

[FC-0014] Add GA 4 support to edX platform

Co-authored-by: Brian Mesick <[email protected]>
  • Loading branch information
MaferMazu and bmtcril authored Jul 18, 2023
1 parent eec8871 commit 5c65a71
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions lms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,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 = ''
Expand Down
1 change: 1 addition & 0 deletions lms/envs/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,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')
Expand Down
13 changes: 13 additions & 0 deletions lms/templates/certificates/accomplishment-base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
<title>${document_title}</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:
<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 | n, js_escaped_string}');
</script>
% endif
</head>

<body class="layout-accomplishment view-valid-accomplishment ${dir_rtl} certificate certificate-${course_mode_class}" data-view="valid-accomplishment">
Expand Down
12 changes: 12 additions & 0 deletions lms/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@
</script>
% endif

<% ga_4_id = static.get_value("GOOGLE_ANALYTICS_4_ID", settings.GOOGLE_ANALYTICS_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 | n, js_escaped_string}');
</script>
% endif

<% branch_key = static.get_value("BRANCH_IO_KEY", settings.BRANCH_IO_KEY) %>
% if branch_key and not is_from_mobile_app:
<script type="text/javascript">
Expand Down
12 changes: 12 additions & 0 deletions lms/templates/main_django.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5c65a71

Please sign in to comment.