Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #554: Add Google Analytics support #560

Merged
merged 1 commit into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they don't seem to document this anywhere though it gets described as "send a page hit to GA"


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 @@ -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,
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