This script will allow you to embed a form on any external website and retrieve the UTM Parameters like “utm_source”, “utm_campaign”, etc.. and pass them into hidden fields within the form.
Supported UTM Parameters:
utm_source, utm_medium, utm_campaign, utm_term, utm_content
If you don’t already have an account, please sign up to become a member
<!-- /**************************
* Copyright 2020 GHL Experts, All Rights Reserved
* Do not share, or distribute this code without author's consent.
* This copyright notice must remain in place whenever using
* this code - DO NOT REMOVE
* Author: Anas Uddin
* Website: https://ghlexperts.com
**************************/ -->
<div id="iframe-container"></div>
<script>
let formId = '' // Add form ID here
let formHeight = '400px' // Change form height if required here
// DO NOT CHANGE ANYTHING BELOW
function getURLParameter(e){return decodeURIComponent((RegExp(e+"=(.+?)(&|$)").exec(location.search)||[,null])[1])}
let utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']
let formUrl = 'https://msgsndr.com/widget/form/'
let formJs = 'https://msgsndr.com/js/form_embed.js'
let urlParams = '?'
for (let param of utmParams) {
getURLParameter()
let urlTerm = getURLParameter(param);
if (urlTerm.trim().length > 0 && urlTerm != "null") {
urlParams += `${param}=${decodeURIComponent(urlTerm).replace(/\+/g,' ')}&`;
}
}
let ifrm = document.createElement('iframe');
ifrm.setAttribute('id', formId);
ifrm.setAttribute('src', formUrl + formId + urlParams);
ifrm.setAttribute('scrolling', 'no');
ifrm.setAttribute('style', 'border:none;width:100%;height:' + formHeight);
let s = document.createElement('script');
s.type = 'text/javascript';
s.src = formJs;
var el = document.getElementById('iframe-container');
el.appendChild(ifrm, el);
el.appendChild(s, el);
</script>
