This guide demonstrates a method to track Pardot frame form submissions using GTM and record them in all required publishers.
Track form submissions of Pardot forms using GTM.
A common issue many GTM webmasters encounter is the use of iframes on websites. You may ask “What is an iframe?” Well… an iframe (Inline Frame) is an additional HTML document that is embedded inside another HTML document on a website. The iframe HTML element is often used to insert content from another source, such as an advertisement or in this case a Pardot form. As mentioned, this is typically from another source and this will reduce the visibility GTM can have over the events within. In other words, you are more-or-less blind to these events unless you can influence both pieces of the puzzle. The first stage of this is to send a message out of the Pardot iframe to the parent window (the webpage). Conveniently, Pardot has a functionality which allows just this; the thank you code.

From here, we can insert some simple Javascript to “post” a message to our window where GTM has been implemented. You should leave the redirect checkbox unchecked for the code to execute. See an example of this code is below:
< script type = "text/javascript" >try {var postObject = JSON.stringify({ //This JSON can be altered to contain dynamic data or static data depending on requirements.type: 'Free Trial',action: 'FormSub'});parent.postMessage(postObject, 'YOUR DOMAIN')}catch (e) {parent.console && parent.console.log(e);}top.location.href = "SUCCESS PAGE URL";//*The thank you page logic can be altered to open a pop up with more information being found here.
The code above will push the message with that treasured payload on a successful form submission followed by a JS redirect to your chosen thank you page. Below is an example of the post message in action:

This is now stage one complete - but there are still a few steps to go! The next stage is to make use of this message, and the best way to do this is by utilising an event listener. This can be implemented directly onto the page or via GTM. The code below demonstrates how you can implement an event listener and trigger a data layer custom event for you to then utilise.
< script type = "text/javascript" >var ValidationVariable = 10; //These variables are used to help debug the implementation if there are any issues.var edataInside = [];var eActions = [];var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";var eventer = window[eventMethod];var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";eventer(messageEvent, function (e) {parsedData = JSON.parse(e.data)edataInside.push(e);eActions.push(parsedData.action)if (parsedData.action == "FormSub") { // this step validates the data passed from the postMessage.window.dataLayer.push({'event': parsedData.action,'type': parsedData.type})}}, true);
This script should be implemented on all pages with a form and should be triggered on the GTM pageview. With this tag and the postMessage in place, you will start seeing a custom event being triggered within the GTM preview block:

The final stage is to use this event for all your tracking needs by creating the relevant DLVs (Data Layer Variables) and trigger. Starting with the trigger, you use the name specified in your thank you code JSON (In this case “Form Sub”):

This can then be used as a trigger for any tag type within GTM to send data where required. Below is an example of how this can be used to record GA events where the type is stored as the action:

Thanks so much for your great overview! I’m using this to hopefully pass the correct referrer through to Google Analytics. Otherwise the attribution makes all our conversions look like they are coming from go.pardot.com. Is this a valid use case for what you put together here and if so, is there a need to configure some kind of dynamic variable to pass the referrer? Right now I’m getting an event to pass to Google Analytics, but it’s not connected to the conversion and the referrer still shows as go.pardot.
I appreciate any thoughts or ideas around this. Thanks in advance!
Hi David,
Hope you are well.
Sounds like an interesting use of the logic!
I would have to review the setup specifically to give some more formal advice but have you tried the referral exclusion list? This sounds like something it could resolve but if not it’s good to check all avenues.
If that doesn’t work, feel free to reach out to my email: [email protected]
Kind regards,
Dom