docs: Event listener doc (#2432)

Co-authored-by: Johannes <johannes@formbricks.com>
Co-authored-by: S P <spalriwalau@gmail.com>
This commit is contained in:
Dhruwang Jariwala
2024-04-22 17:52:13 +05:30
committed by GitHub
parent c9a8d022b4
commit 2ff17c2b22
3 changed files with 100 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
import { TellaVideo } from "@/components/docs/TellaVideo";
export const metadata = {
title: "Embed Surveys in Your Web Page",
description: "Embed Formbricks surveys seamlessly into your website or web application using an iframe.",
};
#### 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?
<TellaVideo tellaVideoIdentifier="clvavyy2f00000fjr0mple922"/>
1. Create and publish a link survey.
2. Open survey summary page and click on **share** button on the top right.
3. In the survey share modal, click on **Embed survey** button.
4. Navigate to **Embed in a Web Page** tab and click on Copy code
5. Paste the copied iframe code into the HTML of your web page where you want the survey to appear.
### Example of Embedding a Survey
<Col>
<CodeGroup title="Example Embedding Code">
```html
<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>
```
</CodeGroup>
</Col>
## 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?
1. Embed the Formbricks survey on your webpage using the iframe method as described above.
2. Add an event listener to your webpages JavaScript that listens for `message` events from the iframe.
3. Check if the received message indicates that the survey is completed by comparing the `event.data` with the value `formbricksSurveyCompleted`.
<Note>
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.
</Note>
4. Implement your custom actions within the callback function based on the survey completion.
### Example of Handling Survey Completion Events
<Col>
<CodeGroup title="Example Code for Event Listener">
```javascript
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
}
});
```
</CodeGroup>
</Col>

View File

@@ -210,6 +210,7 @@ export const navigation: Array<NavGroup> = [
{ title: "Source Tracking", href: "/docs/link-surveys/source-tracking" },
{ title: "Hidden Fields", href: "/docs/link-surveys/hidden-fields" },
{ title: "Start At Question", href: "/docs/link-surveys/start-at-question" },
{ title: "Embed Surveys", href: "/docs/link-surveys/embed-surveys" },
],
},
{

View File

@@ -0,0 +1,18 @@
import React from "react";
export function TellaVideo({ tellaVideoIdentifier }: { tellaVideoIdentifier: string }) {
return (
<div>
<iframe
className="aspect-video"
style={{
width: "100%",
height: "100%",
border: 0,
}}
src={`https://www.tella.tv/video/${tellaVideoIdentifier}/embed?b=0&title=0&a=1&loop=0&autoPlay=true&t=0&muted=1&wt=0`}
allowFullScreen={true}
title="Tella Video Help"></iframe>
</div>
);
}