35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
import { pushDataLayer } from '../analytics/pushDataLayer.js';
|
|
/**
|
|
* Builds an `onSuccess` handler from a form's panel-configured analytics, so the
|
|
* GTM `dataLayer` event is emitted on submit with ZERO per-form code. Wire it
|
|
* once in the shared form renderer — every form then gets its tracking from the
|
|
* panel:
|
|
*
|
|
* const f = useContactForm({
|
|
* submitAction,
|
|
* onSuccess: formAnalyticsOnSuccess(form.analytics),
|
|
* })
|
|
*
|
|
* A param value starting with `$` is resolved from the submitted values, so an
|
|
* editor can set `form_name = $topic` and get the chosen topic with no code.
|
|
* No event name configured → the returned handler is a no-op.
|
|
*/ export function formAnalyticsOnSuccess(config) {
|
|
return (_result, values)=>{
|
|
const event = config?.event?.trim();
|
|
if (!event) {
|
|
return;
|
|
}
|
|
const params = {};
|
|
for (const p of config?.params ?? []){
|
|
const key = p?.key?.trim();
|
|
if (!key) {
|
|
continue;
|
|
}
|
|
const raw = typeof p?.value === 'string' ? p.value : '';
|
|
params[key] = raw.startsWith('$') ? values[raw.slice(1)] ?? '' : raw;
|
|
}
|
|
pushDataLayer(event, params);
|
|
};
|
|
}
|
|
|
|
//# sourceMappingURL=formAnalytics.js.map
|