CDN Integration
Prerequisites
- Active
siteKeyfrom Selwise dashboard. - Correct API URL ending with
/api/v1. - Verified storefront domain for public guard origin checks.
- A single base layout where script can be injected once.
Currency rule: use ISO-4217 uppercase codes and replace SITE_CURRENCY in examples with your configured site currency.
Installation
<script
src="https://widget.selwise.com/client.js"
data-site-key="YOUR_SITE_KEY"
data-api-url="https://api.selwise.com/api/v1"
></script>
Required:
src="https://widget.selwise.com/client.js"data-site-key
Optional:
data-api-url
Initialization
CDN mode auto-initializes and binds runtime to window.Selwise.
console.log(window.Selwise);
console.log(window.Selwise?.siteKey);
console.log(window.Selwise?.apiUrl);
Single-instance rule:
- Place script in root/base storefront template.
- Do not inject script in route-level partials.
Identity and Consent
await window.Selwise?.identify?.('user_42', { email: 'user@example.com' });
await window.Selwise?.setTraits?.({ loyaltyTier: 'gold' });
const consent = window.Selwise?.getConsentState?.();
console.log(consent);
window.Selwise?.grantConsent?.({ analytics: true, marketing: true, preferences: true });
window.Selwise?.revokeConsent?.(['marketing']);
Event Sending
window.Selwise?.track?.('product_view', {
entityType: 'product',
entityId: 'SKU-123',
metadata: { productItemCode: 'SKU-123', title: 'Runner Pro', price: 149.9 },
});
window.Selwise?.pushDataLayer?.({
event: 'add_to_cart',
basket: {
id: 'cart_001',
currency: 'SITE_CURRENCY',
total: 149.9,
products: [{ sku: 'SKU-123', quantity: 1, price: 149.9 }],
},
});
For full event catalog and canonical payloads:
Order Tracking
await window.Selwise?.trackOrder?.({
orderId: 'ORDER-1001',
currency: 'SITE_CURRENCY',
total: 149.9,
items: [{ productItemCode: 'SKU-123', quantity: 1, unitPrice: 149.9 }],
});
Search and Recommendation Usage
window.Selwise?.pushDataLayer?.({
event: 'search_query',
search: { query: 'running shoes', results: 120 },
});
window.Selwise?.pushDataLayer?.({
event: 'click',
custom: { params: { placement: 'home_reco', widgetId: 'widget_home_1' } },
});
Verification Checklist
window.Selwiseexists after page load.- Config request succeeds:
GET /public/sites/:siteKey/config. - Event probe reaches
POST /public/sites/:siteKey/events/batch. trackOrderprobe reachesPOST /public/sites/:siteKey/orders.- Consent state changes are reflected.
Full runbook: Verification
Troubleshooting (Common)
window.Selwiseundefined.
- Wrong script host/path or blocked script by CSP/network policy.
- 403 origin validation failed.
- Domain not verified or request origin mismatch.
- Events missing.
- Invalid canonical payload or consent blocking event family.
Full matrix: Integration Troubleshooting
Production Go-Live Checklist
- Script tag exists exactly once in base template.
-
siteKeyandapiUrlmatch target environment. - Domain verification completed in dashboard.
- Event/order/consent probes validated on staging.
- Retry/backoff behavior ready for transient failures.