← All docs

JavaScript SDK (cloak.js)

A single script tag adds decisioning, redirection and analytics.

Install

<script src="https://your-host/api/public/cloak.js"
        data-site-key="YOUR_SITE_KEY"
        data-auto-redirect="1"></script>

With data-auto-redirect="1" the SDK calls /decide on load and redirects to the money page automatically when the verdict is money.

Custom flow

<script src="https://your-host/api/public/cloak.js"
        data-site-key="YOUR_SITE_KEY"></script>
<script>
  window.addEventListener('load', async () => {
    const d = await ShieldGate.decide();
    if (d.action === 'money' && d.url) location.replace(d.url);
    else document.getElementById('content').style.display = 'block';
  });
</script>

API surface

MethodDescription
ShieldGate.decide()Returns { verdict, action, url, mode, ttl }
ShieldGate.track(kind, name, meta?)Records a custom event
ShieldGate.identify(userId, traits?)Associates events with a user
ShieldGate.on('decision', fn)Fires on every decision

Auto-tracked events

  • pageview — on load and on history.pushState
  • click — every <a> and <button>
  • download — links to files (pdf, zip, exe, dmg, apk…)
  • form_submit — form submissions (never captures values)

Custom events

ShieldGate.track('custom', 'signup_completed', { plan: 'pro' });
ShieldGate.track('custom', 'add_to_cart',      { sku: 'X1', price: 49 });
ShieldGate.track('conversion', 'purchase',     { revenue: 129.00 });

Data attributes

AttributeEffect
data-site-keyRequired — your site key
data-auto-redirectRedirect on money verdict
data-endpointOverride the API host
data-debugLog decisions to the console
data-no-trackDisable auto-tracking

SSR / no-JS fallback

Because bots often disable JS, we recommend combining cloak.js with a server-side /decide call from your origin. See Integrations.