Forms
Snowplow form tracking creates three event types: change_form, submit_form and focus_form. Using the enableFormTracking method adds event listeners to all form elements and to all interactive elements inside forms (that is, all input, textarea, and select elements).
This will only work for form elements which exist when it is called. If you are creating a form programmatically, call enableFormTracking again after adding it to the document to track it. You can call enableFormTracking multiple times without risk of duplicated events. From v3.2.0, if you are programmatically adding additional fields to a form after initially calling enableFormTracking then calling it again after the new form fields are added will include them in form tracking.
Events on password fields will not be tracked.
Form events are automatically tracked once configured.
Installation#
- JavaScript (tag)
- Browser (npm)
| Tracker Distribution | Included |
|---|---|
sp.js | ✅ |
sp.lite.js | ❌ |
Download:
| Download from GitHub Releases (Recommended) | Github Releases (plugins.umd.zip) |
| Available on jsDelivr | jsDelivr (latest) |
| Available on unpkg | unpkg (latest) |
Note: The links to the CDNs above point to the current latest version. You should pin to a specific version when integrating this plugin on your website if you are using a third party CDN in production.
npm install @snowplow/browser-plugin-form-tracking@3yarn add @snowplow/browser-plugin-form-tracking@3pnpm add @snowplow/browser-plugin-form-tracking@3
Enable form tracking#
- JavaScript (tag)
- Browser (npm)
window.snowplow('addPlugin',
"https://cdn.jsdelivr.net/npm/@snowplow/browser-plugin-form-tracking@3/dist/index.umd.min.js",
["snowplowFormTracking", "FormTrackingPlugin"]
);
snowplow('enableFormTracking');
This is part of the @snowplow/browser-plugin-form-tracking plugin. You need to install it with your favorite package manager: npm install @snowplow/browser-plugin-form-tracking and then initialize it:
import { newTracker, trackPageView } from '@snowplow/browser-tracker';
import { FormTrackingPlugin, enableFormTracking } from '@snowplow/browser-plugin-form-tracking';
newTracker('sp1', '{{collector_url}}', {
appId: 'my-app-id',
plugins: [ FormTrackingPlugin() ],
});
enableFormTracking();
This will only work for form elements which exist when it is called. If you are creating a form programmatically, call enableFormTracking again after adding it to the document to track it. You can call enableFormTracking multiple times without risk of duplicated events. From v3.2.0, if you are programmatically adding additional fields to a form after initially calling enableFormTracking then calling it again after the new form fields are added will include them in form tracking.
By default, all three event types are tracked. However, it is possible to subscribe only to specific event types using the options.events option when enabling form tracking:
- JavaScript (tag)
- Browser (npm)
// subscribing to specific event types
snowplow('enableFormTracking', {
options: {
events: ['submit_form', 'focus_form', 'change_form']
},
});
// subscribing to specific event types
enableFormTracking({
options: {
events: ['submit_form', 'focus_form', 'change_form']
},
});
change_form#
When a user changes the value of a textarea, input, or select element inside a form, a change_form event will be fired. It will capture the name, type, and new value of the element, and the id of the parent form.
submit_form#
When a user submits a form, a submit_form event will be fired. It will capture the id and classes of the form and the name, type, and value of all textarea, input, and select elements inside the form.
Note that this will only work if the original form submission event is actually fired. If you prevent it from firing, for example by using a jQuery event handler which returns false to handle clicks on the form's submission button, the Snowplow submit_form event will not be fired.
focus_form#
When a user focuses on a form element, a focus_form event will be fired. It will capture the id and classes of the form and the name, type, and value of the textarea, input, or select element inside the form that received focus.
Configuration#
It may be that you do not want to track every field in a form, or every form on a page. You can customize form tracking by passing a configuration argument to the enableFormTracking