Reputation: 1
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!
Upvotes: 0
Views: 1102
Reputation: 198
Try:
window.dataLayer.push({ "event": "wpFormsSubmitted" });
Valid JSON string should have double quotes
Upvotes: 1