DataSag guides · Updated 27 October 2026
Track custom events on Flask with Jinja
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 Flask with Jinja.
Start with the Flask with Jinja installation
Put the deferred external script once in the shared Jinja base template under your application templates directory. Preserve existing blocks, imports and asset links. Confirm that render_template calls for your public routes select children of that base. The tracker remains an external browser script rather than Python application code.
Blueprint-specific template directories and independent render_template_string responses can bypass the base template. A child head block can replace inherited content unless it retains it. Inspect the generated response from the affected endpoint.
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 Flask with Jinja
- Add the goal attribute to the Jinja-rendered element for click intent. If a macro builds the control, pass the attribute through the macro and inspect the resulting DOM.
- For confirmed completion, call the API from browser code after the Flask endpoint returns success. Keep Python response creation separate from the browser event. A form submit event can still lead to a rejected request.
- Test accepted and rejected input against the published endpoint. If the frontend replaces a partial after success, verify the handler is not attached repeatedly. Inspect the exact event name and properties rather than inferring success from a visible button state.
The rendered Flask control or success callback emits one event for the action it can actually observe.
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 click intent or call the API from a browser handler after success. A Flask view, Python callback or background job cannot directly call the visitor window.
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 Flask with Jinja
The shared installation belongs in the head of the Jinja base template used by your routes. 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 Flask with Jinja 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 Flask with Jinja setup guide · Search and AI visibility
References
Official Flask with Jinja documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.