Skip to content

Commit 8ce42f0

Browse files
committed
ENH: Support Google Analytics 4 tracking
1 parent bf256c6 commit 8ce42f0

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

pdoc/templates/config.mako

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
# Demo: https://highlightjs.org/static/demo/
3939
hljs_style = 'github'
4040
41-
# If set, insert Google Analytics tracking code. Value is GA
42-
# tracking id (UA-XXXXXX-Y).
41+
# If set, insert Google Analytics 4 tracking code. Value is GA
42+
# tracking id (G-XXXXXXXXXX).
4343
google_analytics = ''
4444
4545
# If set, insert Google Custom Search search bar widget above the sidebar index.

pdoc/templates/html.mako

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,13 @@
392392
<style media="print">${css.print()}</style>
393393

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

401404
% if google_search_query:

pdoc/test/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,10 @@ def test_output_text(self):
433433
for file in self.PUBLIC_FILES])
434434

435435
def test_google_analytics(self):
436-
expected = ['google-analytics.com']
436+
expected = ['googletagmanager.com']
437437
with run_html(EXAMPLE_MODULE):
438438
self._check_files((), exclude_patterns=expected)
439-
with run_html(EXAMPLE_MODULE, config='google_analytics="UA-xxxxxx-y"'):
439+
with run_html(EXAMPLE_MODULE, config='google_analytics="G-xxxxxxxxxx"'):
440440
self._check_files(expected)
441441

442442
def test_relative_dir_path(self):

0 commit comments

Comments
 (0)