Next.js
Add analytics to a Next.js app with one component. Pageviews and route changes are tracked automatically.
Install
npm install @databuddy/sdkNEXT_PUBLIC_DATABUDDY_CLIENT_ID=your-client-idYour Client ID is in the website's tracking setup at app.databuddy.cc. It's public; never put server API keys in NEXT_PUBLIC_ variables.
Add the provider once
Deploy
Set NEXT_PUBLIC_DATABUDDY_CLIENT_ID in your hosting provider and rebuild; Next.js embeds it at build time. Visits from localhost aren't sent.
Track a signup
Track the click in the browser and the finished signup on the server, so a funnel shows who started and who finished.
Never send emails, passwords, tokens or whole form values as properties.
Turn on errors and Web Vitals
<DatabuddyProvider
clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
autocapture={{ webVitals: true, errors: true }}
/>Error tracking needs the Hobby plan or higher. Add source maps with withDatabuddy() in next.config.ts to see your original code in stack traces.
Keep test data out
<DatabuddyProvider
clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
disabled={process.env.NEXT_PUBLIC_VERCEL_ENV !== "production"}
/>NODE_ENV is production on most preview deployments too, so use your host's environment flag (NEXT_PUBLIC_VERCEL_ENV on Vercel), or give previews their own website.
Keep visitors who use ad blockers
Blockers often stop cdn.databuddy.cc and basket.databuddy.cc. Serve both from your own domain:
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
async rewrites() {
return [
{ source: "/_p/s.js", destination: "https://cdn.databuddy.cc/databuddy.js" },
{ source: "/_p/e/:path*", destination: "https://basket.databuddy.cc/:path*" },
];
},
};
export default nextConfig;<DatabuddyProvider
clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
scriptUrl="/_p/s.js"
apiUrl="/_p/e"
/>No data?
Related
How is this guide?