Skip to content

Commit

Permalink
ENH: Support Google Analytics 4 tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Mar 11, 2024
1 parent bf256c6 commit 8ce42f0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pdoc/templates/config.mako
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
# Demo: https://highlightjs.org/static/demo/
hljs_style = 'github'
# If set, insert Google Analytics tracking code. Value is GA
# tracking id (UA-XXXXXX-Y).
# If set, insert Google Analytics 4 tracking code. Value is GA
# tracking id (G-XXXXXXXXXX).
google_analytics = ''
# If set, insert Google Custom Search search bar widget above the sidebar index.
Expand Down
9 changes: 6 additions & 3 deletions pdoc/templates/html.mako
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,13 @@
<style media="print">${css.print()}</style>

% if google_analytics:
<script async src="https://www.googletagmanager.com/gtag/js?id=${google_analytics}"></script>
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', '${google_analytics}', 'auto'); ga('send', 'pageview');
</script><script async src='https://www.google-analytics.com/analytics.js'></script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${google_analytics}');
</script>
% endif

% if google_search_query:
Expand Down
4 changes: 2 additions & 2 deletions pdoc/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,10 @@ def test_output_text(self):
for file in self.PUBLIC_FILES])

def test_google_analytics(self):
expected = ['google-analytics.com']
expected = ['googletagmanager.com']
with run_html(EXAMPLE_MODULE):
self._check_files((), exclude_patterns=expected)
with run_html(EXAMPLE_MODULE, config='google_analytics="UA-xxxxxx-y"'):
with run_html(EXAMPLE_MODULE, config='google_analytics="G-xxxxxxxxxx"'):
self._check_files(expected)

def test_relative_dir_path(self):
Expand Down

0 comments on commit 8ce42f0

Please sign in to comment.