DataSag guides · Updated 27 October 2026
Track custom events on Gatsby
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 Gatsby.
Start with the Gatsby installation
Use the Gatsby SSR onRenderBody API to add one deferred external script through setHeadComponents. Merge the addition into an existing hook rather than overwriting it, and keep the React import required by your file format. Build and publish the generated site; inspect its HTML because this hook concerns generated documents.
A browser onRouteUpdate hook, a plugin and gatsby-ssr can all add analytics behavior. Do not add a second pageview sender without testing the current tracker. gatsby develop is not a substitute for checking generated production documents.
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 Gatsby
- Put the event in the React browser handler that receives a successful action result. Gatsby SSR and build-time code run in Node and cannot use the visitor window.
- Keep the global script in the SSR head hook and consume it through a typed optional API from the component. Do not install another script in a page useEffect just to make its action handler work.
- Test a successful and failed action after direct entry and after Gatsby Link navigation. Check that a retained component state or repeated effect does not report an old success again. Inspect the exact named payload.
The Gatsby browser action records a new confirmed outcome once without build-time window calls or a page-local loader.
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' });Call the browser API from a React action handler after success. Gatsby build-time Node execution and SSR do not have the visitor window. Keep the global loader separate from individual page components.
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 Gatsby
The shared installation belongs in an onRenderBody head component in the root gatsby-ssr.js file. Use this as the installation reference while checking the procedure above.
const React = require("react");
// Merge into your existing onRenderBody hook if one exists.
exports.onRenderBody = ({ setHeadComponents }) => {
setHeadComponents([
<script key="datasag" defer
src="https://www.datasag.com/tracker.js"
data-datasag-key="YOUR_PUBLIC_KEY" />
]);
};Related Gatsby guides
References
Official Gatsby documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.