サーバーサイドテンプレートからユーザーデータを直接出力します:
<script>
window.Asyntai = window.Asyntai || {};
window.Asyntai.userContext = {
"Customer name": "<?php echo $user->name; ?>",
"Email": "<?php echo $user->email; ?>",
"Subscription plan": "<?php echo $user->plan; ?>",
"Order status": "<?php echo $user->latest_order_status; ?>"
};
</script>
アプリがユーザーデータを読み込んだ後にコンテキストを設定します:
window.Asyntai = window.Asyntai || {};
window.Asyntai.userContext = {
"Customer name": currentUser.name,
"Email": currentUser.email,
"Subscription plan": currentUser.subscription.planName,
"Cart total": cart.total,
"Loyalty points": currentUser.loyaltyPoints
};
window.Asyntai.userContext["Cart total"] = newTotal;
Fetch user data only when the chat opens (best for performance):
window.Asyntai = window.Asyntai || {};
window.Asyntai.fetchUserContext = function() {
return fetch('/api/your-user-context-endpoint/')
.then(function(response) { return response.json(); })
.then(function(data) {
window.Asyntai.userContext = data;
});
};
パフォーマンスに最適: ウィジェットはfetchUserContextを検出し、チャットが開かれたときに自動的に呼び出すため、データは必要なときにのみ読み込まれます。