Skip to Content
SourcesTracking SDKJavaScript SDK

JavaScript SDK

The Vendo JavaScript SDK captures events from your website and sends them to your ingestion API. It handles batching, retries, session management, attribution tracking, and identity resolution automatically.

Installation

See Quickstart for the full snippet. After pasting it into your <head>, all methods are available on window.vendo.

Self-Hosted Snippet

If you prefer serving the SDK from your own domain, use the self-hosted async snippet. See Self-Hosting for setup instructions.

Direct Script

<script src="https://cdn.vendodata.com/sdk/v1/vendo.js"></script> <script> const tracker = VendoTracker.init("YOUR_WRITE_KEY", { endpoint: "https://YOUR_TRACKING_ENDPOINT/collect" }); </script>

npm

Coming soon — The npm package @vendo/tracking is not yet published. For now, use the CDN snippet above.


Initialization

vendo('init', writeKey, options)

Initialize the SDK using the CDN snippet API. This is the recommended approach.

vendo('init', 'YOUR_WRITE_KEY', { host: 'https://YOUR_TRACKING_ENDPOINT', trackPageViews: true });

The CDN snippet buffers all calls as arguments. When the SDK loads, autoboot() scans the queue for the init call, initializes the tracker, and replays any remaining buffered calls.

The vendo('init', ...) call accepts these options:

OptionTypeDefaultDescription
hoststring""Base URL of your tracking endpoint. The SDK derives the collect endpoint as {host}/collect.
debugbooleanfalseLog all SDK activity to the browser console.
trackPageViewsbooleanfalseAutomatically send a page view on initialization. Alias for pageView.
pageViewbooleanfalseAutomatically send a page view on initialization.
flushIntervalMsnumber5000How often (ms) to flush the event queue.
batchSizenumber20Max events per batch request.
maxQueueSizenumber1000Max events to buffer locally before dropping oldest.
sessionTimeoutMsnumber1800000Session timeout in ms (default: 30 minutes). A new session starts after this period of inactivity.
exitIntentbooleanfalseAutomatically track exit intent events.
storageKeystring"vendo"localStorage key prefix for SDK data.
requestTimeoutMsnumber10000Timeout for each HTTP request to /collect.

VendoTracker.init(writeKey, config)

Initialize the SDK using the direct API. Returns a tracker instance.

const tracker = VendoTracker.init("YOUR_WRITE_KEY", { endpoint: "https://track.yourdomain.com/collect", debug: true });
OptionTypeDefaultDescription
endpointstring"/collect"Full URL of the collect endpoint.

All other options are the same as vendo('init', ...).


Tracking Methods

track(event, properties)

Track a custom event.

vendo.track("Product Viewed", { sku: "SKU-1", price: 29.99, category: "shoes", currency: "USD" });
ParameterTypeRequiredDescription
eventstringYesName of the event (e.g., "Product Viewed", "Signup Completed").
propertiesobjectNoKey-value pairs of event properties.

Events are queued locally and sent in the next batch flush. The SDK automatically adds these properties to every event:

PropertyDescription
messageIdUnique ID (UUID v4)
timestampISO 8601 timestamp
anonymousIdPersistent anonymous ID
userIdSet after identify() is called
sessionIdCurrent session ID
writeKeyYour write key
page_urlCurrent URL
page_pathCurrent path
page_titleCurrent page title
referrerDocument referrer
browserBrowser name
osOperating system
deviceDevice/platform
localeBrowser locale
screen_widthScreen width (px)
screen_heightScreen height (px)
user_agentFull user agent string
tracking_library_name"vendo-web-tracking"
tracking_library_versionSDK version

UTM parameters and click IDs are also included when present (see Attribution).

page(name, properties)

Track a page view. Automatically captures page URL, path, title, and referrer.

// Basic page view vendo.page(); // Named page view with custom properties vendo.page("Pricing", { variant: "annual" });
ParameterTypeRequiredDescription
namestringNoPage name. Defaults to "Page Viewed".
propertiesobjectNoAdditional page properties.

Page events are written to the events table with type: "page".

identify(userId, traits)

Associate the current visitor with a known user ID and set user traits.

vendo.identify("user-123", { email: "jane@example.com", name: "Jane Smith", plan: "pro", created_at: "2024-01-15T00:00:00Z" });
ParameterTypeRequiredDescription
userIdstringYesUnique user identifier from your system.
traitsobjectNoUser properties (email, name, plan, etc.).

After calling identify():

  • The userId is persisted in localStorage and attached to all subsequent events
  • An identify event is sent to the users table in BigQuery
  • The anonymous ID is preserved, allowing you to link pre-login and post-login activity

Call identify() after login, signup, or whenever user info changes.

group(groupId, traits)

Associate the current user with a group (company, organization, account).

vendo.group("org-42", { name: "Acme Inc", plan: "Enterprise", employee_count: 150 });
ParameterTypeRequiredDescription
groupIdstringYesUnique group identifier.
traitsobjectNoGroup properties.

Group events are written to the groups table in BigQuery.

alias(userId, previousId)

