HubSpot Destination


HubSpot is an inbound marketing and sales platform that helps companies attract visitors, convert leads, and close customers. The analytics.js HubSpot Destination is open-source. You can browse the code on GitHub.

Getting Started

Before you start, make sure HubSpot supports the source type and connection mode you’ve chosen to implement. You can learn more about connection modes here.

Web Mobile Server
📱 Device-mode
☁️ Cloud-mode
  1. From the Segment web app, click Catalog.
  2. Search for “HubSpot” in the Catalog, select it, and choose which of your sources to connect the destination to.
  3. If you haven’t already done so, add your API Key from HubSpot and enter it in the “API Key” field in the Segment web app.
  4. Navigate to the “Settings” page in the HubSpot UI to find your Hub ID and enter it to the “Hub ID” field in the Segment web app.

When you activate the destination from the Segment web app, your changes appear in the Segment CDN in about 45 minutes, and then Analytics.js starts asynchronously loading the HubSpot snippet and recording data.

Page

If you’re not familiar with the Segment Specs, take a look to understand what the Page method does. An example call would look like:

analytics.page()

IMPORTANT: An initial page call is required for data to be sent to HubSpot using Analytics.js. This is included by default in your Segment snippet.

Identify

If you’re not familiar with the Segment Specs, take a look to understand what the Identify method does. An example call would look like:

analytics.identify('user1234', {
  email: 'petergibbon@email.com',
  firstname: 'Peter',
  lastname: 'Gibbon'
})

IMPORTANT: HubSpot’s device-mode integration has two conditions for identify to successfully create or update a contact. A traits.email value must be included and either a page or track call must be called. You can read more from HubSpot’s documentation here. If you are using HubSpot’s cloud-mode integration, an identify call will update records without a page or track call being needed.

HubSpot does not accept any trait keys that contain uppercases or spaces. So for any custom traits you send Segment converts the trait to lower case, and replaces any spaces with an underscore.

Any traits that aren’t contact fields in HubSpot will be removed from the request. To find out which fields you can set, check out the custom field names in Contacts > Contact Settings. Example field names are “firstname”, “lastname”, “company”, “phone”, etc.

If you specify a company name (using traits.company.name), it appears as a property of the contact (you can find it in HubSpot’s UI using About [contact] > View > View All Properties), but it does not appear as the user’s company under [contact]’s Company.

The following traits are tagged as special fields in HubSpot:

  • address
  • city
  • companyName
  • email
  • firstName
  • lastName
  • position
  • phone
  • zip

Track

If you’re not familiar with the Segment Specs, take a look to understand what the Track method does. An example call would look like:

analytics.track("Clicked Buy Now button", {
  value: 20.5
})

IMPORTANT: You must have a HubSpot Enterprise account for traits from an identify call to be passed through to your track call and be successfully sent as a custom event to HubSpot.

The event will appear in your HubSpot UI but may take up to 60 minutes to appear in the graph visualization.

Server

When calling from any of our server-side sources you must provide the contact’s email as properties.email so that HubSpot can tie the event to the appropriate contact. An example call in Python would look like:

analytics.track(
  user_id='YOUR_USER_ID',
  event='Bought Item',
  properties={
    'email' : 'peter@example.com',
  }
)

In this case, your HubSpot eventId is ‘Bought Item’. If you want to use an existing eventId, you can use it instead of the event value (ie. Bought Item). If you don’t want to match an existing eventId, you can use any event label and HubSpot will auto-create the event for you.

Setting Contact Properties on Track

Although we recommend you send traits using identify, you can also set HubSpot properties on a track call, as allowed by their events API. You might want to use this method if you’re running out of API calls on the Identify requests.

Include HubSpot contact properties into the context.traits object:

analytics.track(
  user_id='YOUR_USER_ID',
  event='Bought Item',
  properties={
    'email': 'peter@example.com',
  },
  context={
    'traits': {
    'firstname': 'Peter',
    'lastname': 'Gibbons'
  }
})

Group

NOTE: Group calls are not compatible with our Analytics.js library.

If you’re not familiar with the Segment Specs, take a look to understand what the Group method does. An example call would look like:

analytics.group({
  groupId: "some_group_id",
  userId: "some_user_id",
  traits: {
    website: "https://www.example.com",
    name: "Example Inc."
  }
});

Group calls map to the HubSpot Companies API. Segment’s integration is responsible for creating and updating company objects with whatever traits you set, as well as associating individual contacts to a company.

IMPORTANT: There are three requirements to creating companies and associating contacts:

  1. Group calls only take effect when called using server-side libraries or mobile libraries, not using our client-side javascript library.
  2. Your contact must have been identified and created within HubSpot (called using analytics.identify for this userId first).
  3. You must include a website trait in your group call, and it must be a full, valid, and complete URL. HubSpot uses the domain of the website trait as a unique identifier for companies. To create a new company you must use the full URL and not just the subdomain.

