DataSag guides · Updated 27 October 2026

Track custom events on Docusaurus

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 Docusaurus.

Start with the Docusaurus installation

Add an object to the existing Docusaurus scripts array with src, defer and data-datasag-key attributes. Preserve the rest of your configuration. Build and serve the generated production site, then test a documentation route directly and through client navigation. The config runs in Node.js, so browser event calls do not belong there.

Docusaurus accepts string script entries, but an object is needed here to preserve the public tracking-key attribute and explicit non-blocking loading. Do not also insert the tracker through a client module, swizzled theme component or individual MDX page.

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 Docusaurus

  1. Place action tracking in the React component that actually receives the browser interaction. Keep the external loader in config. A goal attribute on rendered MDX or JSX markup can measure a click without a second event listener.
  2. For TypeScript components, type the optional browser API through a narrow Window declaration or helper. Do not reference window from the Node-evaluated config or during a server render of documentation content.
  3. Test the component after a direct load and after internal sidebar navigation. A successful external signup cannot be inferred from a documentation button click; count confirmed completion only when your application receives that outcome.

Interactive documentation components emit their defined browser events without server-render access or repeated route-mounted loaders.

Complete the collection checks

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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' });

Use a React browser handler in an interactive component or a goal attribute on rendered markup. For a completed action, wait for its actual success callback; a documentation link click only establishes intent.

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 Docusaurus

The shared installation belongs in the scripts array in docusaurus.config.js or docusaurus.config.ts. Use this as the installation reference while checking the procedure above.

// Merge this entry into the existing scripts array.
export default {
  scripts: [{
    src: "https://www.datasag.com/tracker.js",
    defer: true,
    "data-datasag-key": "YOUR_PUBLIC_KEY"
  }]
};

Related Docusaurus guides

Full Docusaurus setup guide · Search and AI visibility

References

Official Docusaurus documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.