Skip to content

Commit 05aac45

Browse files
author
Katie Jiang
authored
Merge pull request hack4impact#147 from hack4impact/google-analytics
Add analytics
2 parents a62abce + e8b4393 commit 05aac45

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

app/templates/partials/_head.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,32 @@
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 %}
29+
30+
{% if config.SEGMENT_API_KEY %}
31+
<!-- Segment Analytics -->
32+
<script>
33+
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="4.0.0";
34+
analytics.load('{{ config.SEGMENT_API_KEY }}');
35+
analytics.page();
36+
}}();
37+
</script>
38+
{% endif %}

config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Config:
2828
print('SECRET KEY ENV VAR NOT SET! SHOULD NOT SEE IN PRODUCTION')
2929
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
3030

31+
# Email
3132
MAIL_SERVER = os.environ.get('MAIL_SERVER') or 'smtp.sendgrid.net'
3233
MAIL_PORT = os.environ.get('MAIL_PORT') or 587
3334
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') or True
@@ -36,6 +37,11 @@ class Config:
3637
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
3738
MAIL_DEFAULT_SENDER = os.environ.get('MAIL_DEFAULT_SENDER')
3839

40+
# Analytics
41+
GOOGLE_ANALYTICS_ID = os.environ.get('GOOGLE_ANALYTICS_ID') or ''
42+
SEGMENT_API_KEY = os.environ.get('SEGMENT_API_KEY') or ''
43+
44+
# Admin account
3945
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD') or 'password'
4046
ADMIN_EMAIL = os.environ.get(
4147
'ADMIN_EMAIL') or 'flask-base-admin@example.com'

docs/analytics.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Adding Analytics
2+
3+
## Google Analytics
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).
10+
11+
## Segment
12+
Including Segment to your flask-base app requires you to add an environment variable named `SEGMENT_API_KEY` to a local file called `.env`.
13+
14+
To obtain a Segment API key, you will first need to [sign up](https://app.segment.com). Then, add a Javascript source and navigate to "Settings" to get a write key.
15+
16+
For more information about using Segment, check out their [help center](https://segment.com/help).

0 commit comments

Comments
 (0)