JavaScript API
Once the script has loaded, it provides window.conversionloop. Use it to pass your own data, to show a campaign from your code, and to tell the script about route changes.
Calls before the script has loaded
The script loads with async, so your code may run first. Create a queue, and the script works through it once it has started:
<script>
window.conversionloop = window.conversionloop || { q: [] };
</script>
<script async src="https://cdn.conversionloop.net/v1/p/prj_live_xxxxxxxxxxxxxxxxxxxxxxxx.js"></script>
Until the script is there, push calls as arrays: the method name first, then its arguments.
window.conversionloop.q.push(['set', { plan: 'pro', logged_in: true }]);
After loading, the script replaces window.conversionloop with the real methods. If your code may run either before or after, a small helper covers both:
function cl(method, ...args) {
const api = (window.conversionloop = window.conversionloop || { q: [] });
if (typeof api[method] === 'function') api[method](...args);
else api.q.push([method, ...args]);
}
cl('set', { cart_value: 149.9 });
set(fields)
Passes values for your data fields. The script checks all campaigns again right away, so a campaign whose audience depends on the value can appear as soon as you set it.
conversionloop.set({
cart_value: 149.9,
plan: 'free',
logged_in: true,
});
- Keys are the data field keys from the dashboard or the API.
- Values are strings, numbers, booleans or
null. Dates are ISO strings, for example'2026-12-03'. - Each call adds to or overwrites earlier values. Keys you leave out stay as they are; set a key to
nullto clear it. - Values live only as long as the page. Set them again on every page load, for example from your template or data layer.
What is sent
With every impression and answer, the script sends the data fields that are set on the page at that moment. You find them in the data_fields column of the CSV export.
Do not pass personal data such as names, email addresses or customer numbers. Use categories instead:
plan: 'pro'rather than an email address,cart_valuerather than an order ID.
get(key)
Returns the value set for a data field, or undefined.
conversionloop.get('plan'); // 'free'
trigger(slug)
Shows a live campaign right away, for example after a click or a completed purchase. The audience is skipped, but the campaign's frequency limits still apply. By default, for example, a visitor who has already answered does not see it again.
document.querySelector('#order-complete').addEventListener('click', () => {
conversionloop.trigger('post-purchase-survey');
});
The argument is the campaign's slug. Unknown slugs are ignored (with a message in the console if debug logging is on).
To use a campaign only through trigger, give it an audience that never matches on its own, for example click on an element that does not exist on your pages.
refresh()
Reads the URL again and checks all campaigns from scratch. Call it after a route change in a single-page app:
// Vue Router
router.afterEach(() => conversionloop.refresh());
// Next.js (App Router)
const pathname = usePathname();
useEffect(() => { window.conversionloop?.refresh(); }, [pathname]);
TypeScript
type DataValue = string | number | boolean | null;
declare global {
interface Window {
conversionloop?: {
set(fields: Record<string, DataValue>): void;
get(key: string): DataValue | undefined;
trigger(slug: string): void;
refresh(): void;
q?: [string, ...unknown[]][];
};
}
}
export {};