Vanilla JavaScript
Add one script tag before </body> and pageviews are tracked from then on.
<script
src="https://cdn.databuddy.cc/databuddy.js"
data-client-id="your-client-id"
data-track-web-vitals
data-track-errors
async
></script>Every data-* option is in the script tag reference. Using a bundler? init() from @databuddy/sdk loads the same script, and its functions replace the window.databuddy calls below.
Track events
window.databuddy.track("signup_clicked", { source: "header" });Or with no JavaScript, add data-track-attributes to the script tag and mark elements:
<button data-track="signup_clicked" data-plan="pro">Sign up</button>A click sends signup_clicked with { plan: "pro" }. window.db is a shorter alias for window.databuddy.
Call the tracker
To put visitors in an account, set window.databuddy.setGlobalProperties({ group_workspace: workspace.id }) on every page.
Let visitors opt out
window.databuddyOptOut();
window.databuddyOptIn();The opt-out is remembered across visits. The tracker fires a databuddy:consent window event, "denied" or "granted", whenever tracking is withdrawn or allowed again. See opt-outs for what an opt-out stops.
Decide what is sent
Set hooks on window.databuddyConfig before the script loads. consent is read before every event and request, and beforeSend sees every event.
<script>
window.databuddyConfig = {
consent: () => !document.cookie.includes("site_consent=denied"),
beforeSend: (event) => {
if (event.type === "error") delete event.stack;
return true;
},
};
</script>
<script src="https://cdn.databuddy.cc/databuddy.js" data-client-id="your-client-id" async></script>To record session replays, add the recorder next to the tracker:
<script src="https://cdn.databuddy.cc/replay.js" data-client-id="your-client-id" async></script>Related
How is this guide?