1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-11-24 07:30:12 +01:00

Use asynchronous Google Analytics tracking

This commit is contained in:
squidfunk 2019-01-28 11:13:26 +01:00
parent db15284cc0
commit 2f31618d60
5 changed files with 14 additions and 41 deletions

View File

@ -185,7 +185,7 @@
{% endblock %}
</div>
{% block scripts %}
<script src="{{ 'assets/javascripts/application.05988d62.js' | url }}"></script>
<script src="{{ 'assets/javascripts/application.ff6f93ee.js' | url }}"></script>
{% if lang.t("search.language") != "en" %}
{% set languages = lang.t("search.language").split(",") %}
{% if languages | length and languages[0] != "" %}

View File

@ -1,2 +1,4 @@
<script>window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)},ga.l=+new Date,ga("create","{{ config.google_analytics[0] }}","{{ config.google_analytics[1] }}"),ga("set","anonymizeIp",!0),ga("send","pageview");var links=document.getElementsByTagName("a");if(Array.prototype.map.call(links,function(a){a.host!=document.location.host&&a.addEventListener("click",function(){var e=a.getAttribute("data-md-action")||"follow";ga("send","event","outbound",e,a.href)})}),document.forms.search){var query=document.forms.search.query;query.addEventListener("blur",function(){if(this.value){var e=document.location.pathname;ga("send","pageview",e+"?q="+this.value)}})}</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id={{
config.google_analytics[0]
}}"></script>
<script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","{{ config.google_analytics[0] }}")</script>

View File

@ -288,9 +288,7 @@ function initialize(config) { // eslint-disable-line func-style
new Material.Event.Listener("[data-md-component=query]", [
"focus", "keyup", "change"
], new Material.Search.Result("[data-md-component=result]", () => {
return fetch(`${config.url.base}/${
config.version < "0.17" ? "mkdocs" : "search"
}/search_index.json`, {
return fetch(`${config.url.base}/search/search_index.json`, {
credentials: "same-origin"
}).then(response => response.json())
.then(data => {

View File

@ -21,39 +21,12 @@
-->
<!-- Google Analytics integration -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{
config.google_analytics[0]
}}"></script>
<script>
window.ga = window.ga || function () {
(ga.q = ga.q || []).push(arguments)
};
ga.l = +new Date;
/* General initialization */
ga("create",
"{{ config.google_analytics[0] }}",
"{{ config.google_analytics[1] }}");
ga("set", "anonymizeIp", true);
ga("send", "pageview");
/* Track outbound links */
var links = document.getElementsByTagName("a");
Array.prototype.map.call(links, function(item) {
if (item.host != document.location.host) {
item.addEventListener("click", function() {
var action = item.getAttribute("data-md-action") || "follow";
ga("send", "event", "outbound", action, item.href);
});
}
});
/* Register handler to log search on blur */
if (document.forms.search) {
var query = document.forms.search.query;
query.addEventListener("blur", function() {
if (this.value) {
var path = document.location.pathname;
ga("send", "pageview", path + "?q=" + this.value);
}
});
}
window.dataLayer = window.dataLayer || [];
function gtag(){ dataLayer.push(arguments); }
gtag("js", new Date());
gtag("config", "{{ config.google_analytics[0] }}");
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>