Skip to content

Commit 2f82708

Browse files
author
Katie Jiang
committed
Add Google analytics
1 parent a62abce commit 2f82708

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

app/templates/partials/_head.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,22 @@
77

88
{% assets 'vendor_js' %}<script type="text/javascript" src="{{ ASSET_URL }}"></script>{% endassets %}
99
{% assets 'app_js' %}<script type="text/javascript" src="{{ ASSET_URL }}"></script>{% endassets %}
10+
11+
{% if config.GOOGLE_ANALYTICS_ID %}
12+
<!-- Google Analytics -->
13+
<script type="text/javascript">
14+
(function(i, s, o, g, r, a, m) {
15+
i['GoogleAnalyticsObject'] = r;
16+
i[r] = i[r] || function() {
17+
(i[r].q = i[r].q || []).push(arguments)
18+
}, i[r].l = 1 * new Date();
19+
a = s.createElement(o),
20+
m = s.getElementsByTagName(o)[0];
21+
a.async = 1;
22+
a.src = g;
23+
m.parentNode.insertBefore(a, m)
24+
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
25+
ga('create', '{{config.GOOGLE_ANALYTICS_ID}}', 'auto');
26+
ga('send', 'pageview');
27+
</script>
28+
{% endif %}

config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Config:
3535
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
3636
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
3737
MAIL_DEFAULT_SENDER = os.environ.get('MAIL_DEFAULT_SENDER')
38+
GOOGLE_ANALYTICS_ID = os.environ.get('GOOGLE_ANALYTICS_ID') or ''
3839

3940
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD') or 'password'
4041
ADMIN_EMAIL = os.environ.get(

docs/google_analytics.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Adding Google Analytics
2+
3+
## Setup
4+
5+
Including Google analytics to your flask-base app is incredibly easy. Simply add an environment variable named `GOOGLE_ANALYTICS_ID` to a local file called `.env`.
6+
7+
A Google analytics ID can be obtained by signing up [here](https://analytics.google.com/analytics/web). The ID should be of the form `UA-XXXXXX-X`.
8+
9+
For more information about using Google analytics, check out their [help center](https://support.google.com/analytics#topic=3544906).

0 commit comments

Comments
 (0)