Next.js & custom code
Integrate the Kindle AI pixel in a Next.js, React, or custom stack: layout script, click-ID persistence, and server events.
When you own the code, the integration is three small pieces: the script in your root layout, the click ID persisted wherever your conversion flow starts, and a server event from your backend.
1. Pixel in the root layout
Next.js App Router:
// app/layout.tsx
import Script from "next/script";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Script
src="/media/cdn/pixel/v1/kindle-ai-pixel.js"
data-key="pk_YOUR_PIXEL_KEY"
strategy="afterInteractive"
/>
</body>
</html>
);
}
For any other stack, use the plain <script> tag before </body> in your base template.
Because you own the code, you can also serve the script from your own site: put a pinned version in public/kindle-ai/ and point src at /kindle-ai/kindle-ai-pixel.js. See Self-hosting the pixel.
2. Persist the click ID
Here is a small helper, used at the start of your conversion flow (the signup mutation, or checkout-session creation):
export function getKindle AIClickId(): string | null {
if (typeof document === "undefined") return null;
const cookie = document.cookie
.split("; ")
.find((entry) => entry.startsWith("kindle-ai_sid="));
if (!cookie) return null;
try {
const session = JSON.parse(
decodeURIComponent(cookie.split("=").slice(1).join("="))
);
return session.sid ?? null;
} catch {
return null;
}
}
Store the value on the business record that will eventually convert:
- Signup: include it in the signup mutation, writing to
accounts.kindle-ai_click_id(a nullable column). - Checkout: put it in the checkout session metadata (for example, Stripe
metadata.kindle-ai_click_id) and read it back in the payment webhook. - Cross-domain (marketing site to app): pass it through the URL. See the decorator script in the Framer guide.
null is a valid value: the cookie is missing when the pixel was blocked or the visitor hasn’t given consent. Never fabricate an ID.
3. Send the conversion from your backend
export async function sendKindle AIConversionEvent(event: {
event_id: string;
event_type: string;
kindle-ai_click_id: string | null;
value?: number;
currency?: string;
external_user_id?: string;
properties?: Record<string, unknown>;
}) {
if (!event.kindle-ai_click_id) return; // no Kindle AI session, so skip
const response = await fetch(
"https://api.trykindle.app/api/v1/pixel/server-events",
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.KINDLE AI_SERVER_EVENT_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(event),
}
);
return response.json();
}
Call it the moment the conversion is confirmed: the payment webhook, the post-verification signup handler, or the CRM sync. For the field reference and response codes, see the Conversion API. For which events to send, see the event guides.
Stack notes
- Server components / SSR: the cookie is first-party and readable on the server too (
cookies().get("kindle-ai_sid")in Next.js). Parsing it on the server during signup avoids a client round-trip. - SPAs: the pixel records the landing page view and client-side route changes automatically, so no manual page tracking is needed.
- Browser conversions (optional, for example instant-feedback events):
if (window.kindle) kindle-ai("lead", { value: 50 }). See Kindle AI Pixel. - CSP: allow
/media/cdn(script-src), andhttps://api.trykindle.appandhttps://kindle-aicore-production-d467.up.railway.app(connect-src). With a self-hosted pixel, onlyhttps://api.trykindle.app(connect-src) is needed.