DataSag guides · Updated 27 October 2026
Track custom events on PHP templates
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 PHP templates.
Start with the PHP templates installation
Add the deferred external script once to the local PHP include that emits your public HTML head. Preserve the existing document structure and include that file through a known local path in the pages you intend to measure. The tracking snippet itself is HTML; it does not need a PHP analytics library.
An independent PHP endpoint may return JSON, redirect before rendering, or use another header include. Keep the tracker in rendered HTML and avoid inserting output before code that needs to send response headers. Do not derive an include path from visitor input.
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 PHP templates
- For a click goal, put data-datasag-goal on the actual PHP-rendered control. Keep the public event name stable and avoid generating it from sensitive visitor data.
- For completion, let browser code inspect the successful PHP response before calling the API. Alternatively use a deliberately designed confirmation page whose access conditions actually establish success; a server branch alone cannot invoke the browser API.
- Test accepted input, rejected input and a repeated visit to the confirmation flow. Inspect the named event and ensure reloading a generic success-looking document does not misrepresent another newly completed business action.
The PHP frontend reports the defined observable action once, with server completion and browser confirmation handled explicitly.
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 an HTML goal attribute for clicks, or invoke the API from browser code after a confirmed response. A server-side PHP success branch cannot directly call the browser API unless it renders an appropriate page or response for browser code to handle.
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 PHP templates
The shared installation belongs in the head emitted by the shared PHP header include. 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 PHP templates guides
References
Official PHP templates documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.