Skip to content

Commit ed9347c

Browse files
committed
Refactored analytics integration
1 parent a8534f7 commit ed9347c

File tree

7 files changed

+131
-45
lines changed

7 files changed

+131
-45
lines changed

material/base.html

+1-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@
7171
<link rel="stylesheet" href="{{ path | url }}">
7272
{% endfor %}
7373
{% block analytics %}
74-
{% if config.google_analytics %}
75-
{% include "partials/integrations/analytics.html" %}
76-
{% endif %}
74+
{% include "partials/integrations/analytics.html" %}
7775
{% endblock %}
7876
{% block extrahead %}{% endblock %}
7977
</head>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{#-
22
This file was automatically generated - do not edit
33
-#}
4-
{% set analytics = config.google_analytics %}
5-
<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>
6-
<script async src="https://www.google-analytics.com/analytics.js"></script>
4+
{% if config.google_analytics %}
5+
{% set provider = "google" %}
6+
{% endif %}
7+
{% if config.extra.analytics %}
8+
{% set provider = config.extra.analytics.provider %}
9+
{% endif %}
10+
{% include "partials/integrations/analytics/" ~ provider ~ ".html" %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{#-
2+
This file was automatically generated - do not edit
3+
-#}
4+
{% if config.google_analytics %}
5+
{% set property = config.google_analytics[0] %}
6+
{% endif %}
7+
{% if config.extra.analytics %}
8+
{% set property = config.extra.analytics.property | d("", true) %}
9+
{% endif %}
10+
{% if property.startswith("G-") %}
11+
<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>
12+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ property }}"></script>
13+
{% elif property.startswith("UA-") %}
14+
<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>
15+
<script async src="https://www.google-analytics.com/analytics.js"></script>
16+
{% endif %}

mkdocs.yml

+3-5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ plugins:
9898

9999
# Customization
100100
extra:
101+
analytics:
102+
provider: google
103+
property: !!python/object/apply:os.getenv ["GOOGLE_ANALYTICS_KEY"]
101104
social:
102105
- icon: fontawesome/brands/github
103106
link: https://github.com/squidfunk
@@ -202,8 +205,3 @@ nav:
202205
- Getting started:
203206
- Installation: insiders/getting-started.md
204207
- Changelog: insiders/changelog.md
205-
206-
# Google Analytics
207-
google_analytics:
208-
- !!python/object/apply:os.getenv ["GOOGLE_ANALYTICS_KEY"]
209-
- auto

src/base.html

+1-3
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@
143143

144144
<!-- Analytics -->
145145
{% block analytics %}
146-
{% if config.google_analytics %}
147-
{% include "partials/integrations/analytics.html" %}
148-
{% endif %}
146+
{% include "partials/integrations/analytics.html" %}
149147
{% endblock %}
150148

151149
<!-- Custom front matter -->

src/partials/integrations/analytics.html

+10-31
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,15 @@
2020
IN THE SOFTWARE.
2121
-->
2222

23-
<!-- Google Analytics integration -->
24-
{% set analytics = config.google_analytics %}
25-
<script>
26-
window.ga = window.ga || function() {
27-
(ga.q = ga.q || []).push(arguments)
28-
}
29-
ga.l = +new Date
23+
<!-- Determine analytics provider (deprecated, removed in v8) -->
24+
{% if config.google_analytics %}
25+
{% set provider = "google" %}
26+
{% endif %}
3027

31-
/* Set up integration and send page view */
32-
ga("create", "{{ analytics[0] }}", "{{ analytics[1] }}")
33-
ga("set", "anonymizeIp", true)
34-
ga("send", "pageview")
28+
<!-- Determine analytics provider -->
29+
{% if config.extra.analytics %}
30+
{% set provider = config.extra.analytics.provider %}
31+
{% endif %}
3532

36-
/* Register handler to log search on blur */
37-
document.addEventListener("DOMContentLoaded", function() {
38-
if (document.forms.search) {
39-
var query = document.forms.search.query
40-
query.addEventListener("blur", function() {
41-
if (this.value) {
42-
var path = document.location.pathname;
43-
ga("send", "pageview", path + "?q=" + this.value)
44-
}
45-
})
46-
}
47-
48-
/* Send page view on location change */
49-
if (typeof location$ !== "undefined")
50-
location$.subscribe(function(url) {
51-
ga("send", "pageview", url.pathname)
52-
})
53-
})
54-
</script>
55-
<script async src="https://www.google-analytics.com/analytics.js"></script>
33+
<!-- Analytics provider -->
34+
{% include "partials/integrations/analytics/" ~ provider ~ ".html" %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<!--
2+
Copyright (c) 2016-2021 Martin Donath <[email protected]>
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to
6+
deal in the Software without restriction, including without limitation the
7+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
sell copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
IN THE SOFTWARE.
21+
-->
22+
23+
<!-- Determine analytics property (deprecated, removed in v8) -->
24+
{% if config.google_analytics %}
25+
{% set property = config.google_analytics[0] %}
26+
{% endif %}
27+
28+
<!-- Determine analytics property -->
29+
{% if config.extra.analytics %}
30+
{% set property = config.extra.analytics.property | d("", true) %}
31+
{% endif %}
32+
33+
<!-- Google Analytics 4 (G-XXXXXXXXXX) -->
34+
{% if property.startswith("G-") %}
35+
<script>
36+
window.dataLayer = window.dataLayer || []
37+
function gtag() { dataLayer.push(arguments) }
38+
39+
/* Set up integration and send page view */
40+
gtag("js", new Date())
41+
gtag("config", "{{ property }}")
42+
43+
/* Register virtual event handlers */
44+
document.addEventListener("DOMContentLoaded", function() {
45+
46+
/* Send page view on location change */
47+
if (typeof location$ !== "undefined")
48+
location$.subscribe(function(url) {
49+
gtag("config", "{{ property }}", {
50+
page_path: url.pathname
51+
})
52+
})
53+
})
54+
</script>
55+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ property }}">
56+
</script>
57+
58+
<!-- Google Analytics (UA-XXXXXXXX-X) -->
59+
{% elif property.startswith("UA-") %}
60+
<script>
61+
window.ga = window.ga || function() {
62+
(ga.q = ga.q || []).push(arguments)
63+
}
64+
ga.l = +new Date()
65+
66+
/* Set up integration and send page view */
67+
ga("create", "{{ property }}", "auto")
68+
ga("set", "anonymizeIp", true)
69+
ga("send", "pageview")
70+
71+
/* Register virtual event handlers */
72+
document.addEventListener("DOMContentLoaded", function() {
73+
if (document.forms.search) {
74+
var query = document.forms.search.query
75+
query.addEventListener("blur", function() {
76+
if (this.value) {
77+
var path = document.location.pathname;
78+
ga("send", "pageview", path + "?q=" + this.value)
79+
}
80+
})
81+
}
82+
83+
/* Send page view on location change */
84+
if (typeof location$ !== "undefined")
85+
location$.subscribe(function(url) {
86+
ga("send", "pageview", url.pathname)
87+
})
88+
})
89+
</script>
90+
<script async src="https://www.google-analytics.com/analytics.js"></script>
91+
{% endif %}
92+
93+

0 commit comments

Comments
 (0)