Including website URL in VoiceFlow chatbot conversation logs

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

I attempted this approach but it’s not working:

(function(doc, scriptTag) {
  var widget = doc.createElement(scriptTag), firstScript = doc.getElementsByTagName(scriptTag)[0];
  widget.onload = function() {
    window.voiceflow.chat.load({
      verify: { projectID: 'YOUR_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 this code to retrieve the URL:

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

The issue is that previous_event appears to be undefined, and I’m not sure how this would actually add the URL data to the transcript logs. Any suggestions on the correct way to implement this?

Your launch event payload probably isn’t sticking around in the logs. Skip the previous_event approach and use VoiceFlow’s variable system instead. Create a project variable called page_url in your dashboard, then set it directly in your launch config. Here’s what’s worked better for me: inject the URL as a query parameter when you initialize the widget. Just append the current page URL to your VoiceFlow project’s launch parameters, then grab it with a Set step right at the start of your flow. This way the URL gets logged as part of the initial conversation context instead of you trying to pull it from an event object that might not even be there when you need it.

Try window.voiceflow.chat.proactive.push() instead. Had the same issue and this worked way better than launch events. Just push your URL data as a custom payload after chat finishes initializing. Also double-check your projectID - that’s usually why payload data vanishes from the logs.

This sounds like a timing issue with your payload data. Don’t rely on the launch event to pass the URL - use the interact method after the chat loads instead. That way your custom data actually gets logged with the conversation.

Here’s what worked for me: once the chat loads, call window.voiceflow.chat.interact() with a custom action that includes the page URL. Set up a variable in your VoiceFlow project to capture this data so it gets written to the transcript.

You could also store the URL in session/local storage, then grab it in your VoiceFlow flow with a code step. This is more reliable since you’re not fighting timing issues. Just make sure you’ve got the right variables set up in VoiceFlow to receive and log everything in the conversation history.