mkdocs-material/docs/setup/setting-up-site-analytics.md

203 lines
6.2 KiB
Markdown
Raw Normal View History

2020-07-22 10:54:17 +03:00
---
template: overrides/main.html
---
# Setting up site analytics
2020-07-26 15:46:09 +03:00
As with any other service offered on the web, understanding how your project
2021-07-19 10:30:47 +03:00
documentation is actually used can be an essential success factor. Material for
2021-10-10 20:22:13 +03:00
MkDocs natively integrates with [Google Analytics] and offers a customizable
and extendable [cookie consent][extra.consent].
2020-07-22 10:54:17 +03:00
2021-10-10 20:22:13 +03:00
[Google Analytics]: https://developers.google.com/analytics
[extra.consent]: #cookie-consent
2020-07-22 10:54:17 +03:00
## Configuration
2020-07-22 12:57:41 +03:00
### Google Analytics
2020-07-22 10:54:17 +03:00
2021-10-10 20:22:13 +03:00
[:octicons-tag-24: 7.1.8][Google Analytics support] ·
:octicons-milestone-24: Default: _none_
2020-07-22 10:54:17 +03:00
Material for MkDocs integrates with both, Google Analytics 4 and the now phasing
2021-10-10 20:22:13 +03:00
out Universal Analytics (`UA-*`). Depending on the property prefix, add the
following lines to `mkdocs.yml`:
2020-07-22 10:54:17 +03:00
2021-10-10 20:22:13 +03:00
=== ":material-google-analytics: Google Analytics 4"
``` yaml
extra:
analytics:
provider: google
property: G-XXXXXXXXXX
```
2021-10-10 20:22:13 +03:00
=== ":material-google-analytics: Universal Analytics"
``` yaml
extra:
analytics:
provider: google
property: UA-XXXXXXXX-X
```
2020-07-22 10:54:17 +03:00
2021-10-03 21:28:52 +03:00
[Google Analytics support]: https://github.com/squidfunk/mkdocs-material/releases/tag/7.1.8
2020-07-22 10:54:17 +03:00
2020-07-24 15:30:03 +03:00
#### Site search tracking
2020-07-22 10:54:17 +03:00
2021-10-10 20:22:13 +03:00
Besides basic page views, site search can also be tracked to understand better
2020-07-22 10:54:17 +03:00
how people use your documentation and what they expect to find. To enable
search tracking:
2021-10-10 13:19:14 +03:00
1. Go to your Google Analytics __admin settings__
2. Select the property for the respective tracking code
3. Go to the __view settings__ tab.
4. Scroll down and enable __site search settings__
5. Set the __query parameter__ to `q`.
2020-07-22 10:54:17 +03:00
2021-10-10 20:22:13 +03:00
Site search tracking is not supported with Google Analytics 4 due to the more
complicated manual setup. If you want to set up site search tracking yourself,
[this tutorial][tutorial] is a good start.
2021-10-10 20:22:13 +03:00
[tutorial]: https://www.analyticsmania.com/post/track-site-search-with-google-tag-manager-and-google-analytics/
2021-07-10 16:21:22 +03:00
### Cookie consent
2021-10-03 21:28:52 +03:00
[:octicons-heart-fill-24:{ .mdx-heart } Insiders][Insiders]{ .mdx-insiders } ·
2021-10-10 20:22:13 +03:00
[:octicons-tag-24: insiders-2.10.0][Insiders] ·
:octicons-milestone-24: Default: _none_
2021-07-10 16:21:22 +03:00
2021-10-10 20:22:13 +03:00
Material for MkDocs ships a native and extensible cookie consent form which
asks the user for his consent prior to sending any analytics. Add the following
2021-07-19 10:30:47 +03:00
to `mkdocs.yml`:
2021-07-10 16:21:22 +03:00
``` yaml
extra:
2021-07-18 18:57:45 +03:00
consent:
title: Cookie consent
2021-07-19 10:30:47 +03:00
description: > # (1)
2021-07-18 18:57:45 +03:00
We use cookies to recognize your repeated visits and preferences, as well
as to measure the effectiveness of our documentation and whether users
find what they're searching for. With your consent, you're helping us to
make our documentation better.
2021-07-10 16:21:22 +03:00
```
2021-10-10 13:19:14 +03:00
1. You can add arbitrary HTML tags in the `description`, e.g. to link to your
terms of service or other parts of the site.
2021-07-19 10:30:47 +03:00
Note that both, `title` and `description`, are required. If Google Analytics was
configured via `mkdocs.yml`, the cookie consent will automatically include a
2021-10-10 20:22:13 +03:00
setting for the user to disable it. Furthermore, [custom cookies] can be
2021-07-19 10:30:47 +03:00
integrated by using the `cookies` field:
2021-10-10 20:22:13 +03:00
=== "Custom cookie name"
2021-07-19 10:30:47 +03:00
``` yaml
extra:
consent:
cookies:
analytics: Custom name # (1)
```
1. The default name of the `analytics` cookie is `Google Analytics`.
2021-10-10 20:22:13 +03:00
=== "Custom cookie"
2021-07-19 10:30:47 +03:00
``` yaml
extra:
consent:
cookies:
analytics: Google Analytics # (1)
custom: Custom cookie
```
1. If you add a custom cookie to the `cookies` field, the `analytics` cookie
must be added back explicitly, or analytics won't be triggered.
2021-07-10 16:21:22 +03:00
When a user first visits your site, a cookie consent form is rendered:
2021-10-10 20:22:13 +03:00
[![extra.consent enabled]][extra.consent enabled]
2021-07-20 20:36:41 +03:00
In order to comply with GDPR, users must be able to change their cookie settings
at any time. This can be done by creating a simple link as part of any document,
2021-10-10 20:22:13 +03:00
e.g. your privacy policy:
2021-07-20 20:36:41 +03:00
``` markdown
[Change cookie settings](#__consent){ .md-button }
```
2021-07-10 16:21:22 +03:00
2021-10-03 21:28:52 +03:00
[Insiders]: ../insiders/index.md
2021-10-10 20:22:13 +03:00
[custom cookies]: #custom-cookies
[extra.consent enabled]: ../assets/screenshots/consent.png
2021-07-10 16:21:22 +03:00
2020-07-22 10:54:17 +03:00
## Customization
2021-07-19 10:30:47 +03:00
### Custom site analytics
2020-07-22 10:54:17 +03:00
2021-07-19 10:30:47 +03:00
In order to integrate another analytics service provider offering a
2021-10-10 20:22:13 +03:00
JavaScript-based tracking solution, just follow the guide on [theme extension]
and create a new partial in the `overrides` folder. The name of the partial is
used to configure the custom integration via `mkdocs.yml`:
=== ":octicons-file-code-16: partials/integrations/analytics/custom.html"
``` html
<script>
/* Add custom analytics integration here, e.g. */
var property = "{{ config.extra.analytics.property }}" // (1)
/* Wait for page to load and application to mount */
document.addEventListener("DOMContentLoaded", function() {
location$.subscribe(function(url) {
/* Add custom page event tracking here */ // (2)
})
})
</script>
```
2020-07-22 10:54:17 +03:00
2021-10-10 20:22:13 +03:00
1. As an example, this variable receives the value set in `mkdocs.yml`,
which is `"foobar"` for `property`.
2. If you're using [instant loading], you can use the `location$`
observable to listen for navigation events, which always emits the
current `URL`.
2021-07-19 10:30:47 +03:00
2021-10-10 20:22:13 +03:00
=== ":octicons-file-code-16: mkdocs.yml"
2020-07-22 10:54:17 +03:00
2021-10-10 20:22:13 +03:00
``` yaml
extra:
analytics:
provider: custom
property: foobar # (1)
```
2021-07-19 10:30:47 +03:00
2021-10-10 20:22:13 +03:00
1. You can add arbitrary key-value combinations to configure your
custom integration. This is especially useful if you're sharing the
custom integration across multiple repositories.
2020-07-22 10:54:17 +03:00
2021-10-10 20:22:13 +03:00
[theme extension]: ../customization.md#extending-the-theme
[instant loading]: setting-up-navigation.md#instant-loading
2020-07-22 10:54:17 +03:00
2021-10-10 20:22:13 +03:00
### Custom cookies
2021-07-19 10:30:47 +03:00
2021-10-10 20:22:13 +03:00
If you've customized the [cookie consent][extra.consent] and added a `custom`
cookie, the user will be prompted to accept your custom cookie. Use [additional
JavaScript] to check whether the user accepted it:
2021-07-19 10:30:47 +03:00
2021-10-10 20:22:13 +03:00
=== ":octicons-file-code-16: docs/javascripts/consent.js"
2021-07-19 10:30:47 +03:00
2021-10-10 20:22:13 +03:00
``` js
var consent = __md_get("__consent")
if (consent && consent.custom) {
/* The user accepted the cookie */
}
```
2021-07-19 10:30:47 +03:00
2021-10-10 20:22:13 +03:00
=== ":octicons-file-code-16: mkdocs.yml"
2021-07-19 10:30:47 +03:00
2021-10-10 20:22:13 +03:00
``` yaml
extra_javascript:
- javascripts/consent.js
```
2020-07-22 12:57:41 +03:00
2021-10-10 20:22:13 +03:00
[additional JavaScript]: ../customization.md#additional-javascript