Skip to content

Commit 482d870

Browse files
authored
Merge pull request #979 from jmerle/cookies-consent
Cookie consent
2 parents ac50e99 + 5438143 commit 482d870

File tree

9 files changed

+77
-30
lines changed

9 files changed

+77
-30
lines changed

assets/javascripts/app/settings.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class app.Settings
55
'manualUpdate'
66
'fastScroll'
77
'arrowScroll'
8+
'analyticsConsent'
89
'docs'
910
'dark'
1011
'layout'
@@ -28,6 +29,7 @@ class app.Settings
2829
news: 0
2930
manualUpdate: false
3031
schema: 1
32+
analyticsConsent: false
3133

3234
constructor: ->
3335
@store = new CookieStore

assets/javascripts/lib/page.coffee

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,21 @@ page.track = (fn) ->
199199
return
200200

201201
track = ->
202-
tracker.call() for tracker in trackers
202+
consentGiven = Cookies.get('analyticsConsent')
203+
consentAsked = Cookies.get('analyticsConsentAsked')
204+
205+
if consentGiven == '1'
206+
tracker.call() for tracker in trackers
207+
else if consentGiven == undefined and consentAsked == undefined
208+
# Only ask for consent once per browser session
209+
Cookies.set('analyticsConsentAsked', '1')
210+
211+
new app.views.Notif 'AnalyticsConsent', autoHide: null
212+
return
213+
214+
@resetAnalytics = ->
215+
for cookie in document.cookie.split(/;\s?/)
216+
name = cookie.split('=')[0]
217+
if name[0] == '_'
218+
Cookies.expire(name)
203219
return

assets/javascripts/templates/notif_tmpl.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,9 @@ app.templates.notifShare = ->
6868
app.templates.notifUpdateDocs = ->
6969
textNotif """ Documentation updates available. """,
7070
""" <a href="/offline">Install them</a> as soon as possible to avoid broken pages. """
71+
72+
app.templates.notifAnalyticsConsent = ->
73+
textNotif """ Tracking cookies """,
74+
""" We would like to gather usage data about how DevDocs is used through Google Analytics and Gauges. We only collect anonymous traffic information.
75+
Please confirm if you accept our tracking cookies. You can always change your decision in the settings.
76+
<br><span class="_notif-right"><a href="#" data-behavior="accept-analytics">Accept</a> or <a href="#" data-behavior="decline-analytics">Decline</a></span> """

assets/javascripts/templates/pages/about_tmpl.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ app.templates.aboutPage = -> """
7373
<ul>
7474
<li><a href="https://devdocs.io">devdocs.io</a> ("App") is operated by <a href="https://www.freecodecamp.org/">freeCodeCamp</a> ("We").
7575
<li>We do not collect personal information through the app.
76-
<li>We use Google Analytics, Gauges and Sentry to collect anonymous traffic information and improve the app.
76+
<li>We use Google Analytics and Gauges to collect anonymous traffic information if you have given consent to this. You can change your decision in the <a href="/settings">settings</a>.
77+
<li>We use Sentry to collect crash data and improve the app.
7778
<li>The app uses cookies to store user preferences.
7879
<li>By using the app, you signify your acceptance of this policy. If you do not agree to this policy, please do not use the app.
7980
<li>If you have any questions regarding privacy, please email <a href="mailto:[email protected]">[email protected]</a>.

assets/javascripts/templates/pages/settings_tmpl.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ app.templates.settingsPage = (settings) -> """
1515
<input type="checkbox" form="settings" name="layout" value="_sidebar-hidden"#{if settings['_sidebar-hidden'] then ' checked' else ''}>Automatically hide and show the sidebar
1616
<small>Tip: drag the edge of the sidebar to resize it.</small>
1717
</label>
18+
<label class="_settings-label">
19+
<input type="checkbox" form="settings" name="analyticsConsent"#{if settings.analyticsConsent then ' checked' else ''}>Enable tracking cookies
20+
<small>With this checked, we enable Google Analytics and Gauges to collect anonymous traffic information.</small>
21+
</label>
1822
</div>
1923
</div>
2024

