generated from DavidLeoni/jupman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleanalytics.py
38 lines (30 loc) · 1.39 KB
/
googleanalytics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# DAVID LEONI, July 2020:
# COPY-PASTED FROM ORIGINAL AND PATCHED,
# SEE https://github.com/DavidLeoni/jupman/issues/48
from sphinx.errors import ExtensionError
def add_ga_javascript(app, pagename, templatename, context, doctree):
if not app.config.googleanalytics_enabled:
return
metatags = context.get('metatags', '')
metatags += """<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '%s']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>""" % app.config.googleanalytics_id
context['metatags'] = metatags
def check_config(app):
if not app.config.googleanalytics_id:
raise ExtensionError("'googleanalytics_id' config value must be set for ga statistics to function properly.")
def setup(app):
app.add_config_value('googleanalytics_id', '', 'html')
app.add_config_value('googleanalytics_enabled', True, 'html')
app.connect('html-page-context', add_ga_javascript)
app.connect('builder-inited', check_config)
return app