Skip to content

JavaScript SDK

The AdvocateLoop script (al.js) gives you a small browser-side API for tracking conversions and reacting to widget activity. It’s loaded by the same script tag that powers the widget.

For server-side tracking with no browser, use the REST API instead.

Loading the SDK

The SDK becomes available once the script tag is on your page:

html
<script src="https://api.advocateloop.com/al.js?brand=YOUR-BRAND-UUID"></script>

The same script also tracks referral visits and renders any widgets you’ve placed — see Install the Widget Script.

The queue pattern

Your code may run before al.js finishes loading. So that a call always runs, push it onto AdvocateLoopQueue instead of calling the global directly:

html
<script>
window.AdvocateLoopQueue = window.AdvocateLoopQueue || [];
AdvocateLoopQueue.push(function () {
AdvocateLoop.trackConversion({ /* ... */ });
});
</script>

Anything you push runs as soon as the SDK is ready — and immediately if it has already loaded.

AdvocateLoop.trackConversion(params)

Records a conversion. Call it on your thank-you / order-confirmation page after a purchase.

html
<script>
window.AdvocateLoopQueue = window.AdvocateLoopQueue || [];
AdvocateLoopQueue.push(function () {
AdvocateLoop.trackConversion({
order_id: 'ORD-12345',
order_total: 108.00,
tax: 8.00,
shipping: 10.00,
discount: 5.00,
customer_email: 'buyer@example.com',
items: [
{
name: 'Vitamin C Serum',
quantity: 2,
unit_price: 32.00,
sku: 'VITC-30',
category_path: ['Skincare', 'Serums'],
image_url: 'https://yourstore.com/img/vitc.jpg'
},
{ name: 'Gift Wrapping', quantity: 1, unit_price: 4.00 }
]
});
});
</script>
ParameterTypeRequiredDescription
order_idstringYesUnique order identifier
order_totalnumberYesWhat the customer paid (items + tax + shipping − discounts)
taxnumberYesTotal tax. Use 0 if not applicable
shippingnumberYesTotal shipping. Use 0 if not applicable
discountnumberYesTotal discount applied. Use 0 if no coupon
customer_emailstringYesCustomer’s email address
customer_namestringNoCustomer’s name
itemsarrayNoLine items in the order. Fields below.

Each entry in items:

FieldTypeRequiredDescription
namestringYesItem or service name
quantitynumberNoUnits. Defaults to 1
unit_pricenumberNoPrice per unit. Or send line_total for the line’s total
skustringNoSKU, plan code, or service code
category_pathstring[]NoOrdered category path, top level first — e.g. ['Skincare', 'Serums']
image_urlstringNoImage URL for the item

For platform-specific examples (Shopify, Squarespace), see the JavaScript Quick Start.

AdvocateLoop.trackClaim(params)

Records an early claim — a referral and an optional cart — before checkout.

html
<script>
window.AdvocateLoopQueue = window.AdvocateLoopQueue || [];
AdvocateLoopQueue.push(function () {
AdvocateLoop.trackClaim({
referral_code: 'JOHN123',
email: 'buyer@example.com',
name: 'Jane Doe',
items: [
{ name: 'Vitamin C Serum', quantity: 2, unit_price: 32.00, category_path: ['Skincare', 'Serums'] }
]
});
});
</script>
ParameterTypeRequiredDescription
referral_codestringNoThe code being claimed. Falls back to the stored referral code when omitted.
emailstringNoCustomer’s email
namestringNoCustomer’s name
phonestringNoCustomer’s phone
itemsarrayNoCart line items — same fields as the conversion items array above

Provide email, or rely on a prior referral visit in this browser.

Returns a promise that resolves with the API response, or rejects on failure.

Clicks and refunds

  • Clicks are tracked automatically when al.js loads on a page reached through a referral link — nothing to call. Await AdvocateLoop.ready to know when init has finished, and use getReferralCode() / getVisitorId() to read the stored values.
  • Refunds are server-side — send them from your backend via the REST API with your API key.

Widget events

The SDK dispatches DOM events you can listen for, to hook your own code into widget activity:

javascript
document.addEventListener('advocateloop:claim', function (e) {
// e.detail: { email, referralCode }
});
document.addEventListener('advocateloop:share', function (e) {
// e.detail: { email, referralCode, shareUrl }
});
EventFires whene.detail
advocateloop:claimA visitor submits the claim form{ email, referralCode }
advocateloop:shareA visitor signs up as an advocate{ email, referralCode, shareUrl }