简体   繁体   中英

How to insert data on Google Tag Manager's dataLayer using wpForms wpforms_process_complete on Wordpress

I want to push an event to Google Tag Manager's dataLayer when the user submitted the form successfully using wpForms plugin. I tried using the wpForms hook "wpforms_process_complete" and it seems that it's not working.

Sample code in functions.php:

function wpf_process_completed( $fields, $entry, $form_id, $form_data ) {
  if ($form_data['id'] === 1033) {
        ?>
            <script>
                window.dataLayer = window.dataLayer || [];
                window.dataLayer.push({ event: 'wpFormsSubmitted' });
            </script>
        <?php
    }
}

add_action('wpforms_process_complete', 'wpf_process_completed', 10, 4);

But nothing happens.

I also tried printing a string and resulted an error.

function wpf_process_completed( $fields, $entry, $form_id, $form_data ) {
  echo 'submitted';
}

Error:
WPForms AJAX submit error:
SyntaxError: Unexpected token s in JSON at position 0

I don't know how to make it work. Please help!

Try:

window.dataLayer.push({ "event": "wpFormsSubmitted" });

Valid JSON string should have double quotes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM