Skip to content

Commit

Permalink
Support new+old Google analytics (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioRosado committed Apr 25, 2021
1 parent b5e9741 commit c249adb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
35 changes: 34 additions & 1 deletion pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from sphinx import addnodes

import jinja2

from bs4 import BeautifulSoup as bs

from .bootstrap_html_translator import BootstrapHTML5Translator
Expand Down Expand Up @@ -257,11 +256,45 @@ def navbar_align_class():
)
return align_options[align]

def generate_google_analytics_script(id):
"""Handle the two types of google analytics id."""
if id:
if "G-" in id:
script = f"""
<script
async
src='https://www.googletagmanager.com/gtag/js?id={id}'
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){{ dataLayer.push(arguments); }}
gtag('config', '{id}');
</script>
"""
else:
script = f"""
<script
async
src='https://www.google-analytics.com/analytics.js'
></script>
<script>
window.ga = window.ga || function () {{
(ga.q = ga.q || []).push(arguments) }};
ga.l = +new Date;
ga('create', '{id}', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
"""
soup = bs(script, "html.parser")
return soup

context["generate_nav_html"] = generate_nav_html
context["generate_toc_html"] = generate_toc_html
context["get_nav_object"] = get_nav_object
context["get_page_toc_object"] = get_page_toc_object
context["navbar_align_class"] = navbar_align_class
context["generate_google_analytics_script"] = generate_google_analytics_script


def _add_collapse_checkboxes(soup):
Expand Down
18 changes: 4 additions & 14 deletions pydata_sphinx_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,14 @@
</div>
</div>

{%- block scripts_end %}
{{ _webpack.body_post() }}

{% if theme_google_analytics_id %}
<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', '{{ theme_google_analytics_id }}', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->
{% endif %}
{%- block scripts_end %}
{{ _webpack.body_post() }}
{{ generate_google_analytics_script(id=theme_google_analytics_id) }}
{%- endblock %}

{%- endblock %}

{%- block footer %}
{%- include "footer.html" %}
{%- endblock %}
{%- endblock %}
35 changes: 29 additions & 6 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
from pathlib import Path
from shutil import copytree

import pytest
import sphinx.errors
from bs4 import BeautifulSoup

from sphinx.testing.util import SphinxTestApp
from sphinx.testing.path import path as sphinx_path
import sphinx.errors

import pytest

from sphinx.testing.util import SphinxTestApp

path_tests = Path(__file__).parent

Expand Down Expand Up @@ -385,3 +382,29 @@ def test_edit_page_url(sphinx_build_factory, html_context, edit_url):
edit_link = index_html.select(".editthispage a")
assert edit_link, "no edit link found"
assert edit_link[0].attrs["href"] == edit_url, f"edit link didn't match {edit_link}"


def test_new_google_analytics_id(sphinx_build_factory):
confoverrides = {"html_theme_options.google_analytics_id": "G-XXXXX"}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides)
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]

assert "gtag" in script_tag.string
assert "G-XXXXX" in script_tag.string


def test_old_google_analytics_id(sphinx_build_factory):
confoverrides = {"html_theme_options.google_analytics_id": "UA-XXXXX"}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides)
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]

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

0 comments on commit c249adb

Please sign in to comment.