Triggering tasks from a contact form
Last updated
Was this helpful?
Last updated
Was this helpful?
This tutorial walks you through setting up a custom task in Mechanic, which is called on Contact Form submission on your Shopify frontend, the contents of the form are passed to the task, which emails the contents in CSV format.
Before beginning this tutorial, here's what you'll need:
A Shopify store, which has Mechanic installed (see )
A basic knowledge of Liquid ()
A basic knowledge of JavaScript ()
We have an online store called Mario's Mushrooms, hosted on Shopify. Business is booming, and our mushrooms are being shipped all over the world. Our CEO, Mario, asks us to connect our default Shopify contact form to our legacy customer relationship management (or CRM) system. We are eager to help! While the CRM doesn't have an HTTP API, it can receive CSV imports via email, which it will then import into its database. This gives us our path forward!
We are going to make a task in this cool Shopify app called Mechanic. ;) Here's what the task will do:
The task will add some JavaScript to the online Shopify store, which will capture the contents of the contact form when submitted, and then send those contents over to Mechanic via
Over on the Mechanic side, the task will receive the form contents, and format them as a CSV file
The task will then send an to our CRM system, containing the CSV file as an attachment
Time to build the task! Out of Mechanic's entire toolkit, here's what we'll use:
We have options here! The only hard requirement is that we use a POST request to send form data to our webhook. This can be done using pure JavaScript, or using a library like jQuery, or even by using plain HTML to set the form tag's action
attribute to our webhook URL.
First things first: we're going to make sure of the element ID, for our contact form. This will be important for writing JavaScript that addresses this form. After investigating, we discover that the form ID is "ContactForm". Easy enough!
Next, we're going to write some JavaScript that listens for thesubmit
event of this form â functionally, this means that we're going to wire up some code to run when the form is submitted. The goal: to jump in when the form is submitted, send the form data to our webhook (which will then trigger our Mechanic task), and then allow the form to submit as usual. This way, we add Mechanic functionality without disabling the form's existing behavior.
Let's get started on our JavaScript. In your Mechanic task editor, scroll down and find the "JavaScript for Online Storefront" area. This will add this feature to our task, and we'll be given a place to add in our JavaScript, which will be automatically loaded into our shop frontend.
Copy in the JavaScript below, reading the comments for details on what's going on. Remember the "ContactForm" ID? Here's where we get to use it!
When pasting in this code, a new task option will appear, allowing the user (that's us, for now) to configure the webhook URL. Here's where we use the Mechanic-generated webhook URL from earlier.
With all that in place, save the task. We're leaving the task code empty for right now, and that's okay!
To make sure what data we're working with, let's submit the contact form, and then examine the resulting event data in Mechanic. (It's okay that we hit the captcha prompt; the important part is making sure that we're sending data to Mechanic.)
Heading to the "Events" page of the Mechanic app, we can see our data coming in.
Clicking through to that new event, we can see the event data on the right, reflecting what was in the form at the time of submission. (Depending on the nature of your specific contact form HTML, you might see something slightly different.)
This is perfect! The data we are interested in is inside of an event data property called "contact"
. This means that, in Liquid, we can access the contact data using {{ event.data.contact }}
.
Here's how we'll configure the task, using the task option fields that automatically appear based on our task code:
With everything assembled, we head back to the contact form, and make a submission. Back in the task editor, we see a new event appear in "Recent activity", with a green checkmark indicating that the task generated and performed an action.
We did it! We augmented our existing contact form with the ability to send submission data to our new Mechanic task, which relays the data to our CRM system using a CSV email attachment. đ
If you'd like to quickly pull in all of the task code and configuration we used here, use this task export:
Start with the tutorial for this part. Webhooks should be configured with respect to the source that supplies them with data, so for this tutorial, use the webhook name "Contact Form" and the event topic "user/webhook/form".
For this tutorial, we'll use JavaScript. And because we're using Mechanic, we don't even have to edit the theme directly to add in our code â instead, we can use the task editor's feature to have our code automatically loaded into the online storefront. (Under the hood, Mechanic leverages Shopify's API.)
For this tutorial, I created a development store and installed the . I use the contact form that comes with the theme as the form that submits to our webook. You can use any contact form on any theme, or create a form specifically for the purpose of submitting to our webhook.
Moving back to the task editor, the first step is to extract this data, and assemble it into something we can format using the filter. Because that filter is made to handle tables of data, this means that we'll create an array of "rows", and fill it with arrays of "columns", and then pass the result into the csv filter.
After that, we'll add an action, configuring it with our CSV data as an attachment. We'll also add a few more task options that will make it easy to reconfigure this task in the future, without having to touch the task code.
When writing a task, it's important to think about , and how they appear to the user (and to Mechanic itself). This task always sends a simple email for every event it receives, and doesn't require any special permissions, so we don't need to do any preview work here. If the task only sent an email under limited conditions, or if it needed to access the Shopify API, we'd need to do more work to make sure the task generates an intentional preview.
To learn more about this, see .
Thanks for reading! If you've got questions or suggestions, join the . :)