HubSpot CRM Integration with Website
HubSpot is a popular CRM for small and medium business with convenient API and free tier. Integration allows automatic lead addition from website forms, contact synchronization, user activity tracking, and launching automatic email sequences.
Integration Methods
HubSpot Forms API — send form data directly to HubSpot. Simplest method for lead collection.
HubSpot CRM API v3 — full control via REST API. Create and update contacts, deals, tickets.
HubSpot JS Tracking Code — script for tracking site behavior. Links visitor actions (page views, clicks) to contact in CRM.
Sending Lead via Forms API
// Get portal_id and form_id from HubSpot form URL
const PORTAL_ID = '12345678';
const FORM_ID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
const response = await fetch(
`https://api.hsforms.com/submissions/v3/integration/submit/${PORTAL_ID}/${FORM_ID}`,
{
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
fields: [
{name: 'firstname', value: formData.firstName},
{name: 'lastname', value: formData.lastName},
{name: 'email', value: formData.email},
{name: 'phone', value: formData.phone},
{name: 'message', value: formData.message}
],
context: {
hutk: getCookie('hubspotutk'), // cookie to link with visit
pageUri: window.location.href,
pageName: document.title
}
})
}
);
Field hutk links lead with site visit history — required for correct attribution.
CRM API: Creating Contact and Deal
// composer require hubspot/hubspot-php
$client = \HubSpot\Factory::createWithAccessToken(env('HUBSPOT_ACCESS_TOKEN'));
// Create or update contact (upsert by email)
$contact = $client->crm()->contacts()->basicApi()->create(
new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput([
'properties' => [
'email' => $user->email,
'firstname' => $user->first_name,
'phone' => $user->phone,
'website_user_id' => $user->id
]
])
);
// Create deal
$deal = $client->crm()->deals()->basicApi()->create(
new \HubSpot\Client\Crm\Deals\Model\SimplePublicObjectInput([
'properties' => [
'dealname' => "Order #{$order->id}",
'amount' => $order->total / 100,
'dealstage' => 'closedwon',
'closedate' => $order->created_at->timestamp * 1000 // ms
]
])
);
// Link deal to contact
$client->crm()->deals()->associationsApi()->create(
$deal->getId(), 'contact', $contact->getId(), 'deal_to_contact'
);
Webhook for Synchronization
HubSpot supports webhooks: when contact or deal changes, sends notification to website endpoint. Used for status synchronization back to site (e.g., manager closed deal in HubSpot → status on site updates).
HubSpot Lists and Segmentation
Website users can be automatically added to HubSpot lists for segmented email campaigns:
$client->crm()->lists()->membershipsApi()->addAndRemoveMemberships(
$listId,
new AddAndRemoveMembershipsInput(['recordIdsToAdd' => [$contactId]])
);
Event Tracking
For advanced tracking — Custom Behavioral Events via API:
$client->events()->send()->basicApi()->sendEvent([
'eventName' => 'pe123456_product_viewed',
'email' => $user->email,
'properties' => ['product_id' => $productId, 'product_name' => $productName]
]);
Integration time: 3–5 days for basic lead and order synchronization. With bidirectional synchronization and event tracking — 1.5–2 weeks.







