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 <carlisle.b3d@gmail.com>
This commit is contained in:
ThomasTNOandAaron Carlisle authored and GitHub committed 2021-06-18 13:59:46 -04:00
1 parent a2997e8888
commit ef9a0584ef
2 files changed
+12 -12

No files matched your search

+5 -3
View File
@@ -13,7 +13,7 @@ For example:
.. code:: python
html_theme_options = {
'analytics_id': 'UA-XXXXXXX-1', # Provided by Google in your dashboard
'analytics_id': 'G-XXXXXXXXXX', # Provided by Google in your dashboard
'analytics_anonymize_ip': False,
'logo_only': False,
'display_version': True,
@@ -96,11 +96,13 @@ Miscellaneous options
.. confval:: analytics_id
If specified, Google Analytics' javascript is included in your pages.
Set the value to the ID provided to you by google (like ``UA-XXXXXXX``).
If specified, Google Analytics' `gtag.js`_ is included in your pages.
Set the value to the ID provided to you by google (like ``UA-XXXXXXX`` or ``G-XXXXXXXXXX``).
:type: string
.. _gtag.js: https://developers.google.com/gtagjs
.. confval:: analytics_anonymize_ip
Anonymize visitor IP addresses in Google Analytics.
+7 -9
View File
@@ -219,17 +219,15 @@
{%- if not READTHEDOCS %}
{%- if theme_analytics_id %}
<!-- Theme Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ theme_analytics_id }}"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
ga('create', '{{ theme_analytics_id }}', 'auto');
{%- if theme_analytics_anonymize_ip|tobool %}
ga('set', 'anonymizeIp', true);
{%- endif %}
ga('send', 'pageview');
gtag('config', '{{ theme_analytics_id }}', {
'anonymize_ip': {{ 'true' if theme_analytics_anonymize_ip|tobool else 'false' }},
});
</script>
{%- endif %}