DataSag guides · Updated 27 October 2026
Track custom events on Drupal Twig themes
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 Drupal Twig themes.
Start with the Drupal Twig themes installation
Use a maintained custom theme or subtheme and identify its html.html.twig document wrapper. Add the deferred tracker inside head while preserving Drupal's existing placeholders, attributes and attached assets. Rebuild the relevant caches and inspect the public rendered document. Do not edit Drupal core or replace the full wrapper with a minimal example.
The active public theme and the administration theme can differ. A node or block template is not the global document wrapper, and Ajax-rendered fragments do not represent independent document loads. Check public routes, caching and any separately configured theme before assuming complete coverage.
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 Drupal Twig themes
- Choose an owned theme element or browser component that can observe the intended action. Use a goal attribute for simple click intent, or a browser success handler for a confirmed form or application result.
- Implement custom listeners through Drupal's supported behavior lifecycle and once-only pattern for the installed version. Ajax attachments can revisit existing contexts, so avoid repeatedly binding the same handler or reinstalling the global tracker.
- Test the initial public page and a form or component re-rendered through Ajax. Include failed validation and successful submission where relevant. Keep PHP preprocessing and Twig evaluation separate from browser event dispatch and user-success claims.
The action produces one defined browser event after the correct boundary, including after repeated Ajax behavior attachments.
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' });Use a goal attribute in markup owned by your theme for click intent. Custom JavaScript action handling must respect Drupal behaviors and repeated Ajax attachments, and emit success only after confirmation. PHP preprocessing and Twig rendering cannot invoke a visitor's browser API.
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 Drupal Twig themes
The shared installation belongs in the head in your custom theme html.html.twig override. 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 Drupal Twig themes 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 Drupal Twig themes setup guide · Search and AI visibility
References
Official Drupal Twig themes documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.