Skip to content

Commit

Permalink
Added support for gtag (#1129)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ThomasTNO and Blendify committed Jun 18, 2021
1 parent a2997e8 commit ef9a058
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
8 changes: 5 additions & 3 deletions docs/configuring.rst
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 8 additions & 10 deletions sphinx_rtd_theme/layout.html
Expand Up @@ -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');

ga('create', '{{ theme_analytics_id }}', 'auto');
{%- if theme_analytics_anonymize_ip|tobool %}
ga('set', 'anonymizeIp', true);
{%- endif %}
ga('send', 'pageview');
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '{{ theme_analytics_id }}', {
'anonymize_ip': {{ 'true' if theme_analytics_anonymize_ip|tobool else 'false' }},
});
</script>

{%- endif %}
Expand Down

0 comments on commit ef9a058

Please sign in to comment.