Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for gtag #1129

Merged
merged 8 commits into from Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 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 @@ -97,7 +97,7 @@ Miscellaneous options
.. confval:: analytics_id

If specified, Google Analytics' javascript is included in your pages.
Blendify marked this conversation as resolved.
Show resolved Hide resolved
Set the value to the ID provided to you by google (like ``UA-XXXXXXX``).
Set the value to the ID provided to you by google (like ``UA-XXXXXXX`` or ``G-XXXXXXXXXX``).

:type: string

Expand Down
27 changes: 17 additions & 10 deletions sphinx_rtd_theme/layout.html
Expand Up @@ -220,16 +220,23 @@
{%- if theme_analytics_id %}
<!-- Theme Analytics -->
<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');
(function () {
// New Google Site Tag (gtag.js) tagging/analytics framework
// https://developers.google.com/gtagjs
var script = document.createElement("script");
ThomasTNO marked this conversation as resolved.
Show resolved Hide resolved
script.src = "https://www.googletagmanager.com/gtag/js?id={{ theme_analytics_id }}";
script.type = "text/javascript";
script.async = true;
document.getElementsByTagName("head")[0].appendChild(script);
}())

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