Documentation tools
Add Loogika to MkDocs
You add Loogika to MkDocs with the extra_javascript setting in mkdocs.yml. The setting adds a script tag to every page and works with any theme. Material for MkDocs offers a second route: a theme override that lets you put the tag in the head yourself.
Last updated .
What do you need before you start?
Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own 12-character site ID in place of YOUR_SITE_ID:
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
The mkdocs.yml method uses only the address from the src attribute. The Material override uses the whole tag.
How do you add Loogika in mkdocs.yml?
List the script address under extra_javascript. From MkDocs 1.5, an entry may carry a path key and a defer key:
extra_javascript:
- path: https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID
defer: true
MkDocs 1.4 and older versions do not know that form. List the plain address there:
extra_javascript:
- https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID
Themes print extra_javascript entries at the end of the body and not in the head. The Loogika script still loads on every page.
How do you add Loogika with a Material for MkDocs override?
Material for MkDocs lets you extend the theme with your own template, which puts the tag inside the head. You need two changes.
- Tell Material in
mkdocs.ymlwhich directory holds your overrides, as in the first block below. - Create the file
overrides/main.html. Put the tag in itsextraheadblock, as in the second block. Material reserves that block for your own tags inside the head.
theme:
name: material
custom_dir: overrides
{% extends "base.html" %}
{% block extrahead %}
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
{% endblock %}
How do you publish the change?
Run mkdocs build again, then upload the site/ directory to your host. If you publish to GitHub Pages with mkdocs gh-deploy, run that command again.
Does Loogika work with instant navigation in Material?
Yes. The navigation.instant feature of Material changes pages through the History API of the browser, without a full page load. Loogika records those page changes as pageviews when SPA Navigation is on in Site Settings. The setting is on for a new site.
How do you check that Loogika is installed?
Load your deployed docs in a fresh browser tab and visit a few pages. The pageviews show up in the Loogika dashboard within a few seconds. If nothing arrives, open the page source and look for script.js?siteId=.
Why does MkDocs not load the Loogika script?
| Problem | Cause and fix |
|---|---|
MkDocs reports a config error on defer | The form with path and defer needs MkDocs 1.5 or later. Run mkdocs --version. Then upgrade or use the plain address |
MkDocs ignores the file main.html | Material reads the override only after you set theme.custom_dir. Seen from mkdocs.yml, the path of the file must be overrides/main.html |
| Local previews show up in your reports | Both mkdocs serve and mkdocs build render from one and the same config. Add a second site in Loogika and use its ID for local work |
The page on localhost tracking describes more ways to keep development visits out of your data.