Embed Surveys
Embed Surveys in Your Web Page
Embedding Formbricks surveys directly into your web pages allows you to integrate interactive surveys without redirecting users to a separate survey site. This method ensures a seamless integration and maintains the aesthetic continuity of your website or application.
How to Use it?
-
Create and publish a link survey.
-
Open survey summary page and click on share button on the top right.
-
In the survey share modal, click on Embed survey button.
-
Navigate to Embed in a Web Page tab and click on Copy code
-
Paste the copied iframe code into the HTML of your web page where you want the survey to appear.
Example of Embedding a Survey
Example Embedding Code
<div style="position: relative; height:100vh; max-height:100vh; overflow:auto;">
<iframe
src="https://app.formbricks.com/s/<your-surveyId>"
frameborder="0"
style="position: absolute; left:0; top:0; width:100%; height:100%; border:0;">
</iframe>
</div>
Iframe Events
The iframe fires a formbricksSurveyCompleted event when a user finishes a survey within the embedded iframe. This event can be captured through a message listener in your webpage's JavaScript
How to Use it?
-
Embed the Formbricks survey on your webpage using the iframe method as described above.
-
Add an event listener to your webpage’s JavaScript that listens for
message
events from the iframe. -
Check if the received message indicates that the survey is completed by comparing the
event.data
with the valueformbricksSurveyCompleted
.
It is important to verify the origin of the message to ensure it comes from the iframe containing your survey, enhancing the security of your event handling.
- Implement your custom actions within the callback function based on the survey completion.
Example of Handling Survey Completion Events
Example Code for Event Listener
window.addEventListener("message", (event) => {
// Replace 'https://app.formbricks.com' with the actual web app url
if (event.origin === "https://app.formbricks.com" && event.data === "formbricksSurveyCompleted") {
console.log("Survey completed!");
// Implement your custom actions here
}
});