O
O
Oleg Rudzhev2019-11-06 16:57:34
Google Analytics
Oleg Rudzhev, 2019-11-06 16:57:34

How to set Google Analytics goal to submit Contact Form 7 in WP?

Good day.
Help, please, who faced such a task: on the site superpila.com.ua it is necessary that after sending the "Leave an order" form ( on this product page for example ) and the second form "Ask a specialist" (on the same page, but it located in the site header) parameters were passed - gtag('event', 'ostavitzakaz', {'event_category': 'formajax'}); and gtag('event', 'specvopros', {'event_category': 'formajax1'}); respectively.
But I ran through the php files, did not find any submission of these forms, or I simply do not see them.
Tell me where you need to insert this data so that after submitting these forms, they are transferred to Google Analytics (and counted as an order and form submission).
PS I kind of found how to do it in this article , but it does not indicate WHERE to insert the script from their example (in functions.php or in the settings of the Contact Form 7 plugin itself):

<script>
    document.addEventListener( 'wpcf7mailsent', function( event ) {
        gtag('event','submit', {'event_category' : 'Form'});
    }, false );
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yanchevsky, 2019-11-06
@olegwebos

Good.
You don't need to put it in settings. It can be in functions.php (for example, using the wp_footer hook), it can be in a js file, it can be in some plugin for snippets.
Also, if you need to send from a specific form, then you need to check its ID.

document.addEventListener( 'wpcf7mailsent', function( event ) {
    if ( '59' == event.detail.contactFormId ) {
        gtag('event','submit', {'event_category' : 'Form'});
    }
}, false );

Documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question