文档
浏览文档

Add Privatus Analytics to MkDocs

Add cookieless analytics to a MkDocs site with a Material for MkDocs override of the extrahead block, or with a small extra_javascript file.

查看 Markdown

Material for MkDocs#

  1. In mkdocs.yml point the theme at an overrides folder:
yaml
   theme:
     name: material
     custom_dir: overrides
  1. Create overrides/main.html:
jinja
   {% extends "base.html" %}

   {% block extrahead %}
     <script defer src="https://privatusstats.com/js/pa.js" data-site="pa_YOURSITEID" data-modules="engage,auto,vitals,clicks,ask"></script>
   {% endblock %}

extrahead is an empty block for extra tags in the head. Files in overrides replace the theme's files with the same path. If your theme already has an overrides/main.html, add the block to it.

Any theme#

MkDocs' extra_javascript only sets path, type, async and defer, so it can't carry the data- attributes. Load the tracker from your own file instead. Create docs/javascripts/privatus.js:

js
var s = document.createElement('script')
s.defer = true
s.src = 'https://privatusstats.com/js/pa.js'
s.setAttribute('data-site', 'pa_YOURSITEID')
s.setAttribute('data-modules', 'engage,auto,vitals,clicks,ask')
document.head.appendChild(s)

and list it in mkdocs.yml:

yaml
extra_javascript:
  - javascripts/privatus.js

Instant navigation#

Material's instant navigation changes pages with history.pushState. The tracker follows that automatically, and the tag stays loaded, so each page is counted once. See single-page apps.