Skip to content

Commit

Permalink
Merge pull request #4 from OlivierCodol/master
Browse files Browse the repository at this point in the history
Updated tag content to match new GA instructions
  • Loading branch information
jelmer authored Aug 9, 2022
2 parents ea301a0 + 8b78d5f commit 42b3df9
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions sphinxcontrib/googleanalytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,31 @@

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
metatags += """
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=%s"></script>""" % app.config.googleanalytics_id
metatags += """
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '%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')
Expand Down

0 comments on commit 42b3df9

Please sign in to comment.