Java script in my Pardot form ", "answerCount": 7, "upvoteCount": 1, "datePublished": "2021-11-16T02:57:27.000Z", "author": { "@type": "Person", "name": "Wan Chung", "url": "https://trailblazers.salesforce.com/profileView?u=0053A00000FUcfwQAD", "affiliation": { "@type": "Organization", "name": "--" } }, "suggestedAnswer": [ { "@type": "Answer", "text": "A couple of bugs jump out in that script before you even get to the bigger picture.    In `setCookie`, you're scoping the cookie to `location.hostname.replace(\"www.\", \"\")`. If your Pardot form is hosted on a different domain or subdomain than the page where the GCLID gets captured (which is common with Pardot since forms often live on a separate domain), the cookie you set simply won't be readable when the form loads. Worth checking in dev tools whether the cookie even exists on the form's page.    Second, the read side looks off. `name.split(\",\")[1]` is expecting a comma-separated string, but that's not how you're writing the cookie in the first place, so this is probably just returning undefined every time. I'd console.log `document.cookie` on the form page and confirm what's actually there before touching anything else.    Also double check you're testing with an actual `gclid` in the URL. If you're navigating to the page directly rather than clicking through from a live Google Ads click with auto-tagging on, there won't be one to capture.    If you fix the domain scoping and the parsing, this can absolutely work. But if you'd rather not keep babysitting it as Pardot changes domains or forms move around, tools like Attributer (I'm the founder) do this natively, capturing the GCLID via a first-party cookie that persists across subdomains and writing it into a hidden field on the Pardot form for you.", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000DwWrXSAV", "datePublished": "2026-07-10T02:20:05.000Z", "author": { "@type": "Person", "name": "Aaron Beashel", "url": "https://trailblazers.salesforce.com/profileView?u=005KX0000046buSYAQ", "affiliation": { "@type": "Organization", "name": "--" } } } ] } } Skip to main content

Would anyone know how to pass the GCLID into Pardot and Salesforce?   I have been trying to implement Google Offline Tracking conversion. To do that, I followed the official Google document to configure Salesforce and import conversions into Google Ads, however, I am unable to capture the GCLID in Pardot and Salesforce through Pardot forms. Also, I tried adding the java script mentioned  in this article but can’t seem to make it work. I would appreciate it if someone could shed some light on this. 

 

Java script in my header file

<script type="text/javascript">

function setCookie(name, value, days){

var date = new Date();

date.setTime(date.getTime() + (days*24*60*60*1000));

var expires = "; expires=" + date.toGMTString();

document.cookie = name + "=" + value + expires + ";domain=" + location.hostname.replace("www.", '');

}

function getParam(p){

var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);

return match && decodeURIComponent(match[1].replace(/\+/g, ''));

}

var gclid = getParam('gclid');

if(gclid){

var gclsrc = getParam('gclsrc');

if(!gclsrc || gclsrc.indexOf('aw') !== -1){

setCookie('gclid', gclid, 90);

}

}

</script>

 

 Java script in my Pardot form

<script>

window.onload = function getGclid() {

document.getElementById("GCLID").value = (name = new RegExp('(?:^|;\s*)gclid=([^;]*)').exec(document.cookie)) ? name.split(",")[1] : "&"; }

var putGCLID = function() {

document.getElementById("GCLID").value = (name = new RegExp('(?:^|;\s*)gclid=([^;]*)').exec(document.cookie)) ? name.split(",&")[1] : "&";

};

document.getElementById('pardot-form').addEventListener("submit", putGCLID, true);

</script>

7 answers
  1. Jul 10, 2:20 AM

    A couple of bugs jump out in that script before you even get to the bigger picture. 

     

    In `setCookie`, you're scoping the cookie to `location.hostname.replace("www.", "")`. If your Pardot form is hosted on a different domain or subdomain than the page where the GCLID gets captured (which is common with Pardot since forms often live on a separate domain), the cookie you set simply won't be readable when the form loads. Worth checking in dev tools whether the cookie even exists on the form's page. 

     

    Second, the read side looks off. `name.split(",")[1]` is expecting a comma-separated string, but that's not how you're writing the cookie in the first place, so this is probably just returning undefined every time. I'd console.log `document.cookie` on the form page and confirm what's actually there before touching anything else. 

     

    Also double check you're testing with an actual `gclid` in the URL. If you're navigating to the page directly rather than clicking through from a live Google Ads click with auto-tagging on, there won't be one to capture. 

     

    If you fix the domain scoping and the parsing, this can absolutely work. But if you'd rather not keep babysitting it as Pardot changes domains or forms move around, tools like Attributer (I'm the founder) do this natively, capturing the GCLID via a first-party cookie that persists across subdomains and writing it into a hidden field on the Pardot form for you.

0/9000