We’re trying to capture a Google Ads Click ID (GCLID) upon form submission - this will allow us to stitch together post-conversion performance to the original AdWords click.
I am using the following code in tag manager for the page in question. You would need to change your form id obviously,embed the form in RAW HTML and make sure the field is hidden. Then I have a zapier integration to connect the offline connection and form submission.
Hope it helps!
<script>
function getParam(p, url) { var match = RegExp('[?&]' + p + '=([^&]*)').exec(url); return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); }
function setCookie(name, value, days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; }
function getCookie(name) { var nameEQ = name + "="; 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, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; }
function addGclidToForm() { var hubspotForm = document.getElementById('hsForm_22c4644f-8001-4f91-a34a-3b16a8e2c551');
if (hubspotForm) { var gclid = getCookie('gclid');
if (gclid) { var gclidInput = document.createElement('input'); gclidInput.type = 'hidden'; gclidInput.name = 'gclid'; gclidInput.value = gclid;
I figured out how to store and capture the GCLID using the code below in Tagmanager for contacts in Hubspot. However, I still need code to do the same thing for the MSCLKID (Microsoft Click ID for Microsoft Ads). If you know how to fix this problem, please paste the code for tagmanager in the reply.
If you need help with storing the GCLID, paste the code below into tagmanager using all pages as the trigger.
<script>
function getParam(p) { var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search); return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); }
function getExpiryRecord(value) { var expiryPeriod = 90 * 24 * 60 * 60 * 1000; // 90 day expiry in milliseconds
var expiryDate = new Date().getTime() + expiryPeriod; return { value: value, expiryDate: expiryDate }; }
function addGclid() { var gclidParam = getParam('gclid'); var gclidFormFields = ['gclid_field', 'foobar']; // all possible gclid form field ids here var gclidRecord = null; var currGclidFormField;
var gclsrcParam = getParam('gclsrc'); var isGclsrcValid = !gclsrcParam || gclsrcParam.indexOf('aw') !== -1;
Thank you for the code. It works well on GTM. I am facing a simple issue, usually the worst. The hubspot forms we use are standalone, not embedded in the page. I understand it will not be able to capture the information from the cookie, is this correct?
We need to pre-populate the form with another hidden field based on the link click on the website, and this seems quite complex. The pre-population requires no-design raw html forms, which design-wise is ugly. Do you have any insights on this?
Hi @zackphilipps, I'm trying to record offline conversions as well and have a few questions about the stuff in the article you wrote. So basically the steps to record GCLID and send that data to hubspot are: - create a tag in tag manager that triggers on all pages with the code that you posted in the article - Replace the "name", "value", "days" with "gclid", "[whatever value we want to set]", "[x days]"
- The part with assigntrackingparametertocookie I do not understand at all. Could you elaborate please.
- create a hidden field in my hubspot form that captures gclid.
Is this somewhat correct? As I am not a developer some of the things are a bit harder for me to understand.
Just tested this out. For some reason the Gravity Form was not being hidden, despite following the settings in your gif. Anyway, I think I was able to figure it out based on your post, after a lot of troubleshooting and learning about Google Tag Manager. Thanks!
@ChrisJustin Setting the field won’t hide the entire form, just that field. If that setting wasn’t hiding your field, could be an issue with your theme.