Framework Guides
One can integrate Formbricks App Survey SDK into their app using multiple options! Checkout the options below that we provide! If you are looking for something else, please join our Discord! and we would be glad to help.
Prerequisites
Before getting started, make sure you have:
- A web application (behind your user authentication system) in your desired framework is set up and running.
- A Formbricks account with access to your environment ID and API host. You can find these in the Setup Checklist in the Settings.
HTML
All you need to do is copy a <script>
tag to your HTML head, and that’s about it!
HTML
<!-- START Formbricks Surveys -->
<script type="text/javascript">
!function(){
var apiHost = "https://app.formbricks.com";
var environmentId = "<your-environment-id>";
var userId = "<your-user-id>"; //optional
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=apiHost+"/js/formbricks.umd.cjs";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.init({environmentId: environmentId, apiHost: apiHost, userId: userId})},500)}();
</script>
<!-- END Formbricks Surveys -->
Required customizations to be made
- Name
environment-id
- Type
- string
- Description
Formbricks Environment ID.
- Name
api-host
- Type
- string
- Description
URL of the hosted Formbricks instance.
Now visit the Validate your Setup section to verify your setup!
ReactJS
Install the Formbricks SDK using one of the package managers ie npm
,pnpm
,yarn
. Note that zod is required as a peer dependency must also be installed in your project.
Install Formbricks JS library
npm install @formbricks/js zod
Now, update your App.js/ts file to initialise Formbricks.
src/App.js
// other imports
import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>", //optional
});
}
function App() {
// your own app
}
export default App;
Required customizations to be made
- Name
environment-id
- Type
- string
- Description
Formbricks Environment ID.
- Name
api-host
- Type
- string
- Description
URL of the hosted Formbricks instance.
Now visit the Validate your Setup section to verify your setup!
NextJS
NextJs projects typically follow two main conventions: the App Directory and the Pages Directory. To ensure smooth integration with the Formbricks SDK, which operates solely on the client side, follow the guidelines for each convention below:
- App directory: You will have to define a new component in
app/formbricks.tsx
file and call it in yourapp/layout.tsx
file. - Pages directory: You will have to visit your
_app.tsx
and just initialise Formbricks there.
Code snippets for the integration for both conventions are provided to further assist you.
Install Formbricks JS library
npm install @formbricks/js zod
App Directory
app/formbricks.tsx
"use client";
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import formbricks from "@formbricks/js";
export default function FormbricksProvider() {
const pathname = usePathname();
const searchParams = useSearchParams();
useEffect(() => {
formbricks.init({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>", //optional
});
}, []);
useEffect(() => {
formbricks?.registerRouteChange();
}, [pathname, searchParams]);
return null;
}
app/layout.tsx
// other imports
import FormbricksProvider from "./formbricks";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<FormbricksProvider />
<body>{children}</body>
</html>
);
}
Pages Directory
src/pages/_app.tsx
// other import
import { useRouter } from "next/router";
import { useEffect } from "react";
import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>", //optional
});
}
export default function App({ Component, pageProps }: AppProps) {
const router = useRouter();
useEffect(() => {
// Connect next.js router to Formbricks
const handleRouteChange = formbricks?.registerRouteChange;
router.events.on("routeChangeComplete", handleRouteChange);
return () => {
router.events.off("routeChangeComplete", handleRouteChange);
};
}, []);
return <Component {...pageProps} />;
}
Required customizations to be made
- Name
environment-id
- Type
- string
- Description
Formbricks Environment ID.
- Name
api-host
- Type
- string
- Description
URL of the hosted Formbricks instance.
First we initialize the Formbricks SDK, ensuring that it only runs on the client side. To connect the Next.js router to Formbricks and ensure the SDK can keep track of every page change, we are registering the route change event.
Now visit the Validate your Setup section to verify your setup!
VueJs
Integrating the Formbricks SDK with Vue.js is a straightforward process. We will make sure the SDK is only loaded and used on the client side, as it's not intended for server-side usage.
Install Formbricks JS library
npm install @formbricks/js
src/formbricks.js
import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>", //optional
});
}
export default formbricks;
src/main.js
// other imports
import formbricks from "@/formbricks";
const app = createApp(App);
app.use(router);
app.mount("#app");
router.afterEach((to, from) => {
if (typeof formbricks !== "undefined") {
formbricks.registerRouteChange();
}
});
Required customizations to be made
- Name
environment-id
- Type
- string
- Description
Formbricks Environment ID.
- Name
api-host
- Type
- string
- Description
URL of the hosted Formbricks instance.
Now visit the Validate your Setup section to verify your setup!
React Native
Install the Formbricks React Native SDK using one of the package managers, i.e., npm, pnpm, or yarn.
Install Formbricks JS library
npm install @formbricks/react-native
Now, update your App.js/App.tsx file to initialize Formbricks:
src/App.js
// other imports
import Formbricks from "@formbricks/react-native";
const config = {
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>",
};
export default function App() {
return (
<>
{/* Your app content */}
<Formbricks initConfig={config} />
</>
);
}
Required customizations to be made
- Name
environment-id
- Type
- string
- Description
Formbricks Environment ID.
- Name
api-host
- Type
- string
- Description
URL of the hosted Formbricks instance.
Validate your setup
Once you have completed the steps above, you can validate your setup by checking the Setup Checklist in the Settings. Your widget status indicator should go from this:
To this:
Debugging Formbricks Integration
Enabling Formbricks debug mode in your browser is a useful troubleshooting step for identifying and resolving complex issues. This section outlines how to activate debug mode, covers common use cases, and provides insights into specific debug log messages.
Activate Debug Mode
To activate Formbricks debug mode:
-
Via URL Parameter:
- Enable debug mode mode by adding
?formbricksDebug=true
to your application's URL (e.g.https://example.com?formbricksDebug=true
orhttps://example.com?page=123&formbricksDebug=true
). This parameter will enable debugging for the current page.
- Enable debug mode mode by adding
-
View Debug Logs:
-
Open your browser's developer tools by pressing
F12
or right-clicking and selecting "Inspect." -
Navigate to the "Console" tab to view Formbricks debugging information.
How to Open Browser Console:
- Google Chrome: Press
F12
or right-click, select "Inspect," and go to the "Console" tab. - Firefox: Press
F12
or right-click, select "Inspect Element," and go to the "Console" tab. - Safari: Press
Option + Command + C
to open the developer tools and navigate to the "Console" tab. - Edge: Press
F12
or right-click, select "Inspect Element," and go to the "Console" tab.
- Google Chrome: Press
-
Common Use Cases
Debug mode is beneficial for scenarios such as:
- Verifying Formbricks initialization.
- Identifying survey trigger issues.
- Troubleshooting unexpected behavior.
Debug Log Messages
Debug log messages provide insights into:
- API calls and responses.
- Event tracking, survey triggers and form interactions.
- Initialization errors.
Can’t figure it out? Join our Discord!