Skip to content

Commit

Permalink
Fixed up Google Analytics 4 (gtag)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmwatson committed Jan 24, 2024
1 parent 72eb1f2 commit ecf84d5
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
- GOOGLE_RECAPTCHA_SECRET_KEY
- ENVIRONMENT=development
- SENTRY_DSN=https://[email protected]/5246237
- GOOGLE_ANALYTICS_ID=G-T25P07Y7G2
ports:
- "8000:8000"
command: bin/wait-for-deps.sh python manage.py runserver 0.0.0.0:8000
Expand Down
2 changes: 2 additions & 0 deletions pombola/admin_additions/templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
//]]>
</script>

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

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

Expand Down
1 change: 1 addition & 0 deletions pombola/core/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def add_settings( request ):
'settings': {
'STAGING': settings.STAGING,
'GOOGLE_ANALYTICS_ACCOUNT': settings.GOOGLE_ANALYTICS_ACCOUNT,
'GOOGLE_ANALYTICS_ID': settings.GOOGLE_ANALYTICS_ID,
'POLLDADDY_WIDGET_ID': settings.POLLDADDY_WIDGET_ID,
'DISQUS_SHORTNAME': settings.DISQUS_SHORTNAME,
'DISQUS_USE_IDENTIFIERS': settings.DISQUS_USE_IDENTIFIERS,
Expand Down
1 change: 1 addition & 0 deletions pombola/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@
# misc settings
HTTPLIB2_CACHE_DIR = os.path.join(data_dir, "httplib2_cache")
GOOGLE_ANALYTICS_ACCOUNT = os.environ.get("GOOGLE_ANALYTICS_ACCOUNT", None)
GOOGLE_ANALYTICS_ID = os.environ.get("GOOGLE_ANALYTICS_ID", None)
GOOGLE_SITE_VERIFICATION = os.environ.get("GOOGLE_SITE_VERIFICATION", None)

# Markitup settings
Expand Down
3 changes: 0 additions & 3 deletions pombola/settings/south_africa.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,3 @@
},
'TIMEOUT': 60*60*24,
}


GOOGLE_ANALYTICS_ID = 'G-RK0Z57HMF6'
19 changes: 11 additions & 8 deletions pombola/templates/default_base.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<!DOCTYPE html>
<html lang="{% block html_lang %}en{% endblock %}" class="no-js">
<head>
{% if settings.GOOGLE_ANALYTICS_ID %}
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ settings.GOOGLE_ANALYTICS_ID }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '{{ settings.GOOGLE_ANALYTICS_ID }}');
</script>
{% endif %}
{% comment %}

The Google Analytics documentation says that the experiment
Expand Down Expand Up @@ -81,14 +92,6 @@
{% block extra_headers %}{% endblock %}

</head>
<script async src="https://www.googletagmanager.com/gtag/js?id={{ settings.GOOGLE_ANALYTICS_ID }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '{{ settings.GOOGLE_ANALYTICS_ID }}');
</script>
<body {% block body_attributes %}{% endblock %}>

{% block header %}
Expand Down

0 comments on commit ecf84d5

Please sign in to comment.