How to Integrate Segment with Marketo Forms
As with my recent article on adding conditional Chili Piper Routers to HubSpot Forms, I wanted to create this post because I couldn’t find anything quite like it when I first implemented Segment in a prior role (I covered that experience in more depth and color in this post).
The fundamental goal of doing this is twofold:
- Track Marketo form submission events with Segment.
- Identify users/prospects with Segment to create “a single unified customer profile” through identity resolution and append additional data to that profile from the form’s values.
That’s setting aside any potential integrations Segment might also be sending activity to; that’s a can of worms for another day. All this code does is listen for a successful form submission, send that event to Segment, and append the form’s values to the user’s identity. To do that, we’ll be leveraging an analytics.track()
call and an analytics.identify()
call. I’m assuming that Segment’s overall analytics.js()
code is already live on the site as a whole; this code will absolutely fail to run if that is not the case. More details on how Segment’s tracking works can be found in their docs.
Tracking Marketo Form Submissions with Segment
You can either add this code to every page with a Marketo form on your site, or add it to a tag in Google Tag Manager and only trigger it when a Marketo form is found on a given page. I’ve taken the second path in the past, as it means you don’t need to track down every page with a form that might already exist.
Here’s the code:
<script>
MktoForms2.whenReady(function (form) {
// Code inside this function will execute when 'MktoForms2'
// has determined it is fully loaded
form.onSuccess(function(vals,tyurl){
// Code inside this function will execute when a 'MktoForms2'
// form is successfully submitted
analytics.identify(
vals.email, // User ID, using email adress in this case.
{
//Include your list of Marketo Form Values here; any traits you wish
//to append to this user in Segment.
mostRecentFormID : form.getId(),
},
function(){document.location.href = tyURL
} // Hit callback
)
analytics.track(
"Filled out form", // Event Name; customize based on your needs and desired output.
{
//Include any additional Marketo Form Values or page information you with
//to append to this event here.
formID: form.getId(),
}
)
}
)
})
</script>
I believe Sanford Whiteman first shared a version of this code in a Marketo Community Post but I’m struggling to track it down at this time. If I do locate it, I’ll add a link here!
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-04-10Links to this post
This week in links: July 30th Articles and blogs about strategy, operations, and moreSubscribe
Fill out the form to get notified whenever a new post goes live!