Refactored analytics integration

This commit is contained in:
squidfunk 2021-06-06 17:32:30 +02:00
parent a8534f7a48
commit ed9347c487
7 changed files with 131 additions and 45 deletions

View File

@ -71,9 +71,7 @@
<link rel="stylesheet" href="{{ path | url }}"> <link rel="stylesheet" href="{{ path | url }}">
{% endfor %} {% endfor %}
{% block analytics %} {% block analytics %}
{% if config.google_analytics %}
{% include "partials/integrations/analytics.html" %} {% include "partials/integrations/analytics.html" %}
{% endif %}
{% endblock %} {% endblock %}
{% block extrahead %}{% endblock %} {% block extrahead %}{% endblock %}
</head> </head>

View File

@ -1,6 +1,10 @@
{#- {#-
This file was automatically generated - do not edit This file was automatically generated - do not edit
-#} -#}
{% set analytics = config.google_analytics %} {% if config.google_analytics %}
<script>window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)},ga.l=+new Date,ga("create","{{ analytics[0] }}","{{ analytics[1] }}"),ga("set","anonymizeIp",!0),ga("send","pageview"),document.addEventListener("DOMContentLoaded",function(){document.forms.search&&document.forms.search.query.addEventListener("blur",function(){var e;this.value&&(e=document.location.pathname,ga("send","pageview",e+"?q="+this.value))}),"undefined"!=typeof location$&&location$.subscribe(function(e){ga("send","pageview",e.pathname)})})</script> {% set provider = "google" %}
<script async src="https://www.google-analytics.com/analytics.js"></script> {% endif %}
{% if config.extra.analytics %}
{% set provider = config.extra.analytics.provider %}
{% endif %}
{% include "partials/integrations/analytics/" ~ provider ~ ".html" %}

View File

@ -0,0 +1,16 @@
{#-
This file was automatically generated - do not edit
-#}
{% if config.google_analytics %}
{% set property = config.google_analytics[0] %}
{% endif %}
{% if config.extra.analytics %}
{% set property = config.extra.analytics.property | d("", true) %}
{% endif %}
{% if property.startswith("G-") %}
<script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","{{ property }}"),document.addEventListener("DOMContentLoaded",function(){"undefined"!=typeof location$&&location$.subscribe(function(t){gtag("config","{{ property }}",{page_path:t.pathname})})})</script>
<script async src="https://www.googletagmanager.com/gtag/js?id={{ property }}"></script>
{% elif property.startswith("UA-") %}
<script>window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)},ga.l=+new Date,ga("create","{{ property }}","auto"),ga("set","anonymizeIp",!0),ga("send","pageview"),document.addEventListener("DOMContentLoaded",function(){document.forms.search&&document.forms.search.query.addEventListener("blur",function(){var e;this.value&&(e=document.location.pathname,ga("send","pageview",e+"?q="+this.value))}),"undefined"!=typeof location$&&location$.subscribe(function(e){ga("send","pageview",e.pathname)})})</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
{% endif %}

View File

@ -98,6 +98,9 @@ plugins:
# Customization # Customization
extra: extra:
analytics:
provider: google
property: !!python/object/apply:os.getenv ["GOOGLE_ANALYTICS_KEY"]
social: social:
- icon: fontawesome/brands/github - icon: fontawesome/brands/github
link: https://github.com/squidfunk link: https://github.com/squidfunk
@ -202,8 +205,3 @@ nav:
- Getting started: - Getting started:
- Installation: insiders/getting-started.md - Installation: insiders/getting-started.md
- Changelog: insiders/changelog.md - Changelog: insiders/changelog.md
# Google Analytics
google_analytics:
- !!python/object/apply:os.getenv ["GOOGLE_ANALYTICS_KEY"]
- auto

View File

@ -143,9 +143,7 @@
<!-- Analytics --> <!-- Analytics -->
{% block analytics %} {% block analytics %}
{% if config.google_analytics %}
{% include "partials/integrations/analytics.html" %} {% include "partials/integrations/analytics.html" %}
{% endif %}
{% endblock %} {% endblock %}
<!-- Custom front matter --> <!-- Custom front matter -->

View File

@ -20,36 +20,15 @@
IN THE SOFTWARE. IN THE SOFTWARE.
--> -->
<!-- Google Analytics integration --> <!-- Determine analytics provider (deprecated, removed in v8) -->
{% set analytics = config.google_analytics %} {% if config.google_analytics %}
<script> {% set provider = "google" %}
window.ga = window.ga || function() { {% endif %}
(ga.q = ga.q || []).push(arguments)
}
ga.l = +new Date
/* Set up integration and send page view */ <!-- Determine analytics provider -->
ga("create", "{{ analytics[0] }}", "{{ analytics[1] }}") {% if config.extra.analytics %}
ga("set", "anonymizeIp", true) {% set provider = config.extra.analytics.provider %}
ga("send", "pageview") {% endif %}
/* Register handler to log search on blur */ <!-- Analytics provider -->
document.addEventListener("DOMContentLoaded", function() { {% include "partials/integrations/analytics/" ~ provider ~ ".html" %}
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)
}
})
}
/* Send page view on location change */
if (typeof location$ !== "undefined")
location$.subscribe(function(url) {
ga("send", "pageview", url.pathname)
})
})
</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>

View File

@ -0,0 +1,93 @@
<!--
Copyright (c) 2016-2021 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Determine analytics property (deprecated, removed in v8) -->
{% if config.google_analytics %}
{% set property = config.google_analytics[0] %}
{% endif %}
<!-- Determine analytics property -->
{% if config.extra.analytics %}
{% set property = config.extra.analytics.property | d("", true) %}
{% endif %}
<!-- Google Analytics 4 (G-XXXXXXXXXX) -->
{% if property.startswith("G-") %}
<script>
window.dataLayer = window.dataLayer || []
function gtag() { dataLayer.push(arguments) }
/* Set up integration and send page view */
gtag("js", new Date())
gtag("config", "{{ property }}")
/* Register virtual event handlers */
document.addEventListener("DOMContentLoaded", function() {
/* Send page view on location change */
if (typeof location$ !== "undefined")
location$.subscribe(function(url) {
gtag("config", "{{ property }}", {
page_path: url.pathname
})
})
})
</script>
<script async src="https://www.googletagmanager.com/gtag/js?id={{ property }}">
</script>
<!-- Google Analytics (UA-XXXXXXXX-X) -->
{% elif property.startswith("UA-") %}
<script>
window.ga = window.ga || function() {
(ga.q = ga.q || []).push(arguments)
}
ga.l = +new Date()
/* Set up integration and send page view */
ga("create", "{{ property }}", "auto")
ga("set", "anonymizeIp", true)
ga("send", "pageview")
/* Register virtual event handlers */
document.addEventListener("DOMContentLoaded", function() {
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)
}
})
}
/* Send page view on location change */
if (typeof location$ !== "undefined")
location$.subscribe(function(url) {
ga("send", "pageview", url.pathname)
})
})
</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
{% endif %}