1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-12-11 07:16:09 +01:00
mkdocs-material/docs/setup/setting-up-site-analytics.md

321 lines
11 KiB
Markdown
Raw Normal View History

2020-07-22 09:54:17 +02:00
# Setting up site analytics
2020-07-26 14:46:09 +02:00
As with any other service offered on the web, understanding how your project
2021-07-19 09:30:47 +02:00
documentation is actually used can be an essential success factor. Material for
2021-10-10 19:22:13 +02:00
MkDocs natively integrates with [Google Analytics] and offers a customizable
2022-03-23 11:08:22 +01:00
[cookie consent] and a [feedback widget].
2020-07-22 09:54:17 +02:00
2021-10-10 19:22:13 +02:00
[Google Analytics]: https://developers.google.com/analytics
2022-09-11 19:25:40 +02:00
[cookie consent]: ensuring-data-privacy.md#cookie-consent
2022-03-23 11:08:22 +01:00
[feedback widget]: #was-this-page-helpful
2020-07-22 09:54:17 +02:00
## Configuration
2020-07-22 11:57:41 +02:00
### Google Analytics
2020-07-22 09:54:17 +02:00
2023-09-14 19:09:18 +02:00
<!-- md:version 7.1.8 -->
<!-- md:default none -->
2020-07-22 09:54:17 +02:00
Material for MkDocs integrates natively with Google Analytics 4[^1]. If you
already set up Google Analytics and have a property, enable it by adding the
2021-10-10 19:22:13 +02:00
following lines to `mkdocs.yml`:
2020-07-22 09:54:17 +02:00
[^1]:
Prior to Material for MkDocs 9.2.0, Universal Analytics was supported as
well. However, since Universal Analytics has been sunset, this integration
was removed in 9.2.0.
``` yaml
extra:
analytics:
provider: google
property: G-XXXXXXXXXX
```
2020-07-22 09:54:17 +02:00
2021-11-01 08:38:24 +01:00
??? question "How to measure site search usage?"
2020-07-22 09:54:17 +02:00
Besides page views and events, [site search] can be tracked to better
understand how people use your documentation and what they expect to find.
In order to enable site search tracking, the following steps are required:
2020-07-22 09:54:17 +02:00
1. Go to your Google Analytics __admin settings__
2. Select the property for the respective tracking code
3. Select the __data streams__ tab and click the corresponding URL
4. Click the gear icon within the __enhanced measurement__ section
5. Ensure that __site search__ is enabled
[site search]: setting-up-site-search.md
### Was this page helpful?
2023-09-14 19:09:18 +02:00
<!-- md:version 8.4.0 -->
<!-- md:default none -->
A simple [feedback widget] can be included at the bottom of each page,
encouraging users to give instant feedback whether a page was helpful or not.
Add the following lines to `mkdocs.yml`:
``` yaml
extra:
2021-12-11 14:30:07 +01:00
analytics: # (1)!
feedback:
title: Was this page helpful?
ratings:
- icon: material/emoticon-happy-outline
name: This page was helpful
data: 1
note: >-
Thanks for your feedback!
- icon: material/emoticon-sad-outline
name: This page could be improved
data: 0
2021-12-11 14:30:07 +01:00
note: >- # (2)!
Thanks for your feedback! Help us improve this page by
2022-08-21 09:29:12 +02:00
using our <a href="..." target="_blank" rel="noopener">feedback form</a>.
```
2022-03-23 11:08:22 +01:00
1. This feature is natively integrated with [Google Analytics][analytics],
which is why `provider` and `property` are also required. However, it's also
possible to provide a [custom feedback integration].
2. You can add arbitrary HTML tags to the note which is shown after the user
submitted the feedback, e.g. to link to a feedback form.
Both properties, `title` and `ratings`, are required. Note that it's allowed to
define more than two ratings, e.g. to implement a 1-5 star rating. Since the
2023-09-14 19:09:18 +02:00
feedback widget sends data to a third-party service, it is, of course, natively
integrated with the [cookie consent] feature[^2].
[^2]:
If the user doesn't accept the `analytics` cookie, the feedback widget is
not shown.
??? question "How to visualize the collected feedback ratings?"
2021-11-30 19:48:40 +01:00
To visualize feedback ratings you'll need to create a custom report with
[Google Analytics] that will quickly show you the worst- and best-rated
pages of your project documentation.
1. Go to your Google Analytics __dashboard__
2022-04-02 14:42:13 +02:00
2. Go to the __configure__ page on the left hand menu, then select
__custom definitions__
2022-04-02 14:42:13 +02:00
2023-09-14 19:09:18 +02:00
3. Click the __custom metrics__ tab and then __create custom metrics__,
enter the following values:
2022-04-02 14:42:13 +02:00
* Metric name: Page helpful
* Description: Was this page helpful?
* Event parameter: `data`
* Unit of measurement: Standard
2022-04-02 14:42:13 +02:00
4. Go to the __explore__ page on the left hand menu, create a new
__blank exploration__
2022-04-02 14:42:13 +02:00
5. Configure the report as follows:
2022-04-02 14:42:13 +02:00
* Dimensions: Add `Event name` and `Page location`
* Metrics: Add `Event count` and `Page helpful`
(the custom metric created in step 3)
* Rows: `Page location`
* Values: Drag in both `Event count` and `Page helpful`
2023-09-14 19:09:18 +02:00
* Filters: Add a new filter for
`Event name / exactly matches / feedback`
2021-11-30 19:48:40 +01:00
!!! warning "Delay in data availability"
2021-11-30 19:48:40 +01:00
The report may take 24 hours or longer to begin displaying data
2021-11-30 19:48:40 +01:00
Now, after you've saved the report and collected some feedback ratings,
you'll have a list of all pages with the total number of ratings, and an
average rating per page. This should help you identify pages that need to
be improved:
2023-08-31 14:04:20 +02:00
!!! danger "Google Analytics 4 does not support average values"
To our knowledge, Google Analytics 4 has currently no feature that
allows to define a custom calculated metric to compute the average
rating of a page. See #5740.
[![feedback report]][feedback report]
2022-06-05 14:43:37 +02:00
The following properties are available for each rating:
2023-09-14 19:09:18 +02:00
<!-- md:option analytics.feedback.ratings.icon -->
2023-09-14 19:09:18 +02:00
: <!-- md:default none --> <!-- md:flag required -->
This property must point to a valid icon path referencing [any icon bundled
with the theme][custom icons], or the build will not succeed. Some popular
combinations:
* :material-emoticon-happy-outline: + :material-emoticon-sad-outline: `material/emoticon-happy-outline` + `material/emoticon-sad-outline`
* :material-thumb-up-outline: + :material-thumb-down-outline: `material/thumb-up-outline` + `material/thumb-down-outline`
* :material-heart: + :material-heart-broken: `material/heart` + `material/heart-broken`
2023-09-14 19:09:18 +02:00
<!-- md:option analytics.feedback.ratings.name -->
2023-09-14 19:09:18 +02:00
: <!-- md:default none --> <!-- md:flag required -->
The value of this property is shown on user interaction (i.e. keyboard focus
or mouse hover), explaining the meaning of the rating behind the icon.
2023-09-14 19:09:18 +02:00
<!-- md:option analytics.feedback.ratings.data -->
2023-09-14 19:09:18 +02:00
: <!-- md:default none --> <!-- md:flag required -->
The value of this property is sent as a data value with the custom event
that is transmitted to Google Analytics[^3] (or any custom integration).
[^3]:
Note that for Google Analytics, the data value must be an integer.
2023-09-14 19:09:18 +02:00
<!-- md:option analytics.feedback.ratings.note -->
2023-09-14 19:09:18 +02:00
: <!-- md:default none --> <!-- md:flag required -->
The value of this property is shown after the user selected the rating.
It may contain arbitrary HTML tags, which is especially useful to ask the
user to provide more detailed feedback for the current page through a form.
2021-11-30 19:33:13 +01:00
It's also possible to pre-fill forms with the URL and title of the current
page by using the following placeholders:
2021-11-30 19:33:13 +01:00
- `{url}` Page URL
- `{title}` Page title
```
https://github.com/.../issues/new/?title=[Feedback]+{title}+-+{url}
```
2023-09-14 19:09:18 +02:00
In this example, when clicking the link, the user is redirected to the "new
issue" form of your repository, with a pre-filled title including the path
of the current document, e.g.:
```
2021-11-30 19:33:13 +01:00
[Feedback] Setting up site analytics /setup/setting-up-site-analytics/
```
An alternative to GitHub issues is [Google Forms].
[feedback widget]: #feedback
2022-03-23 11:08:22 +01:00
[analytics]: #google-analytics
[feedback report]: ../assets/screenshots/feedback-report.png
[custom feedback integration]: #custom-site-feedback
2023-09-20 14:59:25 +02:00
[custom icons]: https://github.com/squidfunk/mkdocs-material/tree/master/material/templates/.icons
[Google Forms]: https://www.google.com/forms/about/
2021-11-07 12:35:21 +01:00
## Usage
### Hiding the feedback widget
2022-10-02 16:36:47 +02:00
The [feedback widget] can be hidden for a document with the front matter `hide`
property. Add the following lines at the top of a Markdown file:
2021-11-07 12:35:21 +01:00
2022-09-11 19:25:40 +02:00
``` yaml
2021-11-07 12:35:21 +01:00
---
hide:
- feedback
---
2023-09-14 19:09:18 +02:00
# Page title
2021-11-07 12:35:21 +01:00
...
```
2020-07-22 09:54:17 +02:00
## Customization
2021-07-19 09:30:47 +02:00
### Custom site analytics
2020-07-22 09:54:17 +02:00
2023-09-14 19:09:18 +02:00
In order to integrate another analytics service provider offering a
2021-10-10 19:22:13 +02: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`:
2022-09-11 19:25:40 +02:00
=== ":octicons-file-code-16: `overrides/partials/integrations/analytics/custom.html`"
2021-10-10 19:22:13 +02:00
``` html
<script>
/* Add custom analytics integration here, e.g. */
2021-12-11 14:30:07 +01:00
var property = "{{ config.extra.analytics.property }}" // (1)!
2021-10-10 19:22:13 +02:00
/* Wait for page to load and application to mount */
document.addEventListener("DOMContentLoaded", function() {
location$.subscribe(function(url) {
2021-12-11 14:30:07 +01:00
/* Add custom page event tracking here */ // (2)!
2021-10-10 19:22:13 +02:00
})
})
</script>
```
2020-07-22 09:54:17 +02:00
2021-10-10 19:22:13 +02: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 09:30:47 +02:00
2022-09-11 19:25:40 +02:00
=== ":octicons-file-code-16: `mkdocs.yml`"
2020-07-22 09:54:17 +02:00
2021-10-10 19:22:13 +02:00
``` yaml
extra:
analytics:
provider: custom
2021-12-11 14:30:07 +01:00
property: foobar # (1)!
2021-10-10 19:22:13 +02:00
```
2021-07-19 09:30:47 +02:00
2021-10-10 19:22:13 +02: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 09:54:17 +02:00
2021-10-10 19:22:13 +02:00
[theme extension]: ../customization.md#extending-the-theme
[instant loading]: setting-up-navigation.md#instant-loading
2020-07-22 09:54:17 +02:00
### Custom site feedback
A custom feedback widget integration just needs to process the events that are
generated by users interacting with the feedback widget with the help of some
[additional JavaScript]:
2022-09-11 19:25:40 +02:00
=== ":octicons-file-code-16: `docs/javascripts/feedback.js`"
``` js
var feedback = document.forms.feedback
feedback.hidden = false // (1)!
feedback.addEventListener("submit", function(ev) {
ev.preventDefault()
var page = document.location.pathname // (2)!
var data = ev.submitter.getAttribute("data-md-value")
console.log(page, data) // (3)!
feedback.firstElementChild.disabled = true // (4)!
var note = feedback.querySelector(
".md-feedback__note [data-md-value='" + data + "']"
)
if (note)
note.hidden = false // (5)!
})
```
1. The feedback widget is hidden by default so that it does not appear when
people have JavaScript turned off. So, it needs to be turned on here.
2. Retrieve page and feedback value.
3. Replace this with the code that sends the data off to your analytics
provider.
4. Disable the form after submission.
5. Show the configured notes. Which one is shown depends on the user
feedback.
2022-09-11 19:25:40 +02:00
=== ":octicons-file-code-16: `mkdocs.yml`"
``` yaml
extra_javascript:
- javascripts/feedback.js
```
&nbsp;
{ #feedback style="margin: 0; height: 0" }
2022-04-10 09:48:56 +02:00
[additional JavaScript]: ../customization.md#additional-javascript