diff --git a/media/javascript/readthedocs-analytics.js b/media/javascript/readthedocs-analytics.js new file mode 100644 index 00000000000..e04090308f1 --- /dev/null +++ b/media/javascript/readthedocs-analytics.js @@ -0,0 +1,50 @@ +// Google Analytics is a contentious issue inside Read the Docs and in our community. +// Some users are very sensitive and privacy conscious to usage of GA. +// Other users want their own GA tracker on their docs to see the usage their docs get. +// The developers at Read the Docs understand that different users have different priorities +// and we try to respect the different viewpoints as much as possible while also accomplishing +// our own goals. + +// Read the Docs largely funds our operations and development through advertising and +// advertisers ask us questions that are easily answered with an analytics solution like +// "how many users do you have in Switzerland browsing Python docs?". We need to be able +// to easily get this data. We also use data from GA for some development decisions such +// as what browsers to support (or not) or how much usage a particular page/feature gets. + +// We have taken steps with GA to address some of the privacy issues. +// Read the Docs instructs Google to anonymize IPs sent to them before they are stored (see below). + +// We are always exploring our options with respect to analytics and if you would like +// to discuss further, feel free to open an issue on github. + + +// RTD Analytics Code + +(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ +(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), +m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) +})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); + +if (typeof READTHEDOCS_DATA !== 'undefined') { + if (READTHEDOCS_DATA.global_analytics_code) { + ga('create', READTHEDOCS_DATA.global_analytics_code, 'auto', 'rtfd'); + ga('rtfd.set', 'dimension1', READTHEDOCS_DATA.project); + ga('rtfd.set', 'dimension2', READTHEDOCS_DATA.version); + ga('rtfd.set', 'dimension3', READTHEDOCS_DATA.language); + ga('rtfd.set', 'dimension4', READTHEDOCS_DATA.theme); + ga('rtfd.set', 'dimension5', READTHEDOCS_DATA.programming_language); + ga('rtfd.set', 'dimension6', READTHEDOCS_DATA.builder); + ga('rtfd.set', 'anonymizeIp', true); + ga('rtfd.send', 'pageview'); + } + + // User Analytics Code + if (READTHEDOCS_DATA.user_analytics_code) { + ga('create', READTHEDOCS_DATA.user_analytics_code, 'auto', 'user'); + ga('user.set', 'anonymizeIp', true); + ga('user.send', 'pageview'); + } + // End User Analytics Code +} + +// end RTD Analytics Code diff --git a/readthedocs/doc_builder/backends/mkdocs.py b/readthedocs/doc_builder/backends/mkdocs.py index dfb5cda8b82..b84b0ba3cdc 100644 --- a/readthedocs/doc_builder/backends/mkdocs.py +++ b/readthedocs/doc_builder/backends/mkdocs.py @@ -88,8 +88,8 @@ def append_conf(self, **__): media_url = get_absolute_media_url() user_config.setdefault('extra_javascript', []).extend([ 'readthedocs-data.js', - 'readthedocs-dynamic-include.js', - '%sstatic/core/js/readthedocs-doc-embed.js' % media_url + '%sstatic/core/js/readthedocs-doc-embed.js' % media_url, + '%sjavascript/readthedocs-analytics.js' % media_url, ]) user_config.setdefault('extra_css', []).extend([ '%scss/badge_only.css' % media_url, @@ -112,10 +112,6 @@ def append_conf(self, **__): with open(os.path.join(docs_path, 'readthedocs-data.js'), 'w') as f: f.write(rtd_data) - dynamic_include = self.generate_dynamic_include() - with open(os.path.join(docs_path, 'readthedocs-dynamic-include.js'), 'w') as f: - f.write(dynamic_include) - def generate_rtd_data(self, docs_dir): """Generate template properties and render readthedocs-data.js.""" # Will be available in the JavaScript as READTHEDOCS_DATA. @@ -131,6 +127,8 @@ def generate_rtd_data(self, docs_dir): 'source_suffix': ".md", 'api_host': getattr(settings, 'PUBLIC_API_URL', 'https://readthedocs.org'), 'commit': self.version.project.vcs_repo(self.version.slug).commit, + 'global_analytics_code': getattr(settings, 'GLOBAL_ANALYTICS_CODE', 'UA-17997319-1'), + 'user_analytics_code': self.version.project.analytics_code, } data_json = json.dumps(readthedocs_data, indent=4) data_ctx = { @@ -143,14 +141,6 @@ def generate_rtd_data(self, docs_dir): tmpl = template_loader.get_template('doc_builder/data.js.tmpl') return tmpl.render(data_ctx) - def generate_dynamic_include(self): - include_ctx = { - 'global_analytics_code': getattr(settings, 'GLOBAL_ANALYTICS_CODE', 'UA-17997319-1'), - 'user_analytics_code': self.version.project.analytics_code, - } - tmpl = template_loader.get_template('doc_builder/include.js.tmpl') - return tmpl.render(include_ctx) - def build(self): checkout_path = self.project.checkout_path(self.version.slug) build_command = [ diff --git a/readthedocs/doc_builder/templates/doc_builder/include.js.tmpl b/readthedocs/doc_builder/templates/doc_builder/include.js.tmpl deleted file mode 100644 index bc15165fa24..00000000000 --- a/readthedocs/doc_builder/templates/doc_builder/include.js.tmpl +++ /dev/null @@ -1,27 +0,0 @@ -// RTD Analytics Code - -(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ -(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), -m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) -})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); - -ga('create', '{{ global_analytics_code }}', 'auto', 'rtfd'); -ga('rtfd.set', 'dimension1', READTHEDOCS_DATA.project); -ga('rtfd.set', 'dimension2', READTHEDOCS_DATA.version); -ga('rtfd.set', 'dimension3', READTHEDOCS_DATA.language); -ga('rtfd.set', 'dimension4', READTHEDOCS_DATA.theme); -ga('rtfd.set', 'dimension5', READTHEDOCS_DATA.programming_language); -ga('rtfd.set', 'dimension6', READTHEDOCS_DATA.builder); -ga('rtfd.set', 'anonymizeIp', true); -ga('rtfd.send', 'pageview'); - -{% if user_analytics_code %} -// User Analytics Code -ga('create', '{{ user_analytics_code }}', 'auto', 'user'); -ga('user.set', 'anonymizeIp', true); -ga('user.send', 'pageview'); -// End User Analytics Code -{% endif %} - -// end RTD Analytics Code -