Merged in fix/SW-3115-form-tracking-changes (pull request #2470)
fix(SW-3115): add form complete tracking and removed input error tracking * fix(SW-3115): add form complete tracking and removed input error tracking Approved-by: Bianca Widstam
This commit is contained in:
committed by
Bianca Widstam
parent
86bd3fcea3
commit
d272cd03ce
@@ -1,10 +1,15 @@
|
||||
"use client"
|
||||
import isEqual from "fast-deep-equal"
|
||||
|
||||
import { usePathname } from "next/navigation"
|
||||
import { startTransition, useEffect, useRef, useState } from "react"
|
||||
import {
|
||||
startTransition,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from "react"
|
||||
import {
|
||||
type Control,
|
||||
type FieldErrors,
|
||||
type FieldValues,
|
||||
useFormState,
|
||||
type UseFromSubscribe,
|
||||
@@ -21,8 +26,8 @@ import { createSDKPageObject, trackPageView } from "@/utils/tracking"
|
||||
import {
|
||||
type FormType,
|
||||
trackFormAbandonment,
|
||||
trackFormCompletion,
|
||||
trackFormInputStarted,
|
||||
trackFormValidationError,
|
||||
} from "@/utils/tracking/form"
|
||||
|
||||
import type {
|
||||
@@ -284,38 +289,10 @@ export function useFormTracking<T extends FieldValues>(
|
||||
const [formStarted, setFormStarted] = useState(false)
|
||||
const lastAccessedField = useRef<string | undefined>(undefined)
|
||||
const formState = useFormState({ control })
|
||||
const previousErrors = useRef<FieldErrors<T>>({})
|
||||
|
||||
useEffect(() => {
|
||||
const errors = formState.errors
|
||||
const prevErrors = previousErrors.current
|
||||
|
||||
const isNewErrors = !isEqual(errors, prevErrors)
|
||||
if (Object.keys(errors).length && isNewErrors) {
|
||||
const errorString = Object.values(errors)
|
||||
.map((err) => {
|
||||
if (err && "message" in err) {
|
||||
return err.message
|
||||
}
|
||||
const nested = Object.values(err ?? {}).find(
|
||||
(val) => val && typeof val === "object" && "message" in val
|
||||
)
|
||||
if (nested) {
|
||||
return nested.message
|
||||
}
|
||||
return undefined
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join("|")
|
||||
|
||||
trackFormValidationError(formType, errorString, nameSuffix)
|
||||
previousErrors.current = { ...errors }
|
||||
}
|
||||
}, [formType, formState, nameSuffix])
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = subscribe({
|
||||
formState: { touchedFields: true },
|
||||
formState: { dirtyFields: true },
|
||||
callback: (data) => {
|
||||
if ("name" in data) {
|
||||
lastAccessedField.current = data.name as string
|
||||
@@ -353,4 +330,14 @@ export function useFormTracking<T extends FieldValues>(
|
||||
window.removeEventListener("visibilitychange", handleVisibilityChange)
|
||||
}
|
||||
}, [formStarted, formType, nameSuffix, formState.isValid])
|
||||
|
||||
const trackFormSubmit = useCallback(() => {
|
||||
if (formState.isValid) {
|
||||
trackFormCompletion(formType, nameSuffix)
|
||||
}
|
||||
}, [formType, nameSuffix, formState.isValid])
|
||||
|
||||
return {
|
||||
trackFormSubmit,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user