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

Support the new Google analytics gtag.js #7691

Merged
merged 4 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions media/javascript/readthedocs-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,40 @@ if (navigator.doNotTrack === '1') {
console.log('Respecting DNT with respect to analytics...');
} else {
if (typeof READTHEDOCS_DATA !== 'undefined' && READTHEDOCS_DATA.global_analytics_code) {
// RTD Analytics Code
(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');
(function () {
// New Google Site Tag (gtag.js) tagging/analytics framework
// https://developers.google.com/gtagjs
var script = document.createElement("script");
script.src = "https://www.googletagmanager.com/gtag/js?id=" + READTHEDOCS_DATA.global_analytics_code;
script.type = "text/javascript";
script.async = true;
document.getElementsByTagName("head")[0].appendChild(script);
}())

ga('create', READTHEDOCS_DATA.global_analytics_code, 'auto', 'rtfd', {
'cookieExpires': 30 * 24 * 60 * 60
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

// Setup the RTD global analytics code and send a pageview
gtag('config', READTHEDOCS_DATA.global_analytics_code, {
'anonymize_ip': true,
'cookie_expires': 30 * 24 * 60 * 60, // 30 days
'dimension1': READTHEDOCS_DATA.project,
'dimension2': READTHEDOCS_DATA.version,
'dimension3': READTHEDOCS_DATA.language,
'dimension4': READTHEDOCS_DATA.theme,
'dimension5': READTHEDOCS_DATA.programming_language,
'dimension6': READTHEDOCS_DATA.builder,
'groups': 'rtfd'
});
ga('rtfd.set', 'dimension1', READTHEDOCS_DATA.project);
ga('rtfd.set', 'dimension2', READTHEDOCS_DATA.version);
ga('rtfd.set', 'dimension3', READTHEDOCS_DATA.language);
ga('rtfd.set', 'dimension4', READTHEDOCS_DATA.theme);
ga('rtfd.set', 'dimension5', READTHEDOCS_DATA.programming_language);
ga('rtfd.set', 'dimension6', READTHEDOCS_DATA.builder);
ga('rtfd.set', 'anonymizeIp', true);
ga('rtfd.send', 'pageview');

// User Analytics Code
// Setup the project (user) analytics code and send a pageview
if (READTHEDOCS_DATA.user_analytics_code) {
ga('create', READTHEDOCS_DATA.user_analytics_code, 'auto', 'user', {
'cookieExpires': 30 * 24 * 60 * 60
gtag('config', READTHEDOCS_DATA.user_analytics_code, {
'anonymize_ip': true,
'cookie_expires': 30 * 24 * 60 * 60 // 30 days
});
ga('user.set', 'anonymizeIp', true);
ga('user.send', 'pageview');
}
// End User Analytics Code
}
// end RTD Analytics Code
}
13 changes: 10 additions & 3 deletions readthedocs/core/static-src/core/js/doc-embed/sphinx.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ function init() {
$(document).on('click', "[data-toggle='rst-current-version']", function () {
var flyout_state = $("[data-toggle='rst-versions']").hasClass('shift-up') ? 'was_open' : 'was_closed';

// This needs to handle both old style legacy analytics for previously built docs
// as well as the newer universal analytics
if (typeof ga !== 'undefined') {
// This needs to handle old style legacy analytics for previously built docs
// as well as the newer universal analytics and Google Site Tag
if (typeof gtag !== 'undefined') {
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
gtag('event', 'Click', {
'event_category': 'Flyout',
'event_label': flyout_state,
'send_to': 'rtfd'
});
} else if (typeof ga !== 'undefined') {
ga('rtfd.send', 'event', 'Flyout', 'Click', flyout_state);
} else if (typeof _gaq !== 'undefined') {
_gaq.push(
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/core/static/core/js/readthedocs-doc-embed.js

Large diffs are not rendered by default.

38 changes: 25 additions & 13 deletions readthedocs/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,48 @@
<!-- title -->
<title>{% block title %}{% endblock %}{% block head_title %}{% endblock %} | {% block branding %}Read the Docs {% endblock %}</title>

{% if GLOBAL_ANALYTICS_CODE %}
<!-- Google Analytics -->
<script>
if ({{ DO_NOT_TRACK_ENABLED | lower }} && navigator.doNotTrack === '1') {
console.log('Respecting DNT with respect to analytics...');
} else {
// For more details on analytics at Read the Docs, please see:
// https://docs.readthedocs.io/en/latest/advertising-details.html#analytics
(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', '{{ GLOBAL_ANALYTICS_CODE }}', 'auto', 'rtfd', {
'cookieExpires': 30 * 24 * 60 * 60
(function () {
// New Google Site Tag (gtag.js) tagging/analytics framework
// https://developers.google.com/gtagjs
var script = document.createElement("script");
script.src = "https://www.googletagmanager.com/gtag/js?id={{ GLOBAL_ANALYTICS_CODE }}";
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());

// Setup the RTD global analytics code and send a pageview
gtag('config', '{{ GLOBAL_ANALYTICS_CODE }}', {
'anonymize_ip': true,
'cookie_expires': 30 * 24 * 60 * 60, // 30 days
'groups': 'rtfd'
});
ga('rtfd.set', 'anonymizeIp', true);
ga('rtfd.send', 'pageview');

{% if DASHBOARD_ANALYTICS_CODE %}
// Dashboard Analytics Code
ga('create', '{{ DASHBOARD_ANALYTICS_CODE }}', 'auto', 'user', {
'cookieExpires': 30 * 24 * 60 * 60
gtag('config', '{{ DASHBOARD_ANALYTICS_CODE }}', {
'anonymize_ip': true,
'cookie_expires': 30 * 24 * 60 * 60, // 30 days
'groups': 'dashboard'
});
ga('user.set', 'anonymizeIp', true);
ga('user.send', 'pageview');
// End Dashboard Analytics Code
{% endif %}
}
</script>
<!-- End Google Analytics -->
{% endif %}

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