Including webpage URL in VoiceFlow chatbot conversation logs

I want to capture the current page URL where users interact with my VoiceFlow chatbot and include it in the conversation transcripts. This would help me track which pages generate the most questions from users.

I attempted this approach but it’s not working:

(function(doc, tag) {
  var widget = doc.createElement(tag), firstScript = doc.getElementsByTagName(tag)[0];
  widget.onload = function() {
    window.voiceflow.chat.load({
      verify: { projectID: 'MY_PROJECT_ID_HERE' },
      url: 'https://general-runtime.voiceflow.com',
      versionID: 'production',
      launch: {
        event: {
            type: "launch",
            payload: {
            page_url: window.location.href
            }
        }
        },
        autostart: false
    });
  }
  widget.src = "https://cdn.voiceflow.com/widget/bundle.mjs";
  widget.type = "text/javascript";
  firstScript.parentNode.insertBefore(widget, firstScript);
})(document, 'script');

I also tried accessing the data like this:

if (previous_event) {
  page_url = previous_event.payload.page_url
} else {
  page_url = "unknown"
}

The issue is that previous_event returns undefined and I’m not sure how this would actually add the URL data to the transcript records.

That previous_event issue is super common with VoiceFlow’s event handling. Skip the launch events - they’re unreliable. Instead, use session variables with a custom step. Drop a Set Variable step right at the start of your conversation flow and grab the URL through JavaScript integration. You can pull window.location.href directly in VoiceFlow’s code step and save it to something like {page_url}. This variable sticks around for the whole conversation and shows up in your transcript exports. The trick is making the URL capture part of your actual conversation flow instead of just initialization parameters - those usually disappear when transcripts get generated.

I encountered a similar issue before. It seems that the URL in the launch event payload doesn’t always persist as one might expect. What resolved it for me was implementing a custom action within VoiceFlow to capture the URL. Create this action early in your flow and utilize window.voiceflow.chat.interact to pass the URL as a variable. You can add this code to your page: window.voiceflow.chat.interact({ type: 'complete', payload: { current_url: window.location.href } }). Then, create a variable in VoiceFlow to store this URL for later use in the conversation. This ensures the URL gets logged in the transcripts as it becomes part of the conversational context rather than mere initialization data.

Been there with VoiceFlow tracking. The launch event approach never worked consistently for me either.

What actually works: set up a global variable before the widget loads, then pass it through the chat API. Here’s what I do:

// Set this before loading VoiceFlow
window.currentPageUrl = window.location.href;

// Then in your VoiceFlow flow, use a Code step
const pageUrl = window.parent.currentPageUrl || window.top.currentPageUrl || 'unknown';
// Store it in a VoiceFlow variable
return { page_url: pageUrl };

The key is accessing the parent window context since VoiceFlow runs in its own frame. I store this in a variable called page_url right at the start of every conversation.

For transcript logging, make sure you’re using this variable somewhere in your flow. Even if you just set it and never display it, VoiceFlow includes it in the session data when you export transcripts.

I’ve been tracking user interactions this way across multiple sites and it’s solid. The parent window reference handles most iframe access issues.

Honestly, just add a hidden input field that grabs the URL and sends it with the first message. Use document.addEventListener('DOMContentLoaded', function() { window.pageUrl = window.location.href; }); then access window.pageUrl in Voiceflow’s javascript block and store it as a variable. Way more reliable than dealing with those buggy launch events.