2020-07-22 09:54:17 +02:00
|
|
|
---
|
|
|
|
template: overrides/main.html
|
|
|
|
---
|
|
|
|
|
|
|
|
# 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
|
2020-07-22 09:54:17 +02:00
|
|
|
documentation is actually used can be an essential success factor. While
|
2020-07-22 11:57:41 +02:00
|
|
|
Material for MkDocs natively integrates with [Google Analytics][1], [other
|
2021-05-01 20:26:54 +02:00
|
|
|
analytics providers][2] can be used, too.
|
2020-07-22 09:54:17 +02:00
|
|
|
|
|
|
|
[1]: https://developers.google.com/analytics
|
2020-09-03 15:32:32 +02:00
|
|
|
[2]: #other-analytics-providers
|
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
|
|
|
|
2020-07-22 11:57:41 +02:00
|
|
|
[:octicons-file-code-24: Source][3] · :octicons-milestone-24: Default: _none_
|
2020-07-22 09:54:17 +02:00
|
|
|
|
2021-06-12 13:49:23 +02:00
|
|
|
Material for MkDocs integrates with both, Google Analytics 4 and the now phasing
|
|
|
|
out Universal Analytics (`UA-*`). Depending on the prefix of the property, add
|
|
|
|
the following to `mkdocs.yml`:
|
2020-07-22 09:54:17 +02:00
|
|
|
|
2021-06-12 13:49:23 +02:00
|
|
|
=== "Google Analytics 4"
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
extra:
|
|
|
|
analytics:
|
|
|
|
provider: google
|
|
|
|
property: G-XXXXXXXXXX
|
|
|
|
```
|
|
|
|
|
|
|
|
=== "Universal Analytics"
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
extra:
|
|
|
|
analytics:
|
|
|
|
provider: google
|
|
|
|
property: UA-XXXXXXXX-X
|
|
|
|
```
|
2020-07-22 09:54:17 +02:00
|
|
|
|
2020-07-22 11:57:41 +02:00
|
|
|
[3]: https://github.com/squidfunk/mkdocs-material/blob/master/src/partials/integrations/analytics.html
|
2020-07-22 09:54:17 +02:00
|
|
|
|
2020-07-24 14:30:03 +02:00
|
|
|
#### Site search tracking
|
2020-07-22 09:54:17 +02:00
|
|
|
|
2020-07-26 14:46:09 +02:00
|
|
|
Besides basic page views, _site search_ can also be tracked to understand better
|
2020-07-22 09:54:17 +02:00
|
|
|
how people use your documentation and what they expect to find. To enable
|
|
|
|
search tracking:
|
|
|
|
|
|
|
|
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`.
|
|
|
|
|
2021-06-12 13:49:23 +02:00
|
|
|
_Site search tracking is not supported with Google Analytics 4 due to the much
|
|
|
|
more complicated manual setup. If you want to set up site search tracking
|
|
|
|
yourself, [this tutorial][4] might be a good start._
|
|
|
|
|
|
|
|
[4]: https://www.analyticsmania.com/post/track-site-search-with-google-tag-manager-and-google-analytics/
|
|
|
|
|
2020-07-22 09:54:17 +02:00
|
|
|
## Customization
|
|
|
|
|
2020-07-31 09:35:24 +02:00
|
|
|
### Other analytics providers
|
2020-07-22 09:54:17 +02:00
|
|
|
|
2020-07-23 15:34:43 +02:00
|
|
|
[:octicons-file-code-24: Source][3] ·
|
2020-07-22 09:54:17 +02:00
|
|
|
:octicons-mortar-board-24: Difficulty: _easy_
|
|
|
|
|
|
|
|
In order to integrate another analytics service provider offering an
|
2020-07-22 11:57:41 +02:00
|
|
|
asynchronous JavaScript-based tracking solution, you can [extend the theme][5]
|
|
|
|
and [override the `analytics` block][6]:
|
|
|
|
|
2020-07-27 14:44:47 +02:00
|
|
|
``` html
|
2020-07-22 11:57:41 +02:00
|
|
|
{% block analytics %}
|
2020-07-27 14:44:47 +02:00
|
|
|
<!-- Add custom analytics integration here -->
|
2020-07-22 11:57:41 +02:00
|
|
|
{% endblock %}
|
|
|
|
```
|
2020-07-22 09:54:17 +02:00
|
|
|
|
2020-07-22 11:57:41 +02:00
|
|
|
[5]: ../customization.md#extending-the-theme
|
2021-05-01 20:26:54 +02:00
|
|
|
[6]: ../customization.md#overriding-blocks-recommended
|
2020-07-22 09:54:17 +02:00
|
|
|
|
2020-07-22 19:11:22 +02:00
|
|
|
If you're using [instant loading][7], you may use the `location$` observable,
|
2020-07-22 11:57:41 +02:00
|
|
|
which will emit the current `URL` to listen for navigation events and register
|
|
|
|
a page view event with:
|
2020-07-22 09:54:17 +02:00
|
|
|
|
|
|
|
``` js
|
2021-02-22 22:27:30 +01:00
|
|
|
location$.subscribe(function(url) {
|
2020-07-22 11:57:41 +02:00
|
|
|
/* Add custom page event tracking here */
|
2020-07-22 09:54:17 +02:00
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2020-07-22 11:57:41 +02:00
|
|
|
Note that this must be integrated with [additional JavaScript][8], and cannot be
|
2020-07-26 14:46:09 +02:00
|
|
|
included as part of the `analytics` block, as it is included in the `head` of
|
2020-07-22 11:57:41 +02:00
|
|
|
the document.
|
|
|
|
|
|
|
|
[7]: setting-up-navigation.md#instant-loading
|
|
|
|
[8]: ../customization.md#additional-javascript
|