Skip to content
ShowpieceDocs

Talking to your page

A demo on your page tells it what happens inside it, for your own analytics, and does what your page asks: play, pause, go to a step.

What it tells your page

In a frame on your page, the demo sends the page a message whenever one of these happens:

ready
The demo is up: steps, how many it has, and title, its name.
step
It arrived at a step, played to it or sent there by the rail, the chapters, a choice or your page: step (counting from 1), steps, the step's title and its chapter (empty when it has none).
end
It reached its end.
button
A button was clicked: its label, its href, and the step it's on, or null for the end card's.
choice
A choice was chosen: its label, the step it was on, and to, the step it goes to or “end”.
escape
Escape was pressed inside the demo and nothing in it used the key. The pop-up closes on it.
lead
Someone sent a form (lead forms): the step it's on, or null for the end card's. When your page is where the form's answers go, fields too: what they typed, once your page has asked for them (Asking it).

Each is an object with its type and protocol set to “demo/1”.

Where it stops. Dragging the scrubber past a step isn't arriving at it, so it's not told.

Listening

On your page, beside the snippet:

const demo = document.querySelector('iframe[title="Your demo"]');
window.addEventListener("message", (event) => {
  if (event.source !== demo.contentWindow) return;
  const message = event.data;
  if (!message || message.protocol !== "demo/1") return;
  if (message.type === "step") {
    // Send it to your analytics here.
    console.log("Step " + message.step + " of " + message.steps + ": " + message.title);
  }
  if (message.type === "end") console.log("Watched to the end");
});

Asking it

Your page can ask it to play, to pause, or to go to a step, say from a link beside it that jumps to the part a reader wants:

demo.contentWindow.postMessage({ protocol: "demo/1", type: "go", step: 3 }, "*");
demo.contentWindow.postMessage({ protocol: "demo/1", type: "play" }, "*");
demo.contentWindow.postMessage({ protocol: "demo/1", type: "pause" }, "*");

go goes where the step's place on the rail does: the page as it was there, stopped at the step's card. play is the play button's run.

When your page is where its forms' answers go (Only the page it's on), it asks for them with leads, best as the demo says it's ready. From then on each lead carries what was typed, sent to your page's address and nowhere else:

window.addEventListener("message", (event) => {
  if (event.source !== demo.contentWindow) return;
  const message = event.data;
  if (!message || message.protocol !== "demo/1") return;
  if (message.type === "ready") demo.contentWindow.postMessage({ protocol: "demo/1", type: "leads" }, event.origin);
  if (message.type === "lead" && message.fields) console.log("A lead:", message.fields.email);
});

Where it stops. It does what its own page asks and nothing another window sends. A step it doesn't have is ignored.

Where it stops. What's typed into a form goes only to the page that asked for it. A page that frames your file and never asks hears none of it, and the form says it can't send.

What it never says

Only what someone watching can see in it: the demo's own words, as you redacted and replaced them. Nothing about who's watching but what they type into a form and send, and that only when your page is where the form's answers go and has asked for them. Nothing to anyone but the page it's on, and nothing to us.

Where it stops. Opened on its own, not in a frame, it has no page to tell.

Where it stops. On Free, the file plays on your page as an ordinary frame: it tells it nothing and does nothing it asks. That comes with Basic and Pro.