AdvaitaADVAITA DOCS
GitHubGet started
Web SDKv8.21.10MIT License

JavaScript SDK

acai-js is the browser SDK for vanilla JavaScript projects. It persists events to localStorage, manages sessions, and auto-captures UTM parameters, referrer, and click IDs.

Using React, Vue, or Svelte? Consider the TypeScript Browser SDK — it has a plugin system, autocapture, and first-class TypeScript support.

Installation

Shell
npm install acai-js
# or
yarn add acai-js

Or load via async snippet — paste in your <head>:

HTML
<script>
(function(e,t){var n=e.acai||{};window.acai=n;
var r=t.createElement("script");r.type="text/javascript";
r.async=true;r.src="https://cdn.acaiplatform.ai/acai-js.min.js";
var s=t.getElementsByTagName("script")[0];s.parentNode.insertBefore(r,s);
n._q=[];var i=["init","logEvent","setUserId","setUserProperties",
"identify","setGroup","logRevenueV2","setOptOut","regenerateDeviceId"];
i.forEach(function(t){n[t]=function(){n._q.push([t,arguments])}});
})(window,document);
acai.init("YOUR_API_KEY");
</script>

Initialise

JavaScript
import acai from 'acai-js'
acai.init('YOUR_API_KEY', 'user@example.com', {
apiEndpoint: 'events.yourdomain.com', // collector host, no https://
includeUtm: true,
includeReferrer: true,
batchEvents: false,
logLevel: 'WARN',
})

Multiple named instances:

JavaScript
const instance2 = acai.getInstance('instance2')
instance2.init('OTHER_API_KEY')

Track events

JavaScript
// Simple event
acai.logEvent('Button Clicked')
// With properties
acai.logEvent('Purchase Completed', {
product_id: 'prod_123',
price: 49.99,
currency: 'USD',
})
// With callback
acai.logEvent('Sign Up', { method: 'email' }, (code, body) => {
if (code === 200) console.log('Event sent')
})

Identify users

JavaScript
// Set the authenticated user ID
acai.setUserId('user_abc123')
// Set user properties directly
acai.setUserProperties({ plan: 'pro', company: 'Acme Corp' })
// Fine-grained control with the Identify builder
const identify = new acai.Identify()
identify.set('plan', 'pro') // overwrite
identify.setOnce('signup_date', '2024-01-15') // write once
identify.add('login_count', 1) // increment
identify.append('tags', 'power-user') // append to array
identify.prepend('recent_events', 'login')
identify.unset('trial_active') // remove property
identify.clearAll() // remove all user props
acai.identify(identify)

To log out a user:

JavaScript
acai.setUserId(null)
acai.regenerateDeviceId() // start a new anonymous session

Groups

JavaScript
acai.setGroup('company', 'Acme Corp')
acai.setGroup('project', ['proj_1', 'proj_2']) // multiple values

Revenue

JavaScript
const revenue = new acai.Revenue()
revenue
.setProductId('prod_premium')
.setPrice(99.00)
.setQuantity(1)
.setRevenueType('subscription')
.setEventProperties({ coupon: 'LAUNCH20' })
acai.logRevenueV2(revenue)

Config reference

OptionTypeDefaultDescription
apiEndpointstringapi.acai.yourdomain.comCollector host (no https://).
forceHttpsbooleantrueAlways use HTTPS for requests.
batchEventsbooleanfalseBuffer events and send in batches.
eventUploadThresholdnumber30Batch size that triggers a flush.
eventUploadPeriodMillisnumber30000Flush interval in ms.
saveEventsbooleantruePersist unsent events to localStorage.
savedMaxCountnumber1000Max events saved to storage.
sessionTimeoutnumber1800000Session inactivity timeout (30 min).
includeUtmbooleanfalseAuto-capture UTM parameters.
includeReferrerbooleanfalseAuto-capture page referrer.
includeFbclidbooleanfalseAuto-capture Facebook click ID.
includeGclidbooleanfalseAuto-capture Google click ID.
logLevelstringWARNDISABLE · ERROR · WARN · INFO
optOutbooleanfalseDisable all tracking.
disableCookiesbooleanfalseUse localStorage instead of cookies.
cookieExpirationnumber365Cookie expiry in days.
sameSiteCookiestringLaxLax · Strict · None
transportstringhttphttp or beacon (sendBeacon API).
deferInitializationbooleanfalseDelay tracking until enableTracking() is called.
onErrorfunction—Called when an event fails to send.