diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 7c959709..343e0c6b 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -7,6 +7,10 @@ "message": "Add Style", "description": "Title of the page for adding styles" }, + "analyticsEnabled": { + "message": "Send anonymous data to Stylish developers for determining user counts", + "description": "Label for the enable analytics setting" + }, "appliesAdd": { "message": "Add", "description": "Label for the button to add an 'applies' entry" diff --git a/analytics.js b/analytics.js index b30f6115..f1b05512 100644 --- a/analytics.js +++ b/analytics.js @@ -2,8 +2,27 @@ var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-8246384-4']); _gaq.push(['_trackPageview']); -(function() { - var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; - ga.src = 'https://ssl.google-analytics.com/ga.js'; - var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); -})(); +function isAnalyticsEnabled() { + try { + return chrome.extension.getBackgroundPage().prefs.get("analyticsEnabled"); + } catch (ex) { + console.log(ex); + return true; + } +} + +// Prefs seems not available immediately +setTimeout(function() { + if (isAnalyticsEnabled()) { + (function() { + var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; + ga.src = 'https://ssl.google-analytics.com/ga.js'; + var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); + })(); + setInterval(function() { + if (isAnalyticsEnabled()) { + _gaq.push(['_trackPageview']); + } + }, 1000 * 60 * 60 * 24); + } +}, 1000); diff --git a/manage.html b/manage.html index 8cfadee9..d10ee2ad 100644 --- a/manage.html +++ b/manage.html @@ -183,8 +183,9 @@