Skip to content

Commit

Permalink
reorganize all GA and GTM code into body-extra
Browse files Browse the repository at this point in the history
Move out of head to avoid breaKing YouTube video/API loading
  • Loading branch information
bryanlandia committed Jul 12, 2023
1 parent f9ed07d commit 830a624
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
14 changes: 2 additions & 12 deletions lms/templates/body-extra.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<%! import hmac %>
<%! import hashlib %>

<%include file="partials/analytics.html" />

## Renders when Cookie Banner is disabled or when it is enabled and user has dismissed (accepted) it:
% if (not get_global_settings().get('cookie_notification_enabled', False)) or (get_global_settings().get('cookie_notification_enabled', False) and (request.COOKIES.get('cookieconsent_status', '') == 'dismiss')):

Expand Down Expand Up @@ -51,16 +53,4 @@
<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/e4awi275';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script>
% endif

<%! from openedx.core.djangolib.js_utils import js_escaped_string %>
<%
customer_gtm_id = get_global_settings().get('customer_gtm_id')
%>

% if customer_gtm_id:
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${customer_gtm_id | n, js_escaped_string}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
% endif

% endif
31 changes: 0 additions & 31 deletions lms/templates/head-extra.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
<meta name="image" property="og:image" content="${default_og_image}" />
% endif

<%include file="partials/analytics.html" />

% if get_global_settings().get('default_additional_site_head_content', ''):
${get_global_settings().get('default_additional_site_head_content')}
Expand All @@ -57,36 +56,6 @@
## Renders when Cookie Banner is disabled or when it is enabled and user has dismissed (accepted) it:
% if (not get_global_settings().get('cookie_notification_enabled', False)) or (get_global_settings().get('cookie_notification_enabled', False) and (request.COOKIES.get('cookieconsent_status', '') == 'dismiss')):

<%
appsembler_ga_code = get_global_settings().get('appsembler_ga_code', '');
client_ga_code = get_global_settings().get('client_ga_code', '');
client_ga_enabled = get_global_settings().get('client_ga_enabled', True);
client_ga_version = get_global_settings().get('client_ga_version', 'v3');
%>
% if SHOW_GOOGLE_ANALYTICS:
% if client_ga_code and client_ga_enabled:
## for now Appsembler is still using v3 Universal Analytics and some customers do too.
## once Appsembler and all customers shift to v4 we remove this condition
% if client_ga_version == 'v4':
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=${client_ga_code}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '${client_ga_code}');
</script>
% else:
<script>
ga('create', '${client_ga_code}', 'auto', 'clientTracker');
ga('clientTracker.send', 'pageview');
</script>
% endif
% endif
<!-- /Google Analytics code -->
% endif

% if get_global_settings().get('cookie_accepted_additional_site_head_content', ''):
% if request.COOKIES.get('cookieconsent_status', '') == 'dismiss':
${get_global_settings().get('cookie_accepted_additional_site_head_content')}
Expand Down
38 changes: 37 additions & 1 deletion lms/templates/partials/analytics.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@
Dimension3: Visited course name
Dimension4: LMS or Studio
-->
<%
appsembler_ga_code = get_global_settings().get('appsembler_ga_code', '');
client_ga_code = get_global_settings().get('client_ga_code', '');
client_ga_enabled = get_global_settings().get('client_ga_enabled', True);
client_ga_version = get_global_settings().get('client_ga_version', 'v3');
%>

% if SHOW_GOOGLE_ANALYTICS:
% if client_ga_code and client_ga_enabled:
## for now Appsembler is still using v3 Universal Analytics and some customers do too.
## once Appsembler and all customers shift to v4 we remove this condition
% if client_ga_version == 'v4':
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=${client_ga_code}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${client_ga_code}');
</script>
% else:
<script>
ga('create', '${client_ga_code}', 'auto', 'clientTracker');
ga('clientTracker.send', 'pageview');
</script>
% endif
% endif
<!-- /Google Analytics code -->
% endif

<%
appsemblerLmsTrackingCode = get_global_settings().get('appsembler_lms_tracking_code')
Expand All @@ -119,4 +148,11 @@
<!-- /new analytics -->
% endif

% endif
% endif

% if customer_gtm_id:
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${customer_gtm_id | n, js_escaped_string}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
% endif

0 comments on commit 830a624

Please sign in to comment.