Device Mode allows you to capture E-commerce events directly from the storefront using a JavaScript tracker. The tracker listens for customer interactions and lets your application process them in real time.Common use cases include:Analytics and event tracking
Personalization and UI reactions
Forwarding events to external services
Quickstart (5 Minutes)#
If you just want to see events working quickly:Trigger actions on the page and check the browser console to see emitted events.Once confirmed, you can customize listeners inside:
Integration Overview#
Device Mode works by injecting a tracker script into the storefront using an App Snippet.The flow looks like this:User Action
│
▼
Twilight SDK emits event
│
▼
tracker.js receives event
│
▼
Your listener processes payload
│
▼
Analytics / Webhook / API
App Snippet Requirement#
To load your tracker in the merchant storefront, Device Mode requires an App Snippet.The snippet injects your hosted tracker.js file into the store.
Prerequisites#
Before starting, install:pnpm (recommended) or npm
1. Clone the Starter Kit#
Salla provides a starter kit that includes:TypeScript types for events
2. Run the Development Environment#
Open the provided testing page:Trigger interactions to simulate events and verify them in the browser console.
Project Structure#
The starter kit follows a simple modular structure:store-events-tracker-starter-kit
│
├── src
│ ├── listeners
│ │ ├── cart-updated.ts
│ │ ├── product-added.ts
│ │ └── checkout-started.ts
│ │
│ ├── tracker.ts
│ └── index.ts
│
├── example.html
├── package.json
└── vite.config.ts
| Directory | Purpose |
|---|
/src/listeners | Event handlers |
/src/tracker.ts | Tracker initialization |
example.html | Local testing page |
3. Customize Event Logic#
Each event has a dedicated listener inside:Example: sending the Cart Updated event to a webhook.import {
CartUpdatedPayload,
EcommerceEvents,
} from "@salla.sa/ecommerce-events-base";
export const eventName = EcommerceEvents.CART_UPDATED;
export default async (payload: CartUpdatedPayload): Promise<void> => {
try {
const response = await fetch("https://acme.com/webhook", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
event: eventName,
payload,
}),
});
if (!response.ok) {
console.error(
`Webhook failed: ${response.status} ${response.statusText}`
);
}
} catch (error) {
console.error("Webhook error:", error);
}
};
Example Event Payload#
Example payload for Cart Updated:{
"event": "cart.updated",
"timestamp": "2026-03-09T10:15:20Z",
"store_id": 12345,
"customer_id": 99821,
"cart": {
"id": "cart_abc123",
"total": 320,
"currency": "SAR",
"items": [
{
"product_id": 54321,
"name": "Wireless Headphones",
"quantity": 1,
"price": 320
}
]
}
}
This payload can be used for:
4. Add New Event Listeners#
To listen for new events:Create a new file inside:import { EventPayload, EcommerceEvents } from "@salla.sa/ecommerce-events-base";
export const eventName = EcommerceEvents.YOUR_EVENT_NAME;
export default (payload: EventPayload): void => {
console.log("Event received:", payload);
// Custom logic
};
The build system will automatically register new listeners.
5. Deploy the Tracker#
Once your tracker is ready, deploy it so merchants can install it.
Build the Tracker#
Upload to a CDN#
Upload the file to a CDN such as:https://cdn.example.com/tracker.js
Add the App Snippet#
1.
Open the Salla Partners Portal
4.
Add the CDN URL of tracker.js
The snippet will automatically inject the tracker into merchant stores.
Testing in a Demo Store#
1.
Install the app in a demo store
Publish the App#
After successful testing:1.
Return to the Salla Partners Portal
Merchants can now install your tracker.
Recommended Architecture#
For production apps, a common pattern is forwarding events to your backend.Storefront
│
▼
tracker.js
│
▼
Webhook / API Gateway
│
▼
Processing Service
│
├── Analytics (Segment / Amplitude)
├── Data Warehouse
└── Automation Systems
centralized event processing
scalable analytics pipelines
easier integrations with external services
Development Commands#
Debugging Tips#
If events are not firing:Check browser console logs
Ensure Twilight SDK loads before tracker
Run TypeScript validation
Troubleshooting#
Ensure the snippet is installed correctly
Confirm the Twilight SDK loads first
Check listener implementation for runtime errors
Verify event listener exports
Ensure files follow the required listener pattern
Modified at 2026-03-09 07:42:17