The “on_sent_ok” and its sibling setting “on_submit” are deprecated and scheduled to be abolished by the end of 2017. It’s not that using those settings is unsafe, but it’s possible that enabling them will increase risk in case there are vulnerabilities in this plugin or in other components of your site. It’s time to replace them with a safer alternative.
The recommended alternative to on_sent_ok is using DOM events. Coding with DOM events has a number of advantages over using on_sent_ok. For the details of Contact Form 7’s custom DOM events, please refer to the documentation.
In the rest of this post, I’ll explain with some examples how you can replace your
Replacing on_sent_ok with DOM events
Suppose that you currently have the following line of settings in the Additional Settings tab:
on_sent_ok: "ga( 'send', 'event', 'Contact Form', 'submit' );"
This setting tells Contact Form 7 to run the
ga() (Google Analytics tracker) function when a form submission completes and mail has been sent successfully.
To replace it with an alternative code using DOM events, first, find the file named “functions.php” in the directory of your active theme. Second, add the following code at the bottom of the functions.php file:
Third, remove the “on_sent_ok” line from the Additional Settings tab and save the contact form.
Targeting a specific contact form
If you have several contact forms and want to run the function only for a specific contact form, check the ID of the contact form like the following:
In this case, the
ga() function will be executed only if the ID of the contact form (
event.detail.contactFormId) is “123”.
You can find the ID of a contact form by looking for the “id” attribute in the contact-form-7 shortcode. For example, if the shortcode looks like this:
[contact-form-7 id="1234" title="Contact form 1"]
The ID of this contact form is “1234”.