Next.js Installation
Integrate FeedbackJar into your Next.js application.
App Router (Next.js 13+)
Add Script to Root Layout
In your app/layout.tsx:
import Script from 'next/script';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Script
src="https://cdn.feedbackjar.com/sdk.js"
strategy="afterInteractive"
/>
<Script id="feedbackjar-init" strategy="afterInteractive">
{`window.fj.init('${process.env.NEXT_PUBLIC_FEEDBACKJAR_ID}');`}
</Script>
</body>
</html>
);
}
Environment Variables
Add to .env.local:
NEXT_PUBLIC_FEEDBACKJAR_ID=your_widget_id
Pages Router (Next.js 12 and below)
Add to _app.tsx
import Script from 'next/script';
import type { AppProps } from 'next/app';
export default function App({ Component, pageProps }: AppProps) {
return (
<>
<Component {...pageProps} />
<Script
src="https://cdn.feedbackjar.com/sdk.js"
strategy="afterInteractive"
/>
<Script id="feedbackjar-init" strategy="afterInteractive">
{`window.fj.init('${process.env.NEXT_PUBLIC_FEEDBACKJAR_ID}');`}
</Script>
</>
);
}
User Identification with Server Components
Create a client component for user identification:
'use client';
import { useEffect } from 'react';
export function FeedbackJarIdentify({ user }: { user: User }) {
useEffect(() => {
if (window.fj && user) {
window.fj.identify({
id: user.id,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
});
}
}, [user]);
return null;
}
Use it in your layout:
import { FeedbackJarIdentify } from './FeedbackJarIdentify';
import { getUser } from '@/lib/auth';
export default async function RootLayout({ children }) {
const user = await getUser();
return (
<html>
<body>
{children}
{user && <FeedbackJarIdentify user={user} />}
</body>
</html>
);
}
Route-Specific Configuration
Show widget only on specific routes:
'use client';
import { useEffect } from 'react';
import { usePathname } from 'next/navigation';
export function FeedbackJarControl() {
const pathname = usePathname();
useEffect(() => {
if (!window.fj) return;
// Hide on auth pages
if (pathname.startsWith('/auth')) {
window.fj.setWidgetEnabled(false);
} else {
window.fj.setWidgetEnabled(true);
}
}, [pathname]);
return null;
}