assets/javascripts/tracking.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
try {
2-
if (app.config.env == 'production') {
3-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
4-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
5-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
6-
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
7-
ga('create', 'UA-5544833-12', 'devdocs.io');
8-
page.track(function() {
9-
ga('send', 'pageview', {
10-
page: location.pathname + location.search + location.hash,
11-
dimension1: app.router.context && app.router.context.doc && app.router.context.doc.slug_without_version
2+
if (app.config.env === 'production') {
3+
if (Cookies.get('analyticsConsent') === '1') {
4+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
5+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
6+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
7+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
8+
ga('create', 'UA-5544833-12', 'devdocs.io');
9+
page.track(function() {
10+
ga('send', 'pageview', {
11+
page: location.pathname + location.search + location.hash,
12+
dimension1: app.router.context && app.router.context.doc && app.router.context.doc.slug_without_version
13+
});
1214
});
13-
});
1415

15-
page.track(function() {
16-
if (window._gauges)
17-
_gauges.push(['track']);
18-
else
19-
(function() {
20-
var _gauges=_gauges||[];!function(){var a=document.createElement("script");
21-
a.type="text/javascript",a.async=!0,a.id="gauges-tracker",
22-
a.setAttribute("data-site-id","51c15f82613f5d7819000067"),
23-
a.src="https://secure.gaug.es/track.js";var b=document.getElementsByTagName("script")[0];
24-
b.parentNode.insertBefore(a,b)}();
25-
})();
26-
});
16+
page.track(function() {
17+
if (window._gauges)
18+
_gauges.push(['track']);
19+
else
20+
(function() {
21+
var _gauges=_gauges||[];!function(){var a=document.createElement("script");
22+
a.type="text/javascript",a.async=!0,a.id="gauges-tracker",
23+
a.setAttribute("data-site-id","51c15f82613f5d7819000067"),
24+
a.src="https://secure.gaug.es/track.js";var b=document.getElementsByTagName("script")[0];
25+
b.parentNode.insertBefore(a,b)}();
26+
})();
27+
});
28+
} else {
29+
resetAnalytics();
30+
}
2731
}
2832
} catch(e) { }

assets/javascripts/views/content/settings_page.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class app.views.SettingsPage extends app.View
1414
settings.dark = app.settings.get('dark')
1515
settings.smoothScroll = !app.settings.get('fastScroll')
1616
settings.arrowScroll = app.settings.get('arrowScroll')
17+
settings.analyticsConsent = app.settings.get('analyticsConsent')
1718
settings[layout] = app.settings.hasLayout(layout) for layout in app.settings.LAYOUTS
1819
settings
1920

@@ -32,6 +33,11 @@ class app.views.SettingsPage extends app.View
3233
app.settings.set('fastScroll', !enable)
3334
return
3435

36+
toggleAnalyticsConsent: (enable) ->
37+
app.settings.set('analyticsConsent', if enable then '1' else '0')
38+
resetAnalytics() unless enable
39+
return
40+
3541
toggle: (name, enable) ->
3642
app.settings.set(name, enable)
3743
return
@@ -75,6 +81,8 @@ class app.views.SettingsPage extends app.View
7581
@toggleSmoothScroll input.checked
7682
when 'import'
7783
@import input.files[0], input
84+
when 'analyticsConsent'
85+
@toggleAnalyticsConsent input.checked
7886
else
7987
@toggle input.name, input.checked
8088
return

assets/javascripts/views/layout/document.coffee

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ class app.views.Document extends app.View
7575
return unless target.hasAttribute('data-behavior')
7676
$.stopEvent(event)
7777
switch target.getAttribute('data-behavior')
78-
when 'back' then history.back()
79-
when 'reload' then window.location.reload()
80-
when 'reboot' then app.reboot()
81-
when 'hard-reload' then app.reload()
82-
when 'reset' then app.reset() if confirm('Are you sure you want to reset DevDocs?')
78+
when 'back' then history.back()
79+
when 'reload' then window.location.reload()
80+
when 'reboot' then app.reboot()
81+
when 'hard-reload' then app.reload()
82+
when 'reset' then app.reset() if confirm('Are you sure you want to reset DevDocs?')
83+
when 'accept-analytics' then Cookies.set('analyticsConsent', '1') && app.reboot()
84+
when 'decline-analytics' then Cookies.set('analyticsConsent', '0') && app.reboot()
8385
return

assets/stylesheets/components/_notif.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,7 @@
134134

135135
._notif-info { color: var(--textColorLight); }
136136
}
137+
138+
._notif-right {
139+
float: right;
140+
}

0 commit comments

Comments
 (0)