डैशबोर्ड पर वापस जाएं

दस्तावेज़ीकरण

Asyntai का उपयोग करना सीखें

Set Up User Context on Magento / Adobe Commerce

Pass the logged-in customer's data using Magento's customerData JS module

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 Magento Exposes the Customer

Magento 2 uses a client-side section called customer that's loaded via the Magento_Customer/js/customer-data RequireJS module. It holds the current customer's name, email, and greeting text — updated automatically on login / logout via AJAX.

Option 1 — RequireJS Inline Script

Hook into customerData inside a RequireJS block. Place this in any theme layout file (or use a Miscellaneous HTML setting in admin so you don't need theme changes).

1
Open admin → Content → Design → Configuration Pick your store, scroll to HTML Head → Miscellaneous HTML — this accepts raw script tags that get injected on every page.
2
Paste the script below, save, and flush cache System → Cache Management → Flush Magento Cache so the new Miscellaneous HTML is picked up.
<script>
require(['Magento_Customer/js/customer-data'], function(customerData) {
  var customer = customerData.get('customer');
  customer.subscribe(function(data) {
    if (!data || !data.fullname) return;
    window.Asyntai = window.Asyntai || {};
    window.Asyntai.userContext = {
      "Customer name": data.fullname,
      "Email":         data.email || '',
      "Customer group": data.customerGroupId
    };
  });
  // Trigger once on page load if already populated
  var initial = customer();
  if (initial && initial.fullname) {
    window.Asyntai = window.Asyntai || {};
    window.Asyntai.userContext = {
      "Customer name": initial.fullname,
      "Email":         initial.email || '',
    };
  }
});
</script>

What's in customerData by default: Magento ships the customer section with firstname, fullname, and a few privacy-safe fields. Order history and total spent are not included by default — for those, use Option 2 below.

Option 2 — Fetch Rich Data via REST API

For orders, loyalty points, and custom attributes, use fetchUserContext with a proxy that calls Magento's REST API:

<script>
window.Asyntai = window.Asyntai || {};
window.Asyntai.fetchUserContext = function() {
  return fetch('/rest/V1/customers/me', {
    credentials: 'include'
  })
  .then(r => r.ok ? r.json() : null)
  .then(c => {
    if (!c) return;
    window.Asyntai.userContext = {
      "Customer name": c.firstname + ' ' + c.lastname,
      "Email":         c.email,
      "Customer group": c.group_id,
      "Member since":  c.created_at
    };
  });
};
</script>

सुझाव: The /rest/V1/customers/me endpoint uses Magento's session cookie for authentication — no additional token needed. It works for any customer logged into the storefront.

Custom Module (production sites)

For production, build a lightweight module that adds fields to the customer section of customerData (via a custom section data source plus etc/frontend/sections.xml). This gives you Magento-native reactivity and avoids an extra REST call per chat open.

समस्या निवारण

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.