From cec4f1885b436785b842e3f05c52848f2d911d59 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Tue, 16 Feb 2016 13:11:43 +0100 Subject: [PATCH] Set up tracking of outgoing links and actions for Google Analytics --- CHANGELOG | 3 +++ material/base.html | 13 ++++++++++++- src/base.html | 14 +++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 126aac1af..e11a0f812 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ mkdocs-material-0.x.x (2016-xx-xx) + * Fixed webfont delivery problem when hosted in subdirectories + * Added option to configure fonts in mkdocs.yml with fallbacks * Set download link to latest version if available + * Set up tracking of outgoing links and actions for Google Analytics mkdocs-material-0.1.1 (2016-02-11) diff --git a/material/base.html b/material/base.html index c3f4cf18c..5cd799b4a 100644 --- a/material/base.html +++ b/material/base.html @@ -154,10 +154,21 @@ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g; m.parentNode.insertBefore(a,m) })(window, document, - 'script', '//www.google-analytics.com/analytics.js','ga'); + 'script', '//www.google-analytics.com/analytics.js', 'ga'); + /* General initialization */ ga('create', '{{ google_analytics[0] }}', '{{ google_analytics[1] }}'); ga('set', 'anonymizeIp', true); ga('send', 'pageview'); + /* Track outbound links */ + var buttons = document.querySelectorAll('a'); + Array.prototype.map.call(buttons, function(item) { + if (item.host != window.location.host) { + item.addEventListener('click', function() { + var action = item.getAttribute('data-action') || 'follow'; + ga('send', 'event', 'outbound', action, item.href); + }); + } + }); {% endif %} diff --git a/src/base.html b/src/base.html index 26e88d27e..bfdbe1f71 100644 --- a/src/base.html +++ b/src/base.html @@ -237,11 +237,23 @@ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g; m.parentNode.insertBefore(a,m) })(window, document, - 'script', '//www.google-analytics.com/analytics.js','ga'); + 'script', '//www.google-analytics.com/analytics.js', 'ga'); + /* General initialization */ ga('create', '{{ google_analytics[0] }}', '{{ google_analytics[1] }}'); ga('set', 'anonymizeIp', true); ga('send', 'pageview'); + + /* Track outbound links */ + var buttons = document.querySelectorAll('a'); + Array.prototype.map.call(buttons, function(item) { + if (item.host != window.location.host) { + item.addEventListener('click', function() { + var action = item.getAttribute('data-action') || 'follow'; + ga('send', 'event', 'outbound', action, item.href); + }); + } + }); {% endif %}