Merge two user identities. Use this when a user creates an account and you want to link their anonymous activity to the new account.

vendo.alias("user-123", "anon-456");
ParameterTypeRequiredDescription
userIdstringYesThe new canonical user ID.
previousIdstringYesThe previous anonymous or temporary ID.

Alias events are written to the aliases table in BigQuery.


Queue Management

flush()

Immediately send all queued events. Useful before a user navigates away.

vendo.flush();

The SDK flushes automatically on the configured interval (flushIntervalMs, default 5 seconds) and when the batch size is reached. You rarely need to call this manually.

reset()

Clear all stored user data — anonymous ID, user ID, session, attribution, and the event queue. Call this on logout.

vendo.reset();

After calling reset():

  • A new anonymous ID is generated on the next event
  • User ID is cleared
  • Session is reset
  • Attribution data (UTMs, click IDs) is cleared
  • Event queue is cleared

Privacy

optOut()

Opt the user out of tracking. No events are collected or sent.

vendo.optOut();

optIn()

Opt the user back in after an optOut() call.

vendo.optIn();

Use these for GDPR/CCPA compliance. When opted out, all tracking methods become no-ops.


Attribution Tracking

The SDK automatically captures UTM parameters and advertising click IDs from the URL on page load.

UTM Parameters

ParameterExample
utm_sourcegoogle
utm_mediumcpc
utm_campaignspring_sale_2026
utm_contentbanner_a
utm_termanalytics tool
utm_idcamp_12345
utm_source_platformgoogle
utm_campaign_id12345678
utm_creative_formatimage
utm_marketing_tacticprospecting

Click IDs

ParameterPlatform
gclidGoogle Ads
fbclidMeta Ads
msclkidMicrosoft Ads
ttclidTikTok Ads
sccidSnapchat Ads
dclidGoogle Display
twclidTwitter/X
wbraidGoogle Web-to-App
li_fat_idLinkedIn
ko_click_idKochava

First-Touch vs Last-Touch

The SDK stores both first-touch and last-touch values:

  • First-touch (utm_source_first, gclid_first, etc.) — Set once on the user’s first visit. Never overwritten.
  • Last-touch (utm_source_last, gclid_last, etc.) — Updated on every visit with new URL parameters.

Both are attached to every event as properties.


Session Management

Sessions are tracked automatically using an inactivity timeout (default: 30 minutes).

  • A session ID is generated on first activity
  • The session remains active as long as events occur within the timeout window
  • After the timeout, the next event starts a new session
  • Session ID is attached to every event as sessionId

Configure the timeout at initialization:

vendo('init', 'YOUR_WRITE_KEY', { host: 'https://track.yourdomain.com', sessionTimeoutMs: 3600000 // 1 hour });

Local Storage

The SDK uses localStorage with the configured prefix (default: vendo) to persist:

KeyContents
vendoAnonymous ID, user ID, session, UTM parameters, click IDs
vendo_queuePending events waiting to be sent

All data is cleared when reset() is called.


Debug Mode

Enable debug mode to log all SDK activity to the browser console:

vendo('init', 'YOUR_WRITE_KEY', { host: 'https://track.yourdomain.com', debug: true });

Debug output includes:

  • Every tracked event with its full payload
  • Batch flush attempts and responses
  • Session rotation events
  • Attribution parameter capture

Remove debug: true before going to production.


Error Handling and Retries

Failed requests are retried automatically with exponential backoff:

AttemptDelay
1st retry~1 second
2nd retry~2 seconds
3rd retry~4 seconds

Events remain in the queue across page loads (persisted in localStorage). If the queue exceeds maxQueueSize (default: 1000), the oldest events are dropped.


React SPA (Vite / Create React App)

Add the CDN snippet to your index.html:

<!-- index.html --> <head> <script> (function(w,d,s,e,l,k){w['VendoObject']=e;w[e]=w[e]||function(){ (w[e].q=w[e].q||[]).push(arguments)};w[e].l=1*new Date(); l=d.createElement(s);k=d.getElementsByTagName(s)[0]; l.async=1;l.src='https://cdn.vendodata.com/sdk/v1/vendo.js';k.parentNode.insertBefore(l,k); })(window,document,'script','vendo'); vendo('init', 'YOUR_WRITE_KEY', { trackPageViews: true }); </script> </head>

For React Router, track route changes with a component:

// components/TrackPageViews.tsx import { useEffect } from 'react'; import { useLocation } from 'react-router-dom'; declare global { interface Window { vendo?: (...args: unknown[]) => void; } } export function TrackPageViews() { const location = useLocation(); useEffect(() => { if (typeof window.vendo === 'function') { window.vendo('page'); } }, [location]); return null; }

Add it inside your <BrowserRouter>:

<BrowserRouter> <TrackPageViews /> <Routes> {/* your routes */} </Routes> </BrowserRouter>

Framework Guides

For framework-specific setup (route-change tracking, plugins, etc.), see the dedicated guides:

  • Next.js — App Router and Pages Router
  • Vue — Vue 3 plugin with Vue Router
  • Nuxt — Nuxt 3 client plugin

Last updated on