DocuChat Logo
Advanced Features

Session Extraction

Learn how to access and utilize embedded chat information for integrating DocuChat with your user tracking or analytics systems.

DocuChat provides methods to access information about embedded chat widgets on your page. This allows you to integrate chat sessions with your own user tracking or analytics systems. There are two ways to embed DocuChat: using the Chat Widget Embed or directly with an iFrame Embed.

When embedding DocuChat widgets on your website, you are responsible for providing appropriate data protection notices and obtaining necessary user consent as required by applicable laws and regulations (e.g., GDPR, CCPA). The chat information exposed through this API may contain personal data, and its collection and use must comply with your privacy policy and relevant data protection laws.

1. Chat Widget Embed

When using the DocuChat Widget script, chat information is automatically exposed through a global docuchat object on the parent window.

if (window.docuchat && window.docuchat.chatbots) {
  Object.entries(window.docuchat.chatbots).forEach(([chatbotId, chatInfo]) => {
    console.log(`DocuChat Info for bot ${chatbotId}:`, chatInfo);
  });
}
else {
  console.log("No DocuChat information available yet");
}

2. iFrame Embed

When embedding DocuChat directly using an iFrame, you'll need to handle the postMessage API yourself to receive chat information updates.

Listening for Chat Info Updates

window.addEventListener("message", (event) => {
  // Ensure the message is from your DocuChat embed URL
  if (event.origin !== "<https://app.docuchat.io>")
    return;

  if (event.data.type === "chatInfoUpdate") {
    console.log("Updated DocuChat Info:", event.data.chatInfo);
    // Handle the chat info update here
  }
});

Chat Information Structure

For both embedding methods, each chatInfo object contains the following properties:

  • chatbotId: The ID of the chatbot
  • chatbotName: The name of the chatbot
  • sessionId: The unique ID for the chat session