Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SD-256] - Added form abandon event #1336

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/nuxt-ripple-analytics/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,18 @@ export default {
})
}
},
'rpl-form/abandon': () => {
return (payload: any) => {
trackEvent({
event: `form_abandon`,
form_id: payload?.id,
form_name: payload?.name,
component: 'rpl-form',
platform_event: 'abandon',
form_data: payload?.value
})
}
},
'rpl-form/submit': () => {
return (payload: any) => {
trackEvent({
Expand Down
44 changes: 41 additions & 3 deletions packages/ripple-ui-forms/src/components/RplForm/RplForm.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<script setup lang="ts">
import { nextTick, provide, ref, watch, reactive, computed } from 'vue'
import {
nextTick,
provide,
ref,
watch,
reactive,
computed,
onBeforeUnmount
} from 'vue'
import {
getNode,
FormKitSchemaCondition,
Expand Down Expand Up @@ -81,10 +89,34 @@ const submitCounter = ref(0)
// Keep track of whether user has changed something in the form
const formStarted = ref<boolean>(false)

const tryAbandonForm = () => {
if (formStarted.value && props.submissionState.status !== 'success') {
emitRplEvent(
'abandon',
{
id: props.id,
name: props.title,
value: sanitisePIIFields(getNode(props.id))
},
{ global: true }
)
}

formStarted.value = false
}

const onFormReset = () => {
cachedErrors.value = {}
submitCounter.value = 0

tryAbandonForm()
}

provide('form', { id: props.id, name: props.title })
provide('isFormSubmitting', isFormSubmitting)
// submitCounter is watched by some components to efficiently know when to update
provide('submitCounter', submitCounter)
provide('onFormReset', onFormReset)

const submitLabel =
props.schema?.find((field) => field?.key === 'actions')?.label || 'Submit'
Expand Down Expand Up @@ -196,13 +228,15 @@ watch(
{ global: true }
)

formStarted.value = false

reset(props.id)
}
}
}
)

const handleChange = () => {
const handleInput = () => {
// 'Form start' analytics event, fires on first change of the form
if (!formStarted.value) {
formStarted.value = true
Expand All @@ -218,6 +252,10 @@ const handleChange = () => {
}
}

onBeforeUnmount(() => {
tryAbandonForm()
})

const data = reactive({
isFilled: (val) =>
typeof val === 'number'
Expand Down Expand Up @@ -276,7 +314,7 @@ const plugins = computed(
novalidate
@submit-invalid="submitInvalidHandler"
@submit="submitHandler"
@input="handleChange"
@input="handleInput"
>
<fieldset
class="rpl-form__submit-guard"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ const iconPosition = computed(() => {

const form: object = inject('form')
const isFormSubmitting: any = inject('isFormSubmitting')
const onFormReset = inject('onFormReset')

const handleReset = () => {
if (typeof onFormReset === 'function') {
onFormReset()
}

reset(form.id)

emitRplEvent(
Expand Down