Conditional Chili Piper Router based on HubSpot Form values
I’m creating this post in large part because I really could have used one like it a few months back. I’d done some custom work to get Chili Piper and Marketo forms playing nicely with each other in the past and figured I could apply that experience to HubSpot forms without much trouble.1
I was completely wrong. HubSpot forms are a totally different beast.2
Every solution I found online or in HubSpot’s documentation leaned heavily on jQuery for everything involving form values, as well as for any attempt to augment form behavior with onSubmitted or onSubmit handlers. What few snippets of information I found that didn’t leverage jQuery used arrow functions throughout… which Google Tag Manager won’t let you use in a tag. Trying to add a function with arrow functions lands you a "ECMASCRIPT6 Google Tag Manager Error: Arrow function" message, as covered in more depth in this Stack Overflow post.
Chili Piper Custom Submit Code
So here’s where I landed:
<script src="https://js.chilipiper.com/marketing.js" type="text/javascript"></script>
<script>
var cpTenantDomain = "CPDomain"; // replace with your subdomain
var cpRouterName = "CPRouter"; // replace with the router's name
window.addEventListener("message", function (event) {
if (event.data.type === "hsFormCallback"
&& event.data.eventName === "onFormSubmit"
&& event.origin === document.location.origin) {
var lead = {};
for (var key in event.data.data) {
lead[event.data.data[key].name] = event.data.data[key].value;
}
if(Object.keys(lead).length <= 1){lead = event.data.data;}
if(lead.field_name == "Value") {
ChiliPiper.submit(cpTenantDomain, cpRouterName, {
map: true,
lead: lead
});
}
}
});
</script>
This code is deployed through Google Tag Manager across all pages and uses an event listener to grab HubSpot’s global form events. When we see an onFormSubmit event, we’re grabbing the data (all fields and their values) and using if-then logic to only trigger Chili Piper when we see a certain value.
You can do something fairly similar within Chili Piper itself, but the advantage of this approach is that you can easily add additional behavior to this script — GA goal conversions, Ad Conversion Tracking, etc. It’s a baseline for ‘reacting to a HubSpot form submission’ that you can leverage for all kinds of tasks.
There’s likely neater, tidier ways to go about doing this but given my own challenges tracking down good info, I figured I’d share. Sanford Whiteman has a great post on his blog about some of the security risks that come with HubSpot form events that you should definitely check out if you want to go down this path.
The Chili Piper link included here is an affiliate link; if you request a demo from Chili Piper and it results in a deal, I get a small portion of that revenue.↩︎
HubSpot forms really make me miss the Marketo Javascript API. Literally like night and day.↩︎
Written by Jack Segal. Shoot me an email with questions or comments, and if you'd like to support my writing, you can send me a tip using Stripe!
Date
2023-03-17Subscribe
Fill out the form to get notified whenever a new post goes live!