25 lines
1.1 KiB
TypeScript
25 lines
1.1 KiB
TypeScript
/**
|
||
* Pushes an event onto the GTM `window.dataLayer`. SSR-safe (no-op on the
|
||
* server), and tolerant of GTM not being present yet — the array is created so
|
||
* the event is buffered and picked up once GTM loads.
|
||
*
|
||
* The bridge between the form engine and GTM: fire a `form_submit` event after
|
||
* a successful submit so a Meta Pixel / GA "Lead" tag can trigger on it.
|
||
*
|
||
* import { pushDataLayer } from '@intecion/ipal-kit/client'
|
||
* const f = useContactForm({
|
||
* submitAction,
|
||
* onSuccess: (_r, v) => pushDataLayer('form_submit', { form_name: v.topic || 'kontakt' }),
|
||
* })
|
||
*
|
||
* The GTM "Lead" tag then reads `form_name` via a Data Layer Variable
|
||
* (`DLV – form_name`) and fires on the custom event `form_submit`. Consent still
|
||
* gates the tag in GTM (ad_storage) — this only emits the event.
|
||
*/
|
||
export function pushDataLayer(event: string, params: Record<string, unknown> = {}): void {
|
||
if (typeof window === 'undefined') return
|
||
const w = window as unknown as { dataLayer?: unknown[] }
|
||
w.dataLayer = w.dataLayer || []
|
||
w.dataLayer.push({ event, ...params })
|
||
}
|