Skip to content

Commit

Permalink
Merge pull request #560 from openjck/issue-554-google-analytics
Browse files Browse the repository at this point in the history
Fix #554: Add Google Analytics support
  • Loading branch information
spasovski authored Jun 9, 2020
2 parents f8dbb18 + 61587a6 commit a738a1c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env-dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ AUTH0_FRONTEND_CLIENT_ID=
AUTH0_ORIGIN=
AUTH0_AUDIENCE_ID=
AUTH0_API_SCOPE=
GA_TRACKING_ID=
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
es6: true,
},
extends: ['airbnb-base'],
globals: {
gtag: 'readonly',
},
plugins: ['jest', 'svelte3'],
rules: {
'import/prefer-default-export': 'off',
Expand Down
3 changes: 3 additions & 0 deletions glam/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
12 changes: 11 additions & 1 deletion glam/urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.urls import path
from django.views.generic.base import TemplateView

Expand All @@ -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"),
Expand Down
15 changes: 15 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id={{ GA_TRACKING_ID }}"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());

gtag('config', '{{ GA_TRACKING_ID }}');
</script>

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />

Expand Down
5 changes: 4 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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,
Expand Down
8 changes: 8 additions & 0 deletions src/routing/Router.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down

0 comments on commit a738a1c

Please sign in to comment.