Retour au tableau de bord

Documentation

Apprenez à utiliser Asyntai

Set Up User Context on Ecwid

Pass Ecwid customer info to your chatbot using the Storefront JS API

Back to User Context
Standard & Pro Plans

What You're Setting

User Context is a client-side JavaScript object. On every page where the widget loads for a logged-in user, you attach an object to window.Asyntai.userContext describing who they are. The widget sends this with each message, so the AI can reply with their name, order status, plan tier, or anything else relevant.

Keys act as labels the AI sees, so make them descriptive — use "Customer name" or "Loyalty points", not just "name" or "points".

window.Asyntai = window.Asyntai || {};
window.Asyntai.userContext = {
  "Customer name": "Sarah Chen",
  "Email": "sarah@example.com",
  "Subscription plan": "Pro",
  "Loyalty points": 1840,
  "Last order": "#8847, out for delivery"
};

Two ways to set it

Either assign synchronously (if data is already on the page), or provide a fetcher that runs when the chat opens. The second option is better for performance — user data is only loaded when someone actually opens the chat.

// Option A — synchronous (data already available)
window.Asyntai.userContext = { "Customer name": "Sarah", ... };

// Option B — async fetch on chat open (better performance)
window.Asyntai.fetchUserContext = function() {
  return fetch('/api/chat-context/')
    .then(r => r.json())
    .then(data => { window.Asyntai.userContext = data; });
};

Security & size: Never include passwords, credit card numbers, API tokens, or anything sensitive — this data is on the client and visible to anyone inspecting the page. Context is capped at 2,000 characters on Standard and 10,000 characters on Pro; if you exceed it, the tail is truncated.

How Ecwid Exposes the Customer

Ecwid's Storefront JS API provides two callback-based methods: Ecwid.Customer.get() reads the currently logged-in customer on demand, and Ecwid.OnSetProfile.add() fires whenever a customer logs in or out. Both are accessed from a Storefront Startup Script in Ecwid's control panel.

Option 1 — Storefront Startup Script (recommended)

1
Open the Ecwid control panel Go to Settings → General → Cart → scroll to Storefront Startup Scripts (or Legacy: Configuration → Design → Custom Code). Some Ecwid plans also expose this via Developer → Storefront Startup Scripts.
2
Paste the script below and save It reads the current customer once the API is ready, then keeps context in sync on login/logout.
// Ecwid Storefront Startup Script
function applyAsyntaiContext(customer) {
  window.Asyntai = window.Asyntai || {};
  if (!customer) {
    // Customer logged out — clear context
    window.Asyntai.userContext = undefined;
    return;
  }
  window.Asyntai.userContext = {
    "Customer name":   customer.billingPerson ? customer.billingPerson.name : customer.email,
    "Email":           customer.email,
    "Customer ID":     customer.id,
    "Registration date": customer.registered
      ? new Date(parseInt(customer.registered, 10) * 1000).toLocaleDateString()
      : null,
    "Customer group":  customer.groupId || null
  };
}

Ecwid.OnAPILoaded.add(function() {
  // Read the customer on initial page load
  Ecwid.Customer.get(function(customer) {
    applyAsyntaiContext(customer);
  });

  // Stay in sync when they log in / out
  Ecwid.OnSetProfile.add(function(customer) {
    applyAsyntaiContext(customer);
  });
});

Why two hooks? Ecwid.Customer.get() reads the current state once, while Ecwid.OnSetProfile.add() fires on every login/logout. Combining both means the context is correct on first load AND updates if the visitor signs in mid-session.

Callback-based API: Ecwid.Customer.get() does not return the customer directly — you pass it a callback that receives the customer object. The callback argument is null when no customer is logged in.

Option 2 — Proxy via Ecwid REST API (for richer data)

The most reliable method: use a server-side proxy that reads the Ecwid session cookie (or a small customer-identifier param) and calls the Ecwid REST API to retrieve customer details. Then plug it into fetchUserContext:

// On any Ecwid page
window.Asyntai = window.Asyntai || {};
window.Asyntai.fetchUserContext = function() {
  return fetch('/your-proxy/user-context', { credentials: 'include' })
    .then(r => r.json())
    .then(data => { window.Asyntai.userContext = data; });
};

Your proxy calls:

GET https://app.ecwid.com/api/v3/{STORE_ID}/customers/{CUSTOMER_ID}
Authorization: Bearer YOUR_SECRET_TOKEN

Then maps the response into the user context JSON. Use the secret (not public) token in the proxy — it stays server-side.

Astuce : Ecwid's Storefront Startup Scripts run on every page load of the embedded storefront — perfect for setting context, since it re-runs on navigation too.

Dépannage

The AI doesn't seem to know who I am

Open your browser DevTools → Console → type window.Asyntai.userContext after the page loads. You should see your object. If it says undefined, your script didn't run — check the script order (context must be set after the widget script loads, or be re-set whenever the user logs in).

Context works on some pages but not others

The widget reads window.Asyntai.userContext on every message. If a page loads fresh (no SPA routing), you need to set context on that page too. For single-page apps, set it once after login and update it whenever user data changes.

Data is stale after the user updates their profile

Re-assign window.Asyntai.userContext with the fresh data after any update. The next message the user sends will include the updated values — no page reload needed.

Context is truncated

You're over the size limit (2k chars Standard, 10k Pro). Trim verbose fields — keep order history to last 2-3 items, truncate long descriptions, drop fields the AI doesn't need.

User Context status page shows "Not receiving context"

Visit the User Context settings page while logged in as a test user, then send a chat message. The status refreshes within a few seconds. If still empty, check the browser console for JavaScript errors and verify the object is set before the chat message is sent.

Privacy reminder: Only share fields relevant to the conversation. Passing a customer's full purchase history when they just want to ask a general question is wasteful and can confuse the AI. Scope context to what helps.