If your site uses the old version of WordPress and the Contact Form 7 plugin, it is enough to use the code in the Advanced settings tab:
on_sent_ok: "yaCounterXXXXXXXX.reachGoal('mygoal');"
Where XXXXXXXX is the counter ID in Yandex Metrika, mygoal is the name of the goal that was previously created in Yandex Metrika.
For the Contact Form version 5+, the old method does not work, and you need to use the following code:
<script type="text/javascript"> document.addEventListener( 'wpcf7mailsent', function(event) { yaCounterXXXXXXXX.reachGoal('mygoal'); }, false); </script>
Similarly, XXXXXXXX is the counter id in Yandex Metrika, mygoal is the name of the goal that was previously created in Yandex Metrika.
Edit and place this code in header.php or footer.php in your website theme folder.