DataSag guides · Updated 27 October 2026
Track custom events on Material for MkDocs
Use data-datasag-goal for a click goal, or call window.datasag.track after a completed action. Choose a stable event name and distinguish a click from a successful conversion. This guide applies to Material for MkDocs.
Start with the Material for MkDocs installation
Configure a local theme custom_dir and extend base.html in overrides/main.html. Add the deferred tracker inside an extrahead block while preserving parent content with super(). Merge these changes into your existing MkDocs configuration, build the site and inspect the generated document. Do not edit the installed theme package.
Material may use instant navigation, which changes the reading experience without a complete document reload. Install the global tracker once and test actual path changes. A replacement block that omits parent content can remove unrelated theme functionality.
Use the public project key from the DataSag project you are inspecting. A test on a preview domain does not confirm that the production domain has the same code or settings.
Track custom events in Material for MkDocs
- Add a goal attribute to HTML markup that the Markdown pipeline preserves, such as an owned download link. Inspect the built page before testing because source Markdown and final element attributes can differ.
- If a custom JavaScript action observes success, invoke the existing tracker rather than injecting a new copy. For instant navigation, initialize and clean up your action handlers using the theme's supported document lifecycle.
- Test the action after a direct load and after leaving and returning through an instant-navigation link. One interaction should create one named event. A documentation link to an external account flow measures a click, not a completed account.
Generated markup or lifecycle-aware handlers record one defined action without rebuilding the global tracker on page changes.
Complete the collection checks
- Write the event definition first. For example, signup_click means a visitor pressed a signup link, while signup_completed means your application confirmed account creation.
- For click intent, add data-datasag-goal to the element. The tracker delegates clicks, so you do not also need a JavaScript handler for the same goal.
- For a completed action, call the JavaScript API in the success handler. Wait for the tracker to be available. An optional guard avoids a crash but intentionally drops the event if the tracker has not loaded.
- Send only the context needed for the report, such as a form or plan name. Do not include email addresses, passwords, payment details or freeform form contents.
- Complete one successful and one failed attempt. Inspect /api/track for the exact eventName and properties. Only the successful attempt should emit a completed-action event.
- Check the event report available to your plan. Collection and access to reports are separate; review current plan details before depending on a particular report.
<!-- Click intent: use this OR your own click handler -->
<a href="/signup" data-datasag-goal="signup_click">Create account</a>
// JavaScript: inside the confirmed-success handler
window.datasag?.track('signup_completed', { form: 'main_signup' });For click goals, use data-datasag-goal on HTML markup preserved in the generated page. Custom browser success handlers can call the existing API. Under instant navigation, bind your own handlers through the documented lifecycle rather than repeatedly installing the tracker.
What a successful check looks like
A controlled successful action produces one event with the agreed name. A validation failure does not emit a success event. Network evidence matches the report definition.
Record the public path you tested, the browser state, the approximate time and the observed request. Check one direct page load and one later navigation so the result covers more than the homepage.
Common mistake
A goal attribute on a submit button measures clicks, including attempts that fail. Do not report those clicks as completed signups or purchases.
Installation snippet for Material for MkDocs
The shared installation belongs in an extrahead block in a theme override. Use this as the installation reference while checking the procedure above.
# Merge into mkdocs.yml:
theme:
name: material
custom_dir: overrides
<!-- overrides/main.html -->
{% extends "base.html" %}
{% block extrahead %}
{{ super() }}
<script defer src="https://www.datasag.com/tracker.js" data-datasag-key="YOUR_PUBLIC_KEY"></script>
{% endblock %}Related Material for MkDocs guides
- Verify DataSag analytics
- Fix DataSag analytics not working
- Fix duplicate analytics pageviews
- Track UTM campaigns
- Measure AI referral traffic
- Configure CSP for DataSag analytics
- Exclude your own analytics visits
- Load analytics without blocking rendering
Full Material for MkDocs setup guide · Search and AI visibility
References
Official Material for MkDocs documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.