DataSag guides · Updated 27 October 2026

Track custom events on Eleventy

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

Start with the Eleventy installation

Find the shared layout selected by your Eleventy data cascade, commonly an _includes/base.njk file. Add the deferred tracker to its head while preserving the content placeholder and existing template syntax. Build with your normal configuration and deploy the generated output. Layout paths depend on your project's directory settings.

Eleventy supports multiple template languages and chained layouts. A filename copied from an example may not be the active layout for your content. Inspect layout data and generated pages rather than assuming every collection uses the same wrapper.

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 Eleventy

  1. Add data-datasag-goal to a rendered link or button in a layout or content template. Use syntax appropriate to that file's template language and inspect the generated element before relying on the attribute.
  2. For completed actions, invoke the tracker from browser JavaScript after the form or application confirms success. Keep the call out of Eleventy data files, shortcodes that execute during generation and other server-side build helpers.
  3. Test the generated interaction on a collection page and any standalone page that reuses it. One click goal needs one ownership path; do not also attach a manual click handler that emits the identical event from the same element.

Generated markup or a browser success handler emits one defined action without confusing template generation with runtime interaction.

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 goal attribute in a generated link or button for click intent. For a completed action, invoke the browser API after success. Eleventy data files and template rendering run at build time and cannot observe a visitor completing a form.

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 Eleventy

The shared installation belongs in the head of the shared layout resolved from your includes directory. Use this as the installation reference while checking the procedure above.

<script defer src="https://www.datasag.com/tracker.js" data-datasag-key="YOUR_PUBLIC_KEY"></script>

Related Eleventy guides

Full Eleventy setup guide · Search and AI visibility

References

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