From 61587a6bfa16b9d534aeb16610cf8a2826e34f8f Mon Sep 17 00:00:00 2001 From: John Karahalis Date: Sat, 30 May 2020 00:28:21 -0400 Subject: [PATCH] Fix #554: Add Google Analytics support --- .env-dist | 1 + .eslintrc.js | 3 +++ glam/settings.py | 3 +++ glam/urls.py | 12 +++++++++++- public/index.html | 15 +++++++++++++++ rollup.config.js | 5 ++++- src/routing/Router.svelte | 8 ++++++++ 7 files changed, 45 insertions(+), 2 deletions(-) diff --git a/.env-dist b/.env-dist index ee1e5d796..a6799d3f4 100644 --- a/.env-dist +++ b/.env-dist @@ -11,3 +11,4 @@ AUTH0_FRONTEND_CLIENT_ID= AUTH0_ORIGIN= AUTH0_AUDIENCE_ID= AUTH0_API_SCOPE= +GA_TRACKING_ID= diff --git a/.eslintrc.js b/.eslintrc.js index b8881d809..234af24f0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -8,6 +8,9 @@ module.exports = { es6: true, }, extends: ['airbnb-base'], + globals: { + gtag: 'readonly', + }, plugins: ['jest', 'svelte3'], rules: { 'import/prefer-default-export': 'off', diff --git a/glam/settings.py b/glam/settings.py index 8b35f36b8..9bf9f7172 100644 --- a/glam/settings.py +++ b/glam/settings.py @@ -120,6 +120,9 @@ class Core(Configuration): ], } + GA_TRACKING_ID = values.Value("", environ_name="GA_TRACKING_ID", + environ_prefix=None) + class Base(Core): """Configuration that may change per-environment, some with defaults.""" diff --git a/glam/urls.py b/glam/urls.py index ba9f541eb..16e2c0d58 100644 --- a/glam/urls.py +++ b/glam/urls.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.urls import path from django.views.generic.base import TemplateView @@ -6,7 +7,16 @@ urlpatterns = [ - path("", TemplateView.as_view(template_name="index.html"), name="index"), + path( + "", + TemplateView.as_view( + template_name="index.html", + extra_context={ + "GA_TRACKING_ID": settings.GA_TRACKING_ID, + } + ), + name="index" + ), path("api/v1/data/", api_views.aggregations, name="v1-data"), path("api/v1/probes/", api_views.probes, name="v1-probes"), path("api/v1/probes/random/", api_views.random_probes, name="v1-random-probes"), diff --git a/public/index.html b/public/index.html index 07743ae6b..37610a91e 100644 --- a/public/index.html +++ b/public/index.html @@ -1,6 +1,21 @@ + + + + diff --git a/rollup.config.js b/rollup.config.js index 4e8c7216b..5facc6af3 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -18,7 +18,10 @@ export default { }, plugins: [ json(), - replace({ __BASE_DOMAIN__: production ? '' : 'http://localhost:8000' }), + replace({ + __BASE_DOMAIN__: production ? '' : 'http://localhost:8000', + __GA_TRACKING_ID__: process.env.GA_TRACKING_ID, + }), svelte({ // enable run-time checks when not in production dev: !production, diff --git a/src/routing/Router.svelte b/src/routing/Router.svelte index 7ed1ec0cc..25fbebbb2 100644 --- a/src/routing/Router.svelte +++ b/src/routing/Router.svelte @@ -71,6 +71,14 @@ }; } + // Google Analytics middleware + page((ctx, next) => { + gtag('config', '__GA_TRACKING_ID__', { + page_path: ctx.pathname, + }); + next(); + }); + page('/', useComponent(Home)); page('/:product/:section/:probeName/explore', useComponent(ProbeExplore, 'explore')); page('/:product/:section/:probeName/table', useComponent(ProbeTable, 'table'));