简体   繁体   English

如何在 Wordpress 上使用 wpForms wpforms_process_complete 在 Google Tag Manager 的 dataLayer 上插入数据

[英]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.当用户使用 wpForms 插件成功提交表单时,我想将事件推送到 Google 跟踪代码管理器的 dataLayer。 I tried using the wpForms hook "wpforms_process_complete" and it seems that it's not working.我尝试使用 wpForms 挂钩“wpforms_process_complete”,但它似乎不起作用。

Sample code in functions.php:函数中的示例代码。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有效的 JSON 字符串应该有双引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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