Skip to content

Commit ecf84d5

Browse files
committed
Fixed up Google Analytics 4 (gtag)
1 parent 72eb1f2 commit ecf84d5

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ services:
2525
- GOOGLE_RECAPTCHA_SECRET_KEY
2626
- ENVIRONMENT=development
2727
- SENTRY_DSN=https://36d69cae4d76e0452f3fbdbb5019a279@o242378.ingest.sentry.io/5246237
28+
- GOOGLE_ANALYTICS_ID=G-T25P07Y7G2
2829
ports:
2930
- "8000:8000"
3031
command: bin/wait-for-deps.sh python manage.py runserver 0.0.0.0:8000

pombola/admin_additions/templates/admin/base_site.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
//]]>
4848
</script>
4949

50+
{% if settings.GOOGLE_ANALYTICS_ACCOUNT %}
5051
<script type="text/javascript">
5152
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
5253
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
@@ -56,6 +57,7 @@
5657
ga('create', '{{ settings.GOOGLE_ANALYTICS_ACCOUNT }}');
5758
ga('send', 'pageview');
5859
</script>
60+
{% endif %}
5961

6062
{% include 'autocomplete_light/static.html' %}
6163

pombola/core/context_processors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def add_settings( request ):
88
'settings': {
99
'STAGING': settings.STAGING,
1010
'GOOGLE_ANALYTICS_ACCOUNT': settings.GOOGLE_ANALYTICS_ACCOUNT,
11+
'GOOGLE_ANALYTICS_ID': settings.GOOGLE_ANALYTICS_ID,
1112
'POLLDADDY_WIDGET_ID': settings.POLLDADDY_WIDGET_ID,
1213
'DISQUS_SHORTNAME': settings.DISQUS_SHORTNAME,
1314
'DISQUS_USE_IDENTIFIERS': settings.DISQUS_USE_IDENTIFIERS,

pombola/settings/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@
346346
# misc settings
347347
HTTPLIB2_CACHE_DIR = os.path.join(data_dir, "httplib2_cache")
348348
GOOGLE_ANALYTICS_ACCOUNT = os.environ.get("GOOGLE_ANALYTICS_ACCOUNT", None)
349+
GOOGLE_ANALYTICS_ID = os.environ.get("GOOGLE_ANALYTICS_ID", None)
349350
GOOGLE_SITE_VERIFICATION = os.environ.get("GOOGLE_SITE_VERIFICATION", None)
350351

351352
# Markitup settings

pombola/settings/south_africa.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,3 @@
4444
},
4545
'TIMEOUT': 60*60*24,
4646
}
47-
48-
49-
GOOGLE_ANALYTICS_ID = 'G-RK0Z57HMF6'

pombola/templates/default_base.html

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
<!DOCTYPE html>
22
<html lang="{% block html_lang %}en{% endblock %}" class="no-js">
33
<head>
4+
{% if settings.GOOGLE_ANALYTICS_ID %}
5+
<!-- Google tag (gtag.js) -->
6+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ settings.GOOGLE_ANALYTICS_ID }}"></script>
7+
<script>
8+
window.dataLayer = window.dataLayer || [];
9+
function gtag(){dataLayer.push(arguments);}
10+
gtag('js', new Date());
11+
12+
gtag('config', '{{ settings.GOOGLE_ANALYTICS_ID }}');
13+
</script>
14+
{% endif %}
415
{% comment %}
516

617
The Google Analytics documentation says that the experiment
@@ -81,14 +92,6 @@
8192
{% block extra_headers %}{% endblock %}
8293

8394
</head>
84-
<script async src="https://www.googletagmanager.com/gtag/js?id={{ settings.GOOGLE_ANALYTICS_ID }}"></script>
85-
<script>
86-
window.dataLayer = window.dataLayer || [];
87-
function gtag(){dataLayer.push(arguments);}
88-
gtag('js', new Date());
89-
90-
gtag('config', '{{ settings.GOOGLE_ANALYTICS_ID }}');
91-
</script>
9295
<body {% block body_attributes %}{% endblock %}>
9396

9497
{% block header %}

0 commit comments

Comments
 (0)