Angular
Add Databuddy once and every route change in your Angular app counts as a pageview.
Add Databuddy
Your Client ID is in the dashboard under your website's Settings → General. Router navigations are tracked automatically, so you don't need a NavigationEnd subscription.
Track an event
import { Component } from "@angular/core";
import { track } from "@databuddy/sdk";
@Component({
selector: "app-checkout",
template: `<button (click)="complete('ord_123', 49)">Pay</button>`,
})
export class CheckoutComponent {
complete(orderId: string, total: number): void {
track("purchase_complete", { order_id: orderId, total, currency: "USD" });
}
}track() is typed and works with either install. Calls made before the tracker loads wait for it, and calls on the server are skipped.
Track clicks from templates
Add data-track-attributes to the script tag, or attributes: true to autocapture, then name elements in any template:
<a routerLink="/pricing" data-track="pricing_click" data-location="hero">View pricing</a>Each click sends a pricing_click event with location: "hero".
Use a different Client ID per environment
export const environment = { databuddyClientId: "YOUR_PROD_CLIENT_ID" };Pass environment.databuddyClientId to init(). Angular's fileReplacements picks the right file for each build.
Related
How is this guide?