Install target
One v1 widget script in the app shell
Install AnswerLattice
React SPA guidance for installing the v1 widget once and updating context on router changes.
One v1 widget script in the app shell
Path, title, feature, workflow, role, locale
Loaded, origin allowed, route allowed, context received
Install verification
Script, allowed origin, blocked route, safe context, and fallback checks are visible before product users rely on support.
Add the v1 widget script and safe context adapter.
Verify origin, route, context, and fallback readiness.
Go live only after the runtime checks pass.
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
function loadAnswerlattice(widgetKey, updateContext) {
const existing = document.querySelector('script[data-answerlattice-widget="v1"]');
if (existing) {
if (window.AnswerlatticeWidget) updateContext();
else existing.addEventListener('load', updateContext, { once: true });
return () => existing.removeEventListener('load', updateContext);
}
const script = document.createElement('script');
script.src = 'https://answerlattice.com/widget/v1/answerlattice-widget.js';
script.async = true;
script.setAttribute('data-answerlattice-widget', 'v1');
script.setAttribute('data-answerlattice-key', widgetKey);
script.addEventListener('load', updateContext, { once: true });
document.head.appendChild(script);
return () => script.removeEventListener('load', updateContext);
}
export function AnswerlatticeInstall() {
const location = useLocation();
const widgetKey = import.meta.env.VITE_ANSWERLATTICE_WIDGET_KEY;
useEffect(() => {
if (!widgetKey) return;
const updateContext = () => window.AnswerlatticeWidget?.page({
path: location.pathname,
title: document.title,
feature: location.pathname.split('/').filter(Boolean)[0] || 'app',
role: 'member',
locale: navigator.language || 'en',
});
const removeLoadListener = loadAnswerlattice(widgetKey, updateContext);
updateContext();
return removeLoadListener;
}, [location.pathname, widgetKey]);
return null;
}