Segment can send the following group traits to HubSpot as special properties:

  • address
  • city
  • state
  • zip
  • country
  • description
  • employees
  • industry
  • phone
  • website

Troubleshooting

API Call Limits

HubSpot limits the total amount of hourly and daily requests we can make to their API on your behalf. Read more about these limits here.

HubSpot Plan: Free & Starter

  • Maximum Number of API Calls per 10 Seconds, per Key or Token: 100
  • Maximum Number of API Calls per Day, per Key or Token: 250,000

HubSpot Plan: Professional and Enterprise

  • Maximum Number of API Calls per 10 Seconds, per Key or Token: 100
  • Maximum Number of API Calls per Day, per Key or Token: 500,000

HubSpot Plan: API Add-On (Any Tier)

  • Maximum Number of API Calls per 10 Seconds, per Key or Token: 120
  • Maximum Number of API Calls per Day, per Key or Token: 1,000,000

Sending Dates as Property Values

HubSpot’s API has specific requirements regarding how dates should be formatted before they are delivered as contact properties with date types.

In order to ensure proper transformation of these properties, pass them to Segment as ISO-8601 formatted strings and not as UNIX timestamps. Here’s a Javascript example:

analytics.identify('userid', {
    offerDate: new Date() // not Date.now()!
});

Invalid ‘lifecyclestage’

When using any of our server-side sources, our connector will infer traits.lifecycle_stage as lifecyclestage. If you’re using a custom contact property for custom lifecycle stage’s, you should give the property a distinct name, such as custom_lifecycle_stage.

Loading Forms SDK

Segment gives you the option to load the HubSpot Forms SDK alongside their tracking library. This can be done by enabling the Load Forms SDK setting when configuring your HubSpot integration.

Note: The Forms SDK expects to be loaded synchronously but analytics.js is loaded asynchronously. Therefore, in order to interact with the API, you need to run your code inside an analytics.ready callback. Here’s an example:

analytics.ready(function(){
  hbspt.forms.create({
    portalId: '',
    formId: '',
    css: '',
    cssRequired: ''
  });
})

Using HubSpot with Personas

You can send computed traits and audiences that you create in Personas to HubSpot so you can use the data in live chat, automated emails, and other HubSpot features to personalize interactions with your customers.

You can set a “lookback window” for both computed traits and audiences, which limits the period of time in which data is considered when calculating the trait or audience. For example, you might set a lookback window of 7 days on an audience or trait like new_users_7_days, but you would not add a lookback window to a trait that isn’t time-bounded, for example lifetime_value .

When you specify a lookback window, Personas updates the audience or trait hourly. If you do not specify a lookback window, Personas continuously updates both computed traits and audiences in real time.

Syncing Custom Traits to HubSpot

HubSpot requires that you create and define any custom traits in the HubSpot UI before you send any Personas data to HubSpot. If you try to sync a property from Segment that does not exist in HubSpot, the sync fails.

How it works: User-Level Traits and Audiences

Personas sends User-Level data to HubSpot to update properties on a user (or contacts in HubSpot) record, using an Identify call to add a standard Property, and using a Track call when a trait is computed or an audience is entered or exited.

  • Computed Traits: When the trait is first computed, Personas sends an Identify call to update the records of all users who are included in the computed trait. Each time the trait is computed after that, Personas sends a Track call to HubSpot to update the values. For example, if a computed trait counts the number of times a user visits your pricing page, and the user visits your pricing page five times, Segment first sends an Identify call with the property pricing_page_visits: 5, then sends a Track call when this number updates. This appears in HubSpot as a Property for the contact.

  • Audiences: Personas uses an Identify call to add the name of the audience to the user’s profile as a trait, and includes a boolean value that indicates if the user is a member of the audience. For example, when a user first completes an order in the last 30 days, Personas sends an Identify call with the property order_completed_last_30days: true. When the user no longer satisfies these criteria (for example when it’s been longer than 30 days since the last purchase), Personas uses a Track call to set that value to false. This appears as a Property for the contact in HubSpot.

    • When you first create an audience, Personas sends an Identify call for every user in the audience. Later syncs only update users which were added or removed from the audience since the last sync.

How it works: Account-Level Traits and Audiences

Personas sends Account-Level data to HubSpot using Identify calls to add account traits to the users’ profiles, and Track calls to update the trait when it recomputes or when the group enters or exits an audience. Users are added to an account using a single Group call, which appends a groupID to each user within the account.

  • Computed Traits: When you build computed traits with Account-Level data, Personas computes for each account based on traits or aggregated user behavior. You can then export traits for each account, or for each user within an account. Personas adds a new trait (set as the name of the computed trait) to the user profiles for each user in the group, and sets the value of that computed trait.

    For example: Imagine you have a computed trait that counts the number of times that users from a specific company visit your pricing page. If five different users visit your pricing page once each, Personas sends an Identify call with the property pricing_page_visits: 5 to each user in the group.

  • Audiences: When you build audiences with Account-Level data, Personas returns a set of accounts, or a set of users that match your criteria. Personas adds the name of the audience to the user’s profile as a trait (both for individual users, and users within an account), and sets a boolean value to indicate if the user is in the audience. For example, if users in an account first complete an order in the last 30 days, Personas sends an Identify call with the property order_completed_last_30days: true. When the users in this Account no longer satisfy these criteria (for example if it’s been more than 30 days since the last order), Personas sets that value to false.

    • When you first create the audience, Personas sends an Identify call for every user in the account in that audience. Later syncs only send updates for individual accounts and users which were added or removed since the last sync. Note: For user-level events or traits, you can specify None of the users, Any users, or All users when you build your audience criteria.

Creating Personas Audiences in HubSpot

  1. Create your audience criteria and preview the audience in Segment. Click Select Destinations.

  2. Next, select HubSpot as a destination for the audience in Segment. Use the default settings, which send an Identify call to mark users as members of an audience or when they have a specific trait.

  3. Enter a name for the audience, and a description. Write down the Audience key (you’ll need this to configure HubSpot in the next step), but don’t click Create Audience yet.

  4. Go to your HubSpot Settings.
  5. Create a new contact property in HubSpot for each audience you want HubSpot to receive from Personas. This is required because HubSpot’s schema is explicitly defined. You must do this before you send any Personas data from Segment to HubSpot.

  6. Set the object type.
    • For Personas Audiences that are only individual users, set the Object Type as Contact.
    • For Audiences that use Groups, set the Object type to Contact information.
  7. Enter the label for the custom property, and make sure it matches the Audience Key you wrote down from the Personas audience builder (see the tip below). Click Next.

    Tip: The audience label’s “internal name” in HubSpot must exactly match the Segment audience key. You can check this by clicking the </> icon to the right of the Label field, and making corrections.

  8. On the next screen, set the Field type for audiences to Single Checkbox. (This represents a boolean value that indicates audience membership.) (For computed traits, depending on whether the output is a string or number, select Single-line text or Number.)

    Click Create to finish adding the audience contact property.

  9. Back in the Personas Audience builder, click Create Audience. Personas sends any users that meet the audience criteria to HubSpot immediately.

Verify the audience

You can use the debugger in your Personas space to see the calls sent to HubSpot. Edit the URL below to replace YOUR_WORKSPACE and YOUR_PERSONAS_SPACE with the workspace and personas space you’re working in.

https://app.segment.com/YOUR_WORKSPACE/sources/YOUR_PERSONAS_SPACE/debugger

You can check back in HubSpot to see the audience boolean as a contact property. For the audience created in the example above, you could check individual contact profiles and see a contact property called Email Opened 30 Days = Yes.

You can also see this in the contact property history for each user record.

HubSpot Personas Quick Info

  • Personas Destination type: Event Method (data is delivered to this Destination one-by-one on a realtime basis)
  • Traits and Audiences created by: Identify calls add traits and audiences as traits on the user, Track calls update the values.
  • Must create audience_name field before Personas can update those values?: Yes, you must manually create Contact properties in Hubspot before you send Custom Traits or Audiences.
  • Audience appears as: A prose-text version of the audience name (for example, Order Completed 30 Days: Yes) where Yes indicates that the user is in the audience.
  • Destination rate limit: Yes. HubSpot’s rate limit depends on what account tier you have in HubSpot, but is usually 100 calls per ten seconds, or 10 per second.
  • Lookback window allowed: Unlimited
  • Identifiers required : UserID or email
  • Identifiers accepted : UserID and email
  • Client or Server-Side Connection: Server-side

Settings

Segment lets you change these destination settings from the Segment app without having to touch any code.

API Key

You can request your API Key from HubSpot. Please follow the help center article. It should look something like this: dfdbfe6f-e7bf-4938-8e82-7d1938e48ab8

Hub ID

You can find your Hub ID on the Settings page of your HubSpot account. It should be a series of numbers, like 997086.

Load Forms SDK

Select this option if you would like Segment to automatically load the Hubspot Forms SDK onto your site. This will offload the manual work required to add the embed code to your site’s header.

Important: The Forms API expects that the script is loaded onto your page synchronously but analytics.js is loaded asynchronously. To prevent errors, please ensure you are interacting with the Forms API inside an analytics.ready callback.

This page was last modified: 20 Oct 2020

Get started with Segment

Segment is the easiest way to integrate your websites & mobile apps data to over 300 analytics and growth tools.
or
Create free account