Skip to content

Commit ef9a058

Browse files
ThomasTNOBlendify
andauthored
Added support for gtag (#1129)
* Added support for gtag * Fix missing curly braces * Simplify gtag script import * Fix tag * Add gtag to docs Co-authored-by: Aaron Carlisle <[email protected]>
1 parent a2997e8 commit ef9a058

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

docs/configuring.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ For example:
1313
.. code:: python
1414
1515
html_theme_options = {
16-
'analytics_id': 'UA-XXXXXXX-1', # Provided by Google in your dashboard
16+
'analytics_id': 'G-XXXXXXXXXX', # Provided by Google in your dashboard
1717
'analytics_anonymize_ip': False,
1818
'logo_only': False,
1919
'display_version': True,
@@ -96,11 +96,13 @@ Miscellaneous options
9696

9797
.. confval:: analytics_id
9898

99-
If specified, Google Analytics' javascript is included in your pages.
100-
Set the value to the ID provided to you by google (like ``UA-XXXXXXX``).
99+
If specified, Google Analytics' `gtag.js`_ is included in your pages.
100+
Set the value to the ID provided to you by google (like ``UA-XXXXXXX`` or ``G-XXXXXXXXXX``).
101101

102102
:type: string
103103

104+
.. _gtag.js: https://developers.google.com/gtagjs
105+
104106
.. confval:: analytics_anonymize_ip
105107

106108
Anonymize visitor IP addresses in Google Analytics.

sphinx_rtd_theme/layout.html

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,15 @@
219219
{%- if not READTHEDOCS %}
220220
{%- if theme_analytics_id %}
221221
<!-- Theme Analytics -->
222+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ theme_analytics_id }}"></script>
222223
<script>
223-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
224-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
225-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
226-
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
227-
228-
ga('create', '{{ theme_analytics_id }}', 'auto');
229-
{%- if theme_analytics_anonymize_ip|tobool %}
230-
ga('set', 'anonymizeIp', true);
231-
{%- endif %}
232-
ga('send', 'pageview');
224+
window.dataLayer = window.dataLayer || [];
225+
function gtag(){dataLayer.push(arguments);}
226+
gtag('js', new Date());
227+
228+
gtag('config', '{{ theme_analytics_id }}', {
229+
'anonymize_ip': {{ 'true' if theme_analytics_anonymize_ip|tobool else 'false' }},
230+
});
233231
</script>
234232

235233
{%- endif %}

0 commit comments

Comments
 (0)