I’m using an external service that exists as an HTML embed within my app. When a user completes actions within the iframe, certain unique values are stored in the console.log. I’m trying to figure out a way to save those unique values within bubble.
I’ve played around with the Toolbox plugin, specifically the Javascript to Bubble feature, but I’m having trouble getting the embedded HTML/script to recognize the bubble_fn_x function. “ReferenceError: bubble_fn_1 is not defined” appears.
Appreciate any help and/or direction here!
Charles
Try building the javascript function that would grab the variables from the console log and name it something like consoleFunction. Then call the bubble_fn_x(consoleFunction) and it should take care of it for you.
Thank you so much for the reply, this makes sense. This is probably a more rudimentary javascript question (not an expert here): could you explain what a function that would grab variables from the console log might look like more precisely? I’ve attached an example of what the console log output looks like. What I need to be able to do is save the “account created” and “User ID” fields into my bubble app.
I don’t think that you would be able to copy the variables from the console log using javascript. i think the approach you would need to take is to use javascript to access the iFrame and then copy the variables when they are being posted to the console log. It’s hard to say without seeing the embedded code.
Got it - so the fact that the variables are posted to the console log doesn’t really help. Instead, the embedded code should be re-written and/or modified to send the variables to Bubble instead of posting them to the console log?
Here’s the beginning of the iFrame/embedded code that includes the first example of the variables that need to be saved:
If I understand correctly, the “onAccountCreated” function would need to be modified in such a way that the bubble_fn_x can be called with the accountID and userId variables?
Okay, I think that we might be able to make this work. I had to do some quick research, but i think that we can pass the variables using a postMessage/onMessage mechanism. Don’t remove the console.log line from the script in the iFrame, but underneath it, add the following line:
For the JavaScript to Bubble element on the page, make sure that it is returning a list, as that is what you are passing through. Use the List:First Item to get the Account, and List:Last Item to get the User. I couldn’t really test this, but I hope that it works for you.
Wow, thanks for all of this detailed help! A few clarifying questions.
URL of the target origin. Is this the domain that the postMessage is coming from? or going to? As in, would I use my own domain here, or the 3p domain that the iFrame is interacting with?
Where does bubble_fn_1 get defined? Seeing some “Uncaught ReferenceError: bubble_fn_1 is not defined” error messages in the console.
the window.addEventListener … is added within the javascript of the iFrame? Or after that / within the HTML embed element?
This would be from the domain that you are interaction with, basically the url from the iFrame.
Sorry, I’m still trying to figure out how this whole JS to Bubble works. Try the following:
window.addEventListener("message", getAcctData, false);
function getAcctData(event) {
if (event.orgin == 'target origin url') {
if (event.data == []) {
event.source.postMessage('No data passed', event.origin);
} else {
return event.data;
}
}
}
bubble_fn_1(event.data);
the window.addEventListener should be added to an HTML element on the page in bubble. Probably need to add tags for it to work. The event listener triggers the code.
OK I figured it out! Your answers led me to search with a few different keywords and find this thread which explains how to use Javascript to Bubble within an iFrame.