API and Embed Automation
Control a tour from your own website with the postMessage API: listen for viewer events and send commands back to the embedded tour.
You will connect a hosted page to an embedded tour and react to what the visitor does. The viewer sends three events to the parent page and accepts two commands, which is enough to build a tour that follows the content of the page and to automate publishing from your own tools.
Listen to the viewer
The embedded viewer posts a message to its parent window when the tour is ready, when the visitor changes scene and when a hotspot is clicked. Subscribe to the messages, check the type field and read the payload. The tourReady event tells you when the first frame is on screen and the viewer can be measured.
- tourReady — the tour has loaded and the first scene is visible.
- sceneChanged — the visitor moved to another scene, with the scene id.
- hotspotClicked — a hotspot was activated, with its identifier.
Send commands
- Keep a reference to the iframe element of the tour in the code of your page.
- Send the commands only after the tourReady event has arrived.
- goToScene opens the scene with the given id, so the visitor can follow the text on the page.
- setUiMode switches the interface between the full and the minimal view.
Only send messages after the tourReady event. A command that arrives before the viewer is listening is ignored, and the tour will not react to it later.
Put it together
const frame = document.querySelector("iframe");
frame.addEventListener("message", (event) => {
const { type, payload } = event.data ?? {};
if (type === "tourReady") console.log("tour is ready", payload);
if (type === "sceneChanged") console.log("scene", payload.scene);
if (type === "hotspotClicked") console.log("hotspot", payload.hotspotId);
});
frame.contentWindow.postMessage({ type: "goToScene", payload: { scene: 4 } }, "*");
frame.contentWindow.postMessage({ type: "setUiMode", payload: { mode: "minimal" } }, "*");