Triggering After a JavaScript Event

Triggering After a JavaScript Event

Display your message when a visitor has completed any predefined action that is not handled by standard methods.

Triggering After a JavaScript Event is an OptiMonk trigger that fires a campaign when a custom JavaScript event is dispatched on your page — enabling you to connect a campaign to any visitor action your site already tracks, regardless of whether it is covered by OptiMonk's standard trigger options. Where time delay, scroll, exit intent, and click triggers cover the most common trigger scenarios, JavaScript event triggering covers everything else: a video reaching a certain completion percentage, a form field being filled without submission, a specific product variant being selected, a chat widget being opened, a loyalty points threshold being crossed, an add-to-cart action tracked by your own analytics, or any other interaction your site captures through JavaScript events. To use this trigger, you dispatch a custom JavaScript event on your page — document.dispatchEvent(new Event('optimonk_custom_trigger')) — and OptiMonk listens for it and fires the campaign when it is received. The event name must exactly match what is configured in OptiMonk's trigger settings. This trigger is the developer-level escape hatch that makes OptiMonk's campaign system extensible to any behavioral context your site can express in JavaScript.

Key benefits

  • Trigger campaigns from any visitor action your site already tracks. Every ecommerce platform and analytics stack captures dozens of custom events — product interactions, video plays, loyalty program milestones, custom cart events, filter selections. Rather than building separate tracking for OptiMonk, JavaScript event triggering lets you tap into these existing signals directly. Any event your site dispatches in JavaScript can become a campaign trigger with a few lines of code — no new tracking infrastructure required.
  • Supports the most precise timing available for complex interactions. Standard triggers operate on coarse signals — time, scroll position, cursor movement. JavaScript event triggering operates at the exact moment of a specific action, with the precision of your own code. A campaign triggered by a video at 75% completion fires at exactly 75% — not after a time proxy, not after a scroll approximation. For interactions where timing precision matters, this is the most accurate trigger mechanism available.
  • Extends OptiMonk to use cases outside its standard trigger set without custom code in OptiMonk. The trigger configuration in OptiMonk is minimal — you specify an event name and the trigger is armed. All the logic that determines when the event fires lives in your own codebase, where it belongs. This separation keeps OptiMonk's configuration simple while giving your development team complete control over the trigger condition, without needing to configure complex logic inside OptiMonk itself.

How it works

Step 1
Open the campaign trigger settings and add the JavaScript event trigger

In your OptiMonk campaign, click Edit settings and scroll to the Settings summary. Click "Add more triggers" to open the trigger options panel. Click the Plus icon next to "On JavaScript event" to add it to the campaign. Enter the name of the custom event you will dispatch from your site — for example, optimonk_custom_trigger or a more descriptive name relevant to your use case, such as video_75_percent or add_to_cart_complete.

Step 2
Dispatch the matching JavaScript event from your site at the right moment

In your site's JavaScript code — or through Google Tag Manager — add code that dispatches the event at the action you want to trigger the campaign. The standard approach is: document.dispatchEvent(new Event('your_event_name')) where your_event_name exactly matches what you entered in OptiMonk's trigger settings. Place this dispatch call at the point in your code that corresponds to the visitor action — inside a video event listener, a custom cart handler, a loyalty program callback, or wherever the target interaction is already being tracked.

Step 3
Publish the campaign — it fires when the event is received

Once published, OptiMonk listens for the specified event name on the page. When the event is dispatched by your site code, OptiMonk fires the campaign immediately for the visitor who triggered the event, provided all targeting conditions are also met at that moment. No polling, no interval checking — the trigger is event-driven and fires in real time at the exact moment the event is dispatched.

Frequently asked questions

What is Triggering After a JavaScript Event in OptiMonk?+

Triggering After a JavaScript Event is an OptiMonk campaign trigger that fires when a custom JavaScript event is dispatched on your page. You configure an event name in OptiMonk's trigger settings, then dispatch a matching event from your site's JavaScript code at the moment you want the campaign to fire. This allows any visitor action that your site already tracks in JavaScript — a video milestone, a specific form interaction, an add-to-cart event, a loyalty threshold — to become a campaign trigger.

What JavaScript code do I need to add to my site?+

To trigger an OptiMonk campaign using a JavaScript event, dispatch a custom event from your site's code at the moment the target action occurs. The standard implementation is: document.dispatchEvent(new Event('your_event_name')) — replacing your_event_name with the exact string you entered in OptiMonk's trigger settings. This one line of code is typically added inside an existing event listener or callback in your site's JavaScript, at the point where the target visitor action is already being handled.

Can I use Google Tag Manager to dispatch the JavaScript event instead of editing my site's code directly?+

Yes. If your store uses GTM, you can create a Custom HTML tag in GTM that dispatches the OptiMonk event and trigger it from any GTM trigger — a dataLayer push, a timer, an element visibility trigger, or any other GTM trigger condition. This approach keeps the event dispatch logic in GTM alongside your other tags, without requiring changes to your site's source code directly.

Does the event name have to be a specific format?+

The event name must exactly match between your JavaScript dispatch code and OptiMonk's trigger configuration — including case sensitivity. There are no restrictions on the naming format beyond this exact match requirement. Descriptive names that reflect the action (such as video_complete, loyalty_threshold_reached, or product_variant_selected) are recommended over generic names to make the trigger's purpose clear when reviewing campaign settings.

Can I combine a JavaScript event trigger with targeting conditions?+

Yes. Like all OptiMonk triggers, the JavaScript event trigger works alongside targeting conditions using AND logic. When the event is dispatched, OptiMonk checks the event receipt AND all configured targeting conditions simultaneously before deciding whether to display the campaign. This means you can restrict a JavaScript-triggered campaign to specific visitor segments — for example, firing only when the event is dispatched AND the visitor is a non-subscriber AND the cart value is above $50 — without any additional logic needed in your JavaScript code.

Ready to try it?

Try OptiMonk for free

Launch your first campaign, learn what works, then scale what converts.

triggering