Mautic Know-How
Mautic Know-How
Mautic Tutorials for Beginners & Tips for Specialists

Dynamic Values in Mautic Forms: Date, Time, URL, ...

Do you wish to document the date when a form was submitted? Or are you using the same form in multiple places and need to know in which context it was submitted? Those dynamic, calculated values are not regular Mautic features - here is how to achieve them nonetheless!

The problem (and the general solution)

In a marketeer's life, there are many cases where you are looking for ways to track addtional information when a form is submitted.

Examples are:

  • Store date and time that an Opt-In was given
  • Know on what page (path name) or even domain a form was submitted (e.g. in order to send the right whitepaper from a subsequent campaign)
  • Allow to act on the referrer from which a form was accessed.

The general idea for the solution is always the same: Place some JavaScript in the form to get those values, and let it push the result to a hidden field (which then is stored in the contact).

 

Example 1: Store date and time of Form Submission

Let's look at the "Store date and time" task as an example - here is how to achieve that in detail:

1. Create a custom field (type "Text") in which date & time of the form submission can be persisted.
Note:You may also consider using the "Date/Time" field type, but please adjust the format of the timestamp (below) properly.

2. Create a “Hidden” form field (ideally, give it a name without special characters / spaces) and map it to the custom field above. In our example we'll just call it "myhiddenfield".

3. Create an “HTML Area” form field and let JS get the desired value, plus push it to the hidden form field above.

Specifically, use the following JS:

<script> var formName = <form_id>;
if (typeof MauticFormCallback == 'undefined')
{var MauticFormCallback = {};}
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();

MauticFormCallback[formName] = {onValidateStart: function () {document.getElementById('mauticform_input_<form_id>_<id_of_hidden>').value = date+' '+time; }, };
</script>

where, in the simplest case, <id_of_hidden> is the name of the "Hidden" form field.

<form_id> is a little difficult to find out, though: Go to the form in Mautic, click on "Manual copy" in the green "Form HTML" box, and find the value of data-mautic-form= within that HTML.

 

So this is what the result may look like:

In the Attributes of the HTML field, set style="display: none".

In the end, this combination of steps stores some sort of simple timestamp (format can be improved if you like) in a contact field.

 

Example 2: Store Location of Form Submission

The setup here is completely the same, just the JavaScript is different:

<script> var formName = '<form_id>';
if (typeof MauticFormCallback == 'undefined')
{var MauticFormCallback = {};}
MauticFormCallback[formName] = {onValidateStart: function () {document.getElementById('mauticform_input_<form_id>_<id_of_hidden>').value = document.URL; }, };
</script>

In the end, this combination of steps stores the URL (from which the form was sent) in a contact field, and you can now test against it in your campaigns.

The only "gap" here: If someone submits two forms in quick succession, it might happen that the first one is overwritten before it has been evaluated. If you want to protect against that, too, an idea would be to process the value using a MySQL trigger - but that is outside of Mautic.

 

Other areas with the same need

For forms, the above solution is really powerful - you can access a variety of data by modifying the JavaScript "value" that is used.

For emails, we recommend taling a look at https://github.com/Logicify/mautic-advanced-templates-bundle

Currently unsolved: Dynamically calculated values in Campaign Actions - e.g. for updating a contact field to something like NOW() or VALUEOF(CONTACT(NAME)) . Feature idea is here: https://forum.mautic.org/t/dynamic-calculated-values-in-campaign-actions/13736

Your browser is outdated!

Please update your browser to see this website correct. Update your browser now

This web site uses cookies and the web analysis tool Google Analytics. By continuing to use these pages, you accept that policy.

An opt-out is available inside our Data Privacy Statement.
Agreed