Skip to content

Commit

Permalink
FIX: added gtag date and moved google analytics to header (#439)
Browse files Browse the repository at this point in the history
Co-authored-by: Jack Minchin <jack@thisisstratum.co.uk>
Co-authored-by: Chris Holdgraf <choldgraf@gmail.com>
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
  • Loading branch information
4 people authored Sep 16, 2021
1 parent 2c99105 commit 055ae27
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def generate_google_analytics_script(id):
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){{ dataLayer.push(arguments); }}
gtag('js', new Date());
gtag('config', '{id}');
</script>
"""
Expand Down
4 changes: 3 additions & 1 deletion pydata_sphinx_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<link rel="{{ favicon.rel }}" sizes="{{ favicon.sizes }}" href="{{ pathto('_static/' + favicon.href, 1) }}">
{% endif %}
{% endfor %}

<!-- Google Analytics -->
{{ generate_google_analytics_script(id=theme_google_analytics_id) }}
{%- endblock %}

{# Silence the sidebar's, relbar's #}
Expand Down Expand Up @@ -105,7 +108,6 @@

{%- block scripts_end %}
{{ _webpack.body_post() }}
{{ generate_google_analytics_script(id=theme_google_analytics_id) }}
{%- endblock %}

{%- endblock %}
Expand Down
8 changes: 4 additions & 4 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ def test_new_google_analytics_id(sphinx_build_factory):
sphinx_build.build()
index_html = sphinx_build.html_tree("index.html")
# This text makes the assumption that the google analytics will always be
# the last script tag found in the document.
script_tag = index_html.select("script")[-1]
# the second last script tag found in the document (last is the theme js).
script_tag = index_html.select("script")[-2]

assert "gtag" in script_tag.string
assert "G-XXXXX" in script_tag.string
Expand All @@ -403,8 +403,8 @@ def test_old_google_analytics_id(sphinx_build_factory):
sphinx_build.build()
index_html = sphinx_build.html_tree("index.html")
# This text makes the assumption that the google analytics will always be
# the one before last script tag found in the document.
script_tag = index_html.select("script")[-1]
# the second last script tag found in the document (last is the theme js).
script_tag = index_html.select("script")[-2]

assert "ga" in script_tag.string
assert "UA-XXXXX" in script_tag.string

0 comments on commit 055ae27

Please sign in to comment.