Skip to content

Commit 102ff2f

Browse files
davidfischerstsewd
andauthored
Support the new Google analytics gtag.js (#7691)
- This applies to docs and to the dashboard - This is backwards compatible with the old UA-XXX properties assuming nothing custom is going on - Supports the new G-XXXX properties - Continues to support disabling analytics and do not track - Sends custom dimensions like before - Shorter cookie expiry like before Co-authored-by: Santos Gallegos <[email protected]>
1 parent 729a347 commit 102ff2f

File tree

4 files changed

+64
-38
lines changed

4 files changed

+64
-38
lines changed

media/javascript/readthedocs-analytics.js

+28-21
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,40 @@ if (navigator.doNotTrack === '1') {
77
console.log('Respecting DNT with respect to analytics...');
88
} else {
99
if (typeof READTHEDOCS_DATA !== 'undefined' && READTHEDOCS_DATA.global_analytics_code) {
10-
// RTD Analytics Code
11-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
12-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
13-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
14-
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
10+
(function () {
11+
// New Google Site Tag (gtag.js) tagging/analytics framework
12+
// https://developers.google.com/gtagjs
13+
var script = document.createElement("script");
14+
script.src = "https://www.googletagmanager.com/gtag/js?id=" + READTHEDOCS_DATA.global_analytics_code;
15+
script.type = "text/javascript";
16+
script.async = true;
17+
document.getElementsByTagName("head")[0].appendChild(script);
18+
}())
1519

16-
ga('create', READTHEDOCS_DATA.global_analytics_code, 'auto', 'rtfd', {
17-
'cookieExpires': 30 * 24 * 60 * 60
20+
window.dataLayer = window.dataLayer || [];
21+
function gtag(){dataLayer.push(arguments);}
22+
gtag('js', new Date());
23+
24+
// Setup the RTD global analytics code and send a pageview
25+
gtag('config', READTHEDOCS_DATA.global_analytics_code, {
26+
'anonymize_ip': true,
27+
'cookie_expires': 30 * 24 * 60 * 60, // 30 days
28+
'dimension1': READTHEDOCS_DATA.project,
29+
'dimension2': READTHEDOCS_DATA.version,
30+
'dimension3': READTHEDOCS_DATA.language,
31+
'dimension4': READTHEDOCS_DATA.theme,
32+
'dimension5': READTHEDOCS_DATA.programming_language,
33+
'dimension6': READTHEDOCS_DATA.builder,
34+
'groups': 'rtfd'
1835
});
19-
ga('rtfd.set', 'dimension1', READTHEDOCS_DATA.project);
20-
ga('rtfd.set', 'dimension2', READTHEDOCS_DATA.version);
21-
ga('rtfd.set', 'dimension3', READTHEDOCS_DATA.language);
22-
ga('rtfd.set', 'dimension4', READTHEDOCS_DATA.theme);
23-
ga('rtfd.set', 'dimension5', READTHEDOCS_DATA.programming_language);
24-
ga('rtfd.set', 'dimension6', READTHEDOCS_DATA.builder);
25-
ga('rtfd.set', 'anonymizeIp', true);
26-
ga('rtfd.send', 'pageview');
2736

28-
// User Analytics Code
37+
// Setup the project (user) analytics code and send a pageview
2938
if (READTHEDOCS_DATA.user_analytics_code) {
30-
ga('create', READTHEDOCS_DATA.user_analytics_code, 'auto', 'user', {
31-
'cookieExpires': 30 * 24 * 60 * 60
39+
gtag('config', READTHEDOCS_DATA.user_analytics_code, {
40+
'anonymize_ip': true,
41+
'cookie_expires': 30 * 24 * 60 * 60 // 30 days
3242
});
33-
ga('user.set', 'anonymizeIp', true);
34-
ga('user.send', 'pageview');
3543
}
36-
// End User Analytics Code
3744
}
3845
// end RTD Analytics Code
3946
}

readthedocs/core/static-src/core/js/doc-embed/sphinx.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ function init() {
1414
$(document).on('click', "[data-toggle='rst-current-version']", function () {
1515
var flyout_state = $("[data-toggle='rst-versions']").hasClass('shift-up') ? 'was_open' : 'was_closed';
1616

17-
// This needs to handle both old style legacy analytics for previously built docs
18-
// as well as the newer universal analytics
19-
if (typeof ga !== 'undefined') {
17+
// This needs to handle old style legacy analytics for previously built docs
18+
// as well as the newer universal analytics and Google Site Tag
19+
if (typeof gtag !== 'undefined') {
20+
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
21+
gtag('event', 'Click', {
22+
'event_category': 'Flyout',
23+
'event_label': flyout_state,
24+
'send_to': 'rtfd'
25+
});
26+
} else if (typeof ga !== 'undefined') {
2027
ga('rtfd.send', 'event', 'Flyout', 'Click', flyout_state);
2128
} else if (typeof _gaq !== 'undefined') {
2229
_gaq.push(

readthedocs/core/static/core/js/readthedocs-doc-embed.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readthedocs/templates/base.html

+25-13
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,48 @@
1515
<!-- title -->
1616
<title>{% block title %}{% endblock %}{% block head_title %}{% endblock %} | {% block branding %}Read the Docs {% endblock %}</title>
1717

18+
{% if GLOBAL_ANALYTICS_CODE %}
1819
<!-- Google Analytics -->
1920
<script>
2021
if ({{ DO_NOT_TRACK_ENABLED | lower }} && navigator.doNotTrack === '1') {
2122
console.log('Respecting DNT with respect to analytics...');
2223
} else {
2324
// For more details on analytics at Read the Docs, please see:
2425
// https://docs.readthedocs.io/en/latest/advertising-details.html#analytics
25-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
26-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
27-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
28-
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
29-
30-
ga('create', '{{ GLOBAL_ANALYTICS_CODE }}', 'auto', 'rtfd', {
31-
'cookieExpires': 30 * 24 * 60 * 60
26+
(function () {
27+
// New Google Site Tag (gtag.js) tagging/analytics framework
28+
// https://developers.google.com/gtagjs
29+
var script = document.createElement("script");
30+
script.src = "https://www.googletagmanager.com/gtag/js?id={{ GLOBAL_ANALYTICS_CODE }}";
31+
script.type = "text/javascript";
32+
script.async = true;
33+
document.getElementsByTagName("head")[0].appendChild(script);
34+
}())
35+
36+
window.dataLayer = window.dataLayer || [];
37+
function gtag(){dataLayer.push(arguments);}
38+
gtag('js', new Date());
39+
40+
// Setup the RTD global analytics code and send a pageview
41+
gtag('config', '{{ GLOBAL_ANALYTICS_CODE }}', {
42+
'anonymize_ip': true,
43+
'cookie_expires': 30 * 24 * 60 * 60, // 30 days
44+
'groups': 'rtfd'
3245
});
33-
ga('rtfd.set', 'anonymizeIp', true);
34-
ga('rtfd.send', 'pageview');
3546

3647
{% if DASHBOARD_ANALYTICS_CODE %}
3748
// Dashboard Analytics Code
38-
ga('create', '{{ DASHBOARD_ANALYTICS_CODE }}', 'auto', 'user', {
39-
'cookieExpires': 30 * 24 * 60 * 60
49+
gtag('config', '{{ DASHBOARD_ANALYTICS_CODE }}', {
50+
'anonymize_ip': true,
51+
'cookie_expires': 30 * 24 * 60 * 60, // 30 days
52+
'groups': 'dashboard'
4053
});
41-
ga('user.set', 'anonymizeIp', true);
42-
ga('user.send', 'pageview');
4354
// End Dashboard Analytics Code
4455
{% endif %}
4556
}
4657
</script>
4758
<!-- End Google Analytics -->
59+
{% endif %}
4860

4961
<!-- css -->
5062
<link rel="stylesheet" href="{% static 'css/core.css' %}">

0 commit comments

Comments
 (0)