Menu

Using Pre-Engagement Form Data and Context

This guide is for Flex UI 1.x and channels that use Programmable Chat and Proxy. If you are using Flex UI 2.x or you are starting out, we recommend that you build with Flex Conversations.

When using chat as a communications channel for your contact center, you can configure a pre-engagement form to gather relevant user information (such as name and email) before the start of a chat. Alternatively, you can gather relevant context from the data you already have, such as a user's login name or HTTP referer. You can use pre-engagement form data and context for routing the task to the right agent or displaying relevant user information to the agent.

Before you begin

To initiate a chat, a user's friendlyName must be set. The friendlyName attribute is set to "Anonymous" in the context object of your default Webchat configuration. The friendlyName value is displayed to the agent in the Flex UI. When set in both the Webchat context object and a pre-engagement form, the pre-engagement form value overrides the context object value.

Configuring a pre-engagement form

The pre-engagement form is disabled by default. To require details from your customers before they engage with your agents, enable the pre-engagement form by setting the startEngagementOnInit attribute to false in your Configuration object.

Loading Code Sample...
        
        

        Enable Pre-Engagement Form

        Loading Code Sample...
              
              

              Enable Pre-Engagement Context

              Within your Webchat configuration, you can define the preEngagementConfig object for the PreEngagementCanvas component.

              The following example form shows a required input field for the username, an optional text area field for the user's question, and a submit button with the label "Ok Let's Go!"

              {}

              Example Pre-Engagement Form

              PreEngagementCanvas

              Loading Code Sample...
                    
                    

                    Webchat configuration with Pre-Engagement Form

                    Supported field types and validations

                    Field Types

                    • InputItem
                    • SelectItem
                    • TextareaItem

                    Field Validations

                    Field Attribute Description
                    required Validates whether a form field is required or optional. Can be set to true or false.
                    email.value Checks the validity of the value attribute for an email input item.

                    The following example shows a preEngagementConfig object with all supported form field types and their properties and attributes.

                    Loading Code Sample...
                          
                          

                          Pre-Engagement Config Field Types

                          Posting user questions as a chat message

                          You can use WebChat actions to trigger your Flex Webchat Flow to automatically post user questions from a pre-engagement form to a chat window.

                          The following code sample shows how you can pass the question (if any) from the submitted formData and post it to chat using the StartEngagement post-action event.

                          // post question from pre-engagement into chat
                          
                            FlexWebChat.Actions.on("afterStartEngagement", (payload) => {
                                        const { question } = payload.formData;
                                        if (!question) return;
                          
                                        const { channelSid } = manager.store.getState().flex.session;
                                        manager.chatClient.getChannelBySid(channelSid)
                                            .then(channel => {
                                                channel.sendMessage(question);
                                            });
                                    });
                          

                          When posting an initial message on behalf of the customer, consider changing the introductory message or turning off the predefinedMessage MessagingCanvas prop.

                          
                          FlexWebChat.MessagingCanvas.defaultProps.predefinedMessage = false;

                          To learn more about using React Default Props to configure your Webchat components, see Webchat Configuration: React default props API.

                          Gathering and sending context

                          The following code sample shows how you can set the context object in your Webchat configuration:

                          const defaultConfiguration: Config = {
                            ...
                              context: {
                                  locationOrigin: window.location.origin,
                                  someContextProp: "ContextProp1",
                              }
                          }
                          

                          If context is set, the context prop will be saved as a chat channel attribute and can be accessed by the Send to Flex Studio widget.

                          Accessing and using pre-engagement data and context

                          When enabled, pre-engagement form and context data are both automatically saved as Programmable Chat channel attributes (i.e., channel.attributes.pre_engagement_data ) when a chat is initiated.

                          Your form and context data can then be accessed in the WebChat Studio Flow. Check out the Twilio Studio documentation to learn more about how Studio uses variables.You can also save pre-engagement and context data as chat channel attributes:

                          // context set in the WebChat
                          
                              context: {
                                  friendlyName: "Anonymous",
                                  locationOrigin: "http://someOriginUrl.com",
                                  someContextProp: "ContextProp1",
                              },
                          
                          // pre-engagement config set in WebChat
                          
                          preEngagementConfig: {    
                          
                              description: "Please provide some information",
                              fields: [
                                  {
                                      label: "What is your name?",
                                      type: "InputItem",
                                      attributes: {
                                          name: "friendlyName",
                                          type: "text",
                                          required: true
                                      }
                                  },
                                  {
                                      label: "What is your question?",
                                      type: "TextareaItem",
                                      attributes: {
                                          name: "question",
                                          type: "text",
                                          placeholder: "Type your question here",
                                          required: false,
                                          rows: 5
                                      }
                                  }, 
                              ],
                              submitLabel: "Ok Let's Go!"
                          }
                          
                          // Chat channel attributes saved on chat initiation
                          
                          "attributes": "{\"status\":\"ACTIVE\",\"long_lived\":false,\"pre_engagement_data\":{\"friendlyName\":\"Anonymous\",\"question\":\"Can you help me with my invoice?\",\"location\":\"http://localhost:8081/\",\"locationOrigin\":\"http://someOriginUrl.com\",\"someContextProp\":\"ContextProp1\"},\"from\":\"Bob\",\"channel_type\":\"web\"}"
                          

                          Pre-engagement and context data in Studio

                          An incoming message sent to the chat channel triggers the Webchat Flow for your Flex instance, which you can customize within the Twilio Console.

                          In the Webchat Studio flow, you can:

                          • Trigger a bot conversation based on gathered pre-engagement data
                          • Use pre-engagement data for decisions in the flow, like when to send the conversation to an agent
                          • Add pre-engagement data to task attributes for routing decision or information display to an agent in Flex

                          Studio uses Liquid syntax to access the pre-engagement data in a Studio widget. For example, here's how you would access the question attribute from your pre-engagement form data:

                          
                          "{{trigger.message.ChannelAttributes.pre_engagement_data.question}}"

                          And here's how you would add the initial user question to your chat task attributes in the Send To Flex widget:

                          Send to Flex widget initial question

                          {"initial_question": "{{trigger.message.ChannelAttributes.pre_engagement_data.question}}"}

                          You may access your pre-engagement form and context data via the Programmable Chat REST API.

                          Next Steps

                          Rate this page:

                          Need some help?

                          We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

                          Thank you for your feedback!

                          Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                          Sending your feedback...
                          🎉 Thank you for your feedback!
                          Something went wrong. Please try again.

                          Thanks for your feedback!

                          thanks-feedback-gif