@nuasite/components 0.15.2 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@nuasite/components",
3
3
  "description": "Nua Site astro components.",
4
4
  "license": "Apache-2.0",
5
- "version": "0.15.2",
5
+ "version": "0.16.0",
6
6
  "files": [
7
7
  "dist/**",
8
8
  "src/**",
@@ -219,16 +219,32 @@ const tokenId = `${tokenFieldName}_${formId}`
219
219
 
220
220
  refreshSubmissionGuards()
221
221
 
222
+ const trackForm = (action: string, detail?: Record<string, unknown>) => {
223
+ if (typeof window !== 'undefined' && (window as any).analytics?.form) {
224
+ (window as any).analytics.form(formId, action, detail)
225
+ }
226
+ }
227
+
222
228
  form.addEventListener('submit', async (e) => {
223
229
  e.preventDefault()
224
230
 
231
+ const formDataForTracking = Object.fromEntries(
232
+ Array.from(new FormData(form).entries())
233
+ .filter(([k]) => !this.honeypotFieldNames.includes(k) && !/^token_/.test(k) && !form.querySelector(`[name="${k}"][aria-hidden="true"]`))
234
+ .map(([k, v]) => [k, typeof v === 'string' ? v.slice(0, 100) : '[File]']),
235
+ )
236
+
237
+ trackForm('attempt', { formData: formDataForTracking })
238
+
225
239
  if (!this.passesHumanityChecks(form)) {
240
+ trackForm('reject', { reason: 'humanity_check_failed', formData: formDataForTracking })
226
241
  showError(tryAgainMessage)
227
242
  return
228
243
  }
229
244
 
230
245
  const elapsed = performance.now() - activationTime
231
246
  if (minSubmitDelay > 0 && elapsed < minSubmitDelay) {
247
+ trackForm('reject', { reason: 'submitted_too_fast', formData: formDataForTracking })
232
248
  showError(fastSubmitMessage || tryAgainMessage)
233
249
  return
234
250
  }
@@ -236,6 +252,7 @@ const tokenId = `${tokenFieldName}_${formId}`
236
252
  activationTime = performance.now()
237
253
 
238
254
  if (tokenInput && !tokenInput.value) {
255
+ trackForm('reject', { reason: 'missing_token', formData: formDataForTracking })
239
256
  showError(tryAgainMessage)
240
257
  return
241
258
  }
@@ -254,15 +271,18 @@ const tokenId = `${tokenFieldName}_${formId}`
254
271
 
255
272
  if (response.ok && 'success' in result && result.success) {
256
273
  const message = hasCustomSuccess ? successMessage : (result.message || successMessage)
274
+ trackForm('success', { formData: formDataForTracking })
257
275
  showSuccess(message)
258
276
  form.reset()
259
277
  refreshSubmissionGuards()
260
278
  } else {
261
279
  const message = hasCustomError ? errorMessage : (result.error || result.message || errorMessage)
280
+ trackForm('error', { reason: result.error || `http_${response.status}`, formData: formDataForTracking })
262
281
  showError(message)
263
282
  }
264
283
  } catch (err) {
265
284
  console.error('Form submission error:', err);
285
+ trackForm('error', { reason: err instanceof Error ? err.message : 'network_error', formData: formDataForTracking })
266
286
  showError(networkErrorMessage)
267
287
  }
268
288
  })