In order for this instruction to work, we need to save GA Client ID to all new Salesforce leads that Pardot creates.
- First, go to Pardot – Forms.
- Select a form you want to add to GA Connector.
- Click “Edit form”.
- Click “Fields” add a new field – GA_Client_ID field (it need to be synced with GA Client ID field from GA Connector package).
- Open “Look and Feel” and select “Below Form”.
- Click the “Code” symbol ()in the editor. This will allow to paste raw HTML code there.
- Paste this code in the editor:
1 2 3 4 5 6 7 8 9 10 |
<script> function receiveGaClientId(event) { if (event.origin.indexOf('your-domain.com') !== -1 && typeof event.data === 'string') { var cid = event.data; document.querySelector("p.GA_Client_ID input[type=hidden]").value = cid; } } window.addEventListener("message", receiveGaClientId, false); </script> |
This code allows Pardot iframes to receive GA CID cookie value from the parent window.
Keep in mind that this code assumes that your client ID field is called “GA Client ID”, and therefore has a CSS class “GA_Client_ID”. If it’s called differently in your case, you can replace “GA_Client_ID” in the code with the unique CSS class of that field.
Finally, paste this code on your website (on every page that contains Pardot forms):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<script> function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1); if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } function getCID() { var _ga = getCookie('_ga'); var gaObj = window[window.GoogleAnalyticsObject]; if (typeof gaObj !== 'undefined' && typeof gaObj['getAll'] !== 'undefined') { var tracker = gaObj.getAll()[0]; return tracker.get("clientId"); } else if (typeof _ga === 'string' && _ga.length > 0) { var cidArr = _ga.split('.'); return cidArr.slice(cidArr.length - 2, cidArr.length).join('.'); } } function hasClass(element, klass) { return (' ' + element.className + ' ').indexOf(' ' + klass+ ' ') > -1; } function getPardotIframes(callback) { var iframes = document.getElementsByTagName('iframe'); for (var i=0; i<iframes.length; i++) { callback(iframes[i]); try { var innerIframes = iframes[i].contentDocument.getElementsByTagName('iframe'); for (var j=0; j<innerIframes.length; j++) { callback(innerIframes[j]); } } catch (e) {} } } function sendCidToIframe() { try { var cid = getCID(); if (typeof cid === 'undefined') return; getPardotIframes(function(pardotIframe) { pardotIframe.contentWindow.postMessage(cid, "*"); }); } catch (error) { console.log("Pardot CID script error:", error); } } setInterval(sendCidToIframe, 1000); </script> |
If your Pardot forms isn’t hosted on go.pardot.com, you also need to add a class or an ID attribute “pardotform” to your Pardot iframe:
1 |
<iframe id="pardotform" ...> |