Submit Drupal user registration form to Hubspot

Submit Drupal user registration form to Hubspot

Today I am going to show you how to create a small module that submits results of Drupal user registration form to Hubspot. For those who doesn't know, HubSpot provides advanced targeting marketing and lead nurturing features. You can read more about it at their website:

We will call our module hubspot_user.module. First of all we need to create admin configuration form where you can enter three things:

  1. Hubspot Portal ID
  2. Hubspot Form GUID
  3. Hubspot Traffic Logging Code

For portal ID and Form GUID we will use textfield while for Traffic Logging Code we use textarea.

 * Implements hook_menu() to get the config page listed
function hubspot_user_menu() {
  $items = array();

  $items['admin/settings/hubspot_user'] = array(
    'title' => 'Hubspot User integration settings',
    'description' => 'Set up hubspot integration and leads insertion.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('hubspot_user_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,

  return $items;

 * @file
 * Provides admin settings page to adjust hubspot API key and JavaScript embedding.

function hubspot_user_admin_settings() {
  $form = array();
  $form['settings'] = array(
    '#title' => t('hubspot_user Settings'),
    '#type' => 'fieldset',

  $form['settings']['hubspot_user_portalid'] = array(
    '#title' => t('Hubspot Portal ID'),
    '#type' => 'textfield',
	'#required' => TRUE,
    '#default_value' => variable_get('hubspot_user_portalid', ''),
    '#description' => t('Enter the hubspot_user Portal ID for this site.  It can be found by <a href="" target="_blank">logging into hubspot_user</a> going to the Dashboard and examining the url. Example: "".  The number after "dashboard-plus" is your Portal ID.'),
  $form['settings']['hubspot_user_formguid'] = array(
    '#title' => t('Hubspot Form GUID'),
    '#type' => 'textfield',
    '#default_value' => variable_get('hubspot_user_formguid', ''),
    '#description' => t('The Form GUID can be found by editing the form, and looking at the URL. In the URL, the form GUID is 78c2891f-ebdd-44c0-bd94-15c012bbbfbf'),
  $form['settings']['hubspot_user_log_code'] = array(
    '#title' => t('Hubspot Traffic Logging Code'),
    '#type' => 'textarea',
    '#default_value' => variable_get('hubspot_user_log_code', ''),
    '#description' => t('To enable hubspot_user traffic logging on your site, paste the External Site Traffic Logging code here.'),

  return system_settings_form($form);

Now if you enable the module you will see new settings page under admin/settings/hubspot_user:

Next thing we want to do is a function that will take form fields and submit it to virtual form in Hubspot. More about submitting a form can be read at Hubspot docs page

This function will take two paramteres:

  1. Hubspot Form URL
  2. Associate array of fields

We will call this function hubspot_user_insert_lead()

function hubspot_user_insert_lead($formURL, $fields) {
  $fields['UserToken'] = isset($_COOKIE['hubspotutk']) ? $_COOKIE['hubspotutk'] : '';
  $fields['IPAddress'] = ip_address();
  $strPost = "";

  // Turn $fields into POST-compatible list of parameters
  foreach ($fields as $fieldName => $fieldValue) {
    $strPost .= urlencode($fieldName) . '=';
    $strPost .= urlencode($fieldValue);
    $strPost .= '&';

  $strPost = rtrim($strPost, '&'); // nuke the final ampersand
  // send POST data
  $r = drupal_http_request($formURL, array('Content-Type' => 'application/x-www-form-urlencoded'),
                           'POST', $strPost);
  return array('Data' => isset($r->data) ? $r->data : '',
               'Error' => isset($r->error) ? $r->error : '',
               'HTTPCode' => $r->code);

Also we need to add Hubspot tracking code to every page and we do that by using drupal_footer() function.

 * Implements hook_footer() to inject the hubspot_user JavaScript tracking code into
 * the page footer, just before </body>.
function hubspot_user_footer($main = 0) {
  return variable_get('hubspot_user_log_code', '');

Now you can start using hubspot_user_insert_lead() in any form. But in this example I will show you how to add it to user registration form to keep tracking of newly registered users

 * Implements hook_user()
function hubspot_user_user($op, &$edit, &$account, $category = NULL){
  if ($op == 'insert') {
      $portal_id = variable_get('hubspot_user_portalid', '');
      $form_id = variable_get('hubspot_user_formguid', '');
      $hubspot_userURL = ''.$portal_id.'/'.$form_id;
      // These fields must be submitted with each request
      $values['firstname'] = $account->name;
      //Or if you use content profile then you can pull values from content profile fields:
      //$values['firstname'] = $account->field_first_name[0]['value'];
      $values['email'] = $account->mail;
      hubspot_user_insert_lead($hubspot_userURL, $values);

And thats it! Now when a new user registers to your site, the results will be sent to your Hubspot virtual form!

If you are interested in using this module I attached both 6.x and 7.x versions.