How to Pass and Store a Salesforce Contact ID with Gravity Forms and WordPress

My Salesforce Partner agency is working with an enterprise organization right now to implement Salesforce, Marketing Cloud, Mobile Cloud, and Ad Studio. Their websites are all built on WordPress with Gravity Forms, a fantastic form and data management tool that has a ton of capabilities. As they deploy campaigns via Marketing Cloud in email and Mobile Cloud in SMS, we’re configuring their account and processes to always pass the Salesforce Contact ID to any landing page with a form.

By passing contact data, we can populate each Gravity Forms submission with a hidden field to capture the Salesforce Contact ID so that the client can export the data and import the updated information into their CRM. Later iterations will include an automatic population of the data, but for right now we just want to ensure the data is saved appropriately.

There are a few scenarios that we want to incorporate into this strategy:

https://yoursite.com?contactkey=1234567890

Storing a Salesforce Contact ID in a Cookie in WordPress

To capture and store the Salesforce Contact ID in a Cookie in WordPress, we’ll need to add code to our functions.php page in our active theme. We’re going to overwrite any Salesforce Contact ID that may already in an existing cookie as well, since many companies clean up records, remove duplicates, etc:

function set_SalesforceID_cookie() {
 if (isset($_GET['contactkey'])){
  $parameterSalesforceID = $_GET['contactkey'];
  setcookie('contactkey', $parameterSalesforceID, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);
 }
}
add_action('init','set_SalesforceID_cookie');

Using this hook will set a cookie regardless of whether or not a form exists on the page. We also need to populate any Gravity Forms hidden field using the gform_field_value_{name} method and the cookie if there’s no Salesforce Contact ID is passed in the URL:

add_filter( 'gform_field_value_contactkey', 'populate_contactkey' );
function populate_utm_campaign( $value ) {
 if (!isset($_GET['contactkey'])){
   return $_COOKIE['contactkey'];
 }
}

This is a first-party cookie, as well, which is advantageous to us.

Adding a Salesforce Contact ID Hidden Field in Gravity Forms

Within a Gravity Forms form, you’ll want to add a hidden field:

Then, on your hidden field, you’ll want to set the Advanced option of setting your field to be populated dynamically with your querystring variable contactkey. If this sounds redundant… it is. In the event that a visitor blocks tracking via cookies, we can still populate the hidden field with the querystring variable:

Gravity Forms has a ton of other prepopulation options that you can also programmatically incorporate on their site.

Implementation Upgrades

With this configuration, we’re now storing the Salesforce Contact ID as a cookie and populating any Gravity Forms data with it. Even if the user leaves the site and returns in another session, the cookie is set and will prepopulate the Gravity Forms field.

Exit mobile version