Skip to content

Commit 1b2f3c0

Browse files
authored
Merge pull request #427 from torchbox/spike/experiment-with-double-GA-container
Move GTM id from env to wagtail settings, and add experimental secondary GTM id
2 parents 4be635f + 3426ff6 commit 1b2f3c0

5 files changed

Lines changed: 91 additions & 5 deletions

File tree

tbx/core/context_processors.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
from django.conf import settings
44

55
from tbx.core.models import ImportantPageSettings
6+
from tbx.core.utils.models import Tracking
67

78

89
def global_vars(request):
910
# Read the mode cookie to determine the user's saved preference for light or dark mode, if it exists
1011
# Ensure it is one of the allowed values
1112
mode = request.COOKIES.get("torchbox-mode", "dark")
13+
tracking = Tracking.for_request(request)
1214
if mode not in settings.ALLOWED_MODES:
1315
mode = "dark"
1416

1517
return {
16-
"GOOGLE_TAG_MANAGER_ID": getattr(settings, "GOOGLE_TAG_MANAGER_ID", None),
18+
"GOOGLE_TAG_MANAGER_ID": getattr(tracking, "google_tag_manager_id", None),
19+
"GOOGLE_TAG_MANAGER_SECONDARY_ID": getattr(
20+
tracking, "google_tag_manager_secondary_id", None
21+
),
1722
"SEO_NOINDEX": settings.SEO_NOINDEX,
1823
"COOKIE_POLICY_PAGE": ImportantPageSettings.for_request(
1924
request
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Generated by Django 5.2.5 on 2026-04-07 10:07
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("torchbox", "0040_divisionmixin_and_navigationsetmixin"),
10+
("wagtailcore", "0096_referenceindex_referenceindex_source_object_and_more"),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name="Tracking",
16+
fields=[
17+
(
18+
"id",
19+
models.AutoField(
20+
auto_created=True,
21+
primary_key=True,
22+
serialize=False,
23+
verbose_name="ID",
24+
),
25+
),
26+
(
27+
"google_tag_manager_id",
28+
models.CharField(
29+
blank=True,
30+
help_text="Your Google Tag Manager ID.",
31+
max_length=255,
32+
),
33+
),
34+
(
35+
"google_tag_manager_secondary_id",
36+
models.CharField(
37+
blank=True,
38+
help_text="Experimental: Your Second Google Tag Manager ID.",
39+
max_length=255,
40+
),
41+
),
42+
(
43+
"site",
44+
models.OneToOneField(
45+
editable=False,
46+
on_delete=django.db.models.deletion.CASCADE,
47+
to="wagtailcore.site",
48+
),
49+
),
50+
],
51+
options={
52+
"abstract": False,
53+
},
54+
),
55+
]

tbx/core/utils/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ class SocialMediaSettings(BaseSiteSetting):
151151
)
152152

153153

154+
@register_setting(icon="view")
155+
class Tracking(BaseSiteSetting):
156+
google_tag_manager_id = models.CharField(
157+
max_length=255,
158+
blank=True,
159+
help_text="Your Google Tag Manager ID.",
160+
)
161+
google_tag_manager_secondary_id = models.CharField(
162+
max_length=255,
163+
blank=True,
164+
help_text="Experimental: Your Second Google Tag Manager ID.",
165+
)
166+
167+
154168
class ColourTheme(models.TextChoices):
155169
NONE = "", "None"
156170
CORAL = "theme-coral", "Coral"

tbx/project_styleguide/templates/patterns/base_page.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
<!-- End Google Tag Manager -->
1414
{% endif %}
1515

16+
{# Experimental second GTM container #}
17+
{% if GOOGLE_TAG_MANAGER_SECONDARY_ID and not request.in_preview_panel %}
18+
<!-- Google Tag Manager 2 -->
19+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
20+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
21+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
22+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
23+
})(window,document,'script','dataLayer','{{ GOOGLE_TAG_MANAGER_SECONDARY_ID|escapejs }}');</script>
24+
<!-- End Google Tag Manager 2 -->
25+
{% endif %}
26+
1627
{% if not request.in_preview_panel %}
1728
{% if request.COOKIES.torchbox_cookie != 'true' %}
1829
<template data-vwo-loader>
@@ -137,6 +148,11 @@
137148
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={{ GOOGLE_TAG_MANAGER_ID|urlencode }}"
138149
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
139150
{% endif %}
151+
{# Experimental second GTM container #}
152+
{% if GOOGLE_TAG_MANAGER_SECONDARY_ID and not request.in_preview_panel %}
153+
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={{ GOOGLE_TAG_MANAGER_SECONDARY_ID|urlencode }}"
154+
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
155+
{% endif %}
140156
{% endblock body_top %}
141157

142158
{% block header %}

tbx/settings/base.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,10 +611,6 @@
611611
"BASE_TEMPLATE_NAMES": ["patterns/base_page.html"],
612612
}
613613

614-
615-
# Google Tag Manager ID from env
616-
GOOGLE_TAG_MANAGER_ID = env.get("GOOGLE_TAG_MANAGER_ID")
617-
618614
# Trial Hotjar tracking for the CMS admin.
619615
ADMIN_HOTJAR_SITE_ID = env.get("ADMIN_HOTJAR_SITE_ID")
620616

0 commit comments

Comments
 (0)