Skip to content

Commit 8284ab1

Browse files
authored
fix set-up and loading of GA gtag function (#5624)
1 parent 02ec514 commit 8284ab1

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

kitsune/sumo/jinja2/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<!DOCTYPE html>
77
<html class="no-js" lang="{{ request.LANGUAGE_CODE }}"{% if DIR %} dir="{{ DIR }}"{% endif %}{% if settings.GTM_CONTAINER_ID %} data-gtm-container-id="{{ settings.GTM_CONTAINER_ID }}"{% endif %}>
88
<head>
9+
{% include "includes/google-analytics.html" %}
10+
911
{% block head_top %}{% endblock %}
1012
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
1113

@@ -56,8 +58,6 @@
5658
{% if canonical_url %}
5759
<link rel="canonical" href="{{ canonical_url|safe }}" />
5860
{% endif %}
59-
60-
{% include "includes/google-analytics.html" %}
6161
</head>
6262

6363
<body class="html-{{ DIR }} logged-{% if user.is_authenticated %}in{% else %}out{% endif %} responsive {{ classes }} {{ request.LANGUAGE_CODE }}"

kitsune/sumo/static/sumo/js/gtm-snippet.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import dntEnabled from "./libs/dnt-helper";
3434
// product should be first match or null
3535
product = (matches && matches.length > 0) ? matches[1] : null;
3636

37-
3837
w.gtag('config', GTM_CONTAINER_ID, {
3938
'dimension1': product
4039
});

webpack/entrypoints-html.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ module.exports = Object.keys(entrypoints).map(entry =>
1818
htmlWebpackPlugin.tags.headTags.forEach(element => {
1919
element.attributes.nonce = "{{ request.csp_nonce }}";
2020
});
21+
if (entry == "gtm-snippet") {
22+
// Using "blocking" for the "scriptLoading" option doesn't work, so
23+
// for now, let's delete the "defer" attribute for the "gtm-snippet"
24+
// case, because it must block until loaded in order to ensure that
25+
// the "gtag" function is defined before any other JavaScript files
26+
// are executed.
27+
htmlWebpackPlugin.tags.headTags.forEach(element => {
28+
delete element.attributes.defer;
29+
});
30+
}
2131
return htmlWebpackPlugin.tags.headTags.join("");
2232
}
2333
}),

0 commit comments

Comments
 (0)