/**
 * GA4 event tracking for Actuate CMS forms.
 *
 * This module pushes structured events to the site's existing gtag() installation.
 * It does NOT load the GA4 script — that's the site owner's responsibility via
 * their Google Analytics snippet or GTM container.
 */
const isBrowser = typeof window !== 'undefined';
function hasGtag() {
    return isBrowser && typeof window.gtag === 'function';
}
function sendEvent(eventName, params, measurementId) {
    if (!hasGtag())
        return;
    if (measurementId) {
        window.gtag('event', eventName, {
            send_to: measurementId,
            ...params,
        });
    }
    else {
        window.gtag('event', eventName, params);
    }
}
/**
 * Track when a user first interacts with a form (focus on first field).
 * Maps to the GA4 recommended "form_start" event.
 */
export function trackFormStart(formName, formId, config) {
    if (!config?.enabled)
        return;
    sendEvent(config.startEventName ?? 'form_start', {
        form_name: formName,
        form_id: formId,
        ...config.customParameters,
    }, config.measurementId);
}
/**
 * Track a successful form submission.
 * Maps to the GA4 recommended "form_submit" event with optional conversion data.
 */
export function trackFormSubmit(formName, formId, config, extra) {
    if (!config?.enabled)
        return;
    const params = {
        form_name: formName,
        form_id: formId,
        ...config.customParameters,
        ...extra,
    };
    if (config.trackAsConversion) {
        params.value = config.conversionValue ?? 0;
        params.currency = config.conversionCurrency ?? 'USD';
    }
    sendEvent(config.submitEventName ?? 'form_submit', params, config.measurementId);
}
/**
 * Track a form submission error.
 */
export function trackFormError(formName, formId, errorMessage, config) {
    if (!config?.enabled)
        return;
    sendEvent('form_error', {
        form_name: formName,
        form_id: formId,
        error_message: errorMessage,
        ...config.customParameters,
    }, config.measurementId);
}
/**
 * Push a custom dataLayer event (for GTM users who prefer dataLayer over gtag).
 */
export function pushDataLayerEvent(eventName, data) {
    if (!isBrowser)
        return;
    window.dataLayer = window.dataLayer ?? [];
    window.dataLayer.push({
        event: eventName,
        ...data,
    });
}
/** Default confirmation config. */
export function getDefaultConfirmation() {
    return {
        type: 'message',
        message: 'Thank you! Your submission has been received.',
    };
}
/** Default analytics config. */
export function getDefaultAnalyticsConfig() {
    return {
        enabled: false,
    };
}
//# sourceMappingURL=analytics.js.map