form success custom action with javascript

Hello,

I want to create an event in the dataLayer object (for tracking by Google Tag Manager) when a user sucessfully submitted a bricks form.

Code would be something like:

function my_form_custom_action( $form ) {
$formId = $fields[‘formId’];
?>

<?php } add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );

When I create this with the plugin Codes Snippets, I get the error message:

Snippet konnte nicht erstellt werden. Request failed with status code 403

What do I need to do to make the lines work? Does bricks protect this from being executed?

Solution: use the Code-Element instead, place Javascript Code there right below the form:

document.addEventListener( 'bricks/form/success', function ( event ) {
  // Access the element ID
  const elementId = event.detail.elementId;

  // Access the form data
  const formData = event.detail.formData;

  // Access the raw response from AJAX
  const res = event.detail.res;

  // Do your magic here

  var dataLayer = window.dataLayer || [];
  dataLayer.push({
	  'event': 'bricks_form_success',
	  'form_id': elementId
  })

});

Works like charm.

1 Like