Skip to content
GemBoss Docs

The moments API: fire your own event, run your own action

Two calls on the page: fire starts a moment, on runs your code when one happens.

A moment is something GemBoss does during a visit, after the page shows: a bar, a popup or a section appears when a shopper waits, scrolls, clicks or is about to leave. From your store's own code, window.gem.moment.fire(name) starts a moment, and window.gem.moment.on(name, fn) runs your code when one happens.

Where do the names come from?

The store's team makes moments in GemBoss, on a funnel step, under Who sees what › During the visit. Two of the choices there are yours to wire:

  • When › Your own › Custom event. The moment happens when your code fires its name.
  • Then › Your own › Custom action. When the moment happens, GemBoss runs the code you registered for its name.

When the team picks one, GemBoss shows them the line of code to send you.

Fire an event

window.gem.moment.fire('sizes-looked-at');

Every moment waiting for that name happens now, the same way it would for any other When. Each moment hears its event once per page view. A name that no moment is waiting for does nothing.

Run your own action

const off = window.gem.moment.on('open-size-chart', ({ moment }) => { // do what the store asked for: open your size chart, start your animation }); off(); // stop answering for this name
  • Your function gets { moment }, the id of the moment that happened.
  • Register as many functions as you like. Each one is called. One that throws does not stop the others or break the page.
  • Functions stay registered when the shopper moves to another page of the store without a full reload. If your script runs again each time the page shows, register once, or call off() before you register again.

Names

A name is 1 to 40 characters: letters, digits, - or _. It is compared exactly as written, so size-chart and Size-Chart are two different names. The store's team types the same name in GemBoss.

Wait for gem:moments-ready

window.gem.moment appears once the moments script has loaded. That script loads after the page shows, and only on a page where this shopper has a moment. Wrap your calls so they run when it arrives and do nothing when it does not:

function withMoments(fn) { if (window.gem && window.gem.moment) fn(window.gem.moment); else window.addEventListener('gem:moments-ready', () => fn(window.gem.moment), { once: true }); } withMoments((moment) => { moment.on('open-size-chart', () => document.querySelector('#size-chart')?.showModal()); });

GemBoss sends gem:moments-ready on window once per page load, after it has set up the page's moments, so a fire inside your listener reaches them.

When does nothing happen?

Each of these is by design, and none of them throws an error.

  • No consent. Moments run only for a shopper whose consent allows tracking, under the store's privacy setting. Without it, window.gem.moment never appears and your wrapped code does not run. The two calls store nothing on the device.
  • No moment for this shopper on this page. The moments script does not load.
  • Held back. 1 in 10 of a moment's shoppers see nothing, so GemBoss can measure what the moment adds. For them your action is not called, on every page view.
  • Nothing registered. A Custom action with no function registered when its moment happens does nothing and is not counted. Register your functions as soon as window.gem.moment is there.
  • A moment that follows another. It listens only after the moment before it has happened.
  • The editor. Moments do not run in the GemBoss editor's preview. Test on the published page.

How GemBoss reads a shopper's consent on the stores it serves is in the privacy policy, under Consent.

Limits

Limit Value
Name 1 to 40 letters, digits, - or _. Case counts.
Moments on one funnel step 5
Shoppers held back 1 in 10 of each moment's shoppers
Bars and popups One per page view. A Custom action does not count as one.
How often The store's choice: once a visit, once a week, or every time

Example: open a size chart

The store wants its size chart to open when a shopper lingers over the sizes. Put this in a script on the step's page: the page's Custom JS setting in GemBoss, or any script your store already runs there.

(function () { function withMoments(fn) { if (window.gem && window.gem.moment) fn(window.gem.moment); else window.addEventListener('gem:moments-ready', function () { fn(window.gem.moment); }, { once: true }); } withMoments(function (moment) { // This runs each time the page shows: drop the last registration before adding this one. if (window.offSizeChart) window.offSizeChart(); window.offSizeChart = moment.on('open-size-chart', function () { var chart = document.querySelector('#size-chart'); if (chart && chart.showModal && !chart.open) chart.showModal(); }); document.querySelectorAll('[data-size-option]').forEach(function (el) { el.addEventListener('mouseenter', function () { moment.fire('sizes-looked-at'); }); }); }); })();

In GemBoss, the store's team then makes one moment: When Custom event sizes-looked-at, Then Custom action open-size-chart. They choose who sees it and how often.

Frequently asked questions

Do I need a key or a server call?

No. Both calls run in the shopper's browser, on your store's pages. There is nothing to sign in to.

Does GemBoss record my event names?

No. fire sends nothing. When a moment happens, GemBoss counts it as shown or held back, and only with the shopper's consent.

Can I fire a moment on a page that has none?

Nothing happens. window.gem.moment appears only where this shopper has a moment, and a name with no moment waiting for it does nothing.

Stuck on the connection?

You connect at https://mcp.gemboss.ai/mcp and sign in through your browser; there is no key to ask for. If the browser step does not finish, the troubleshooting page walks through what to check.

When the connection will not come up →
Was this page useful?