Skip to content

Commit 2bc8084

Browse files
authored
Merge pull request #4013 from davidfischer/mkdocs-analytics-improvements
MkDocs projects use RTD's analytics privacy improvements
2 parents 08eed81 + 7e7a0b2 commit 2bc8084

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

readthedocs/doc_builder/backends/mkdocs.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,23 @@ def append_conf(self, **__):
100100
if 'theme_dir' not in user_config and self.use_theme:
101101
user_config['theme_dir'] = TEMPLATE_DIR
102102

103-
yaml.safe_dump(
104-
user_config,
105-
open(os.path.join(self.root_path, 'mkdocs.yml'), 'w')
106-
)
107-
108103
docs_path = os.path.join(self.root_path, docs_dir)
109104

110105
# RTD javascript writing
111106
rtd_data = self.generate_rtd_data(docs_dir=docs_dir, mkdocs_config=user_config)
112107
with open(os.path.join(docs_path, 'readthedocs-data.js'), 'w') as f:
113108
f.write(rtd_data)
114109

110+
# Use Read the Docs' analytics setup rather than mkdocs'
111+
# This supports using RTD's privacy improvements around analytics
112+
user_config['google_analytics'] = None
113+
114+
# Write the mkdocs configuration
115+
yaml.safe_dump(
116+
user_config,
117+
open(os.path.join(self.root_path, 'mkdocs.yml'), 'w')
118+
)
119+
115120
def generate_rtd_data(self, docs_dir, mkdocs_config):
116121
"""Generate template properties and render readthedocs-data.js."""
117122
# Get the theme name
@@ -120,6 +125,12 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
120125
if theme_dir:
121126
theme_name = theme_dir.rstrip('/').split('/')[-1]
122127

128+
# Use the analytics code from mkdocs.yml if it isn't set already by Read the Docs
129+
analytics_code = self.version.project.analytics_code
130+
if not analytics_code and mkdocs_config.get('google_analytics'):
131+
# http://www.mkdocs.org/user-guide/configuration/#google_analytics
132+
analytics_code = mkdocs_config['google_analytics'][0]
133+
123134
# Will be available in the JavaScript as READTHEDOCS_DATA.
124135
readthedocs_data = {
125136
'project': self.version.project.slug,
@@ -134,7 +145,7 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
134145
'api_host': getattr(settings, 'PUBLIC_API_URL', 'https://readthedocs.org'),
135146
'commit': self.version.project.vcs_repo(self.version.slug).commit,
136147
'global_analytics_code': getattr(settings, 'GLOBAL_ANALYTICS_CODE', 'UA-17997319-1'),
137-
'user_analytics_code': self.version.project.analytics_code,
148+
'user_analytics_code': analytics_code,
138149
}
139150
data_json = json.dumps(readthedocs_data, indent=4)
140151
data_ctx = {

0 commit comments

Comments
 (0)