REDCap Integration ================== Overview -------- Use ChatbotLab to embed conversational tasks directly into your REDCap data collection instruments. Each REDCap form can display the ChatbotLab chat window within a **Descriptive Text** field using an iframe. .. important:: REDCap must allow HTML and JavaScript in Descriptive Text fields. Some institutions disable this for security reasons. Contact your REDCap administrator if iframes are not permitted. Data Needed to Embed Bot ------------------------ Record ID ^^^^^^^^^ REDCap automatically assigns each participant a unique ``record_id``. This is available as a built-in variable and can be passed to ChatbotLab to identify conversations. In ChatbotLab, ``record_id`` serves the same purpose as Qualtrics's ``ResponseID``. Bot Name ^^^^^^^^ 1. In the ChatbotLab admin panel, navigate to **CHATBOT → Bots**. 2. Find the bot you wish to embed. 3. Copy its **Name** from the table. 4. Replace ```` in the iframe URL below. Study Name ^^^^^^^^^^ Define a short descriptive name for your study, for example ``therapy_bot``. This will be stored in the ChatbotLab database under ``study_name`` for tracking. Participant ID (Optional) ^^^^^^^^^^^^^^^^^^^^^^^^^ If your REDCap project includes a custom participant ID field, you can reference it in the iframe using a REDCap variable such as ``[participant_id]``. If you do not have a custom field, you can rely on ``[record-id]`` as the participant identifier. Embedding ChatbotLab --------------------- 1. Deploy ChatbotLab on AWS following the :doc:`/deployment/index` guide. 2. In your REDCap project, open the desired instrument in the **Online Designer**. 3. Add a new **Descriptive Text** field. 4. Click the **pencil icon** to edit the field. 5. In the **Field Label** box, click the **“<>” (HTML)** icon and paste this code: .. code-block:: html 6. Replace the placeholders: - ````: your bot's name - ````: your chosen study label - ````: your unique REDCap survey identifier - ````: your ChatbotLab domain, from deployment - ```` (optional): unique identifier for participants 7. Save and preview the form to verify that the chat window loads correctly. Passing Data ------------ ChatbotLab automatically logs any metadata passed via URL parameters: - ``participant_id``: usually ``[record-id]`` or a custom ID field - ``study_name``: study descriptor - ``bot_name``: determines which ChatbotLab bot instance to use Data Linking ------------ - Each conversation is stored with its associated REDCap record ID. - Conversations are linked in ChatbotLab’s database by ``participant_id`` and ``study_name``. - You can merge these logs with exported REDCap data using ``record_id``. Keystrokes (Optional) --------------------- To capture engagement metrics (time on page, keystrokes, focus changes), you can insert an optional script field after the chat iframe. .. code-block:: html This mirrors the Qualtrics example and sends timing data to ChatbotLab for each participant. Validation ---------- 1. Test the form in **Data Entry** or **Survey Mode**. 2. Check your browser console for any loading or network errors. 3. Verify that a new conversation record appears in ChatbotLab’s admin panel. 4. Confirm that keystroke and timing data (if enabled) are logged. Other Options ------------- - You can add instructions or task descriptions above the iframe in the same **Descriptive Text** field. - To randomize bots between participants, define a REDCap field (e.g., ``[bot_condition]``) and use it in the iframe: .. code-block:: html bot_name=[bot_condition]