HubSpot plugin for 'analytics' module
npm install @analytics/hubspotanalyticsIntegration with HubSpot for analytics
This analytics plugin will load HubSpot's client side tracking script into your application and send custom events, page views, and identify visitors inside HubSpot.
Click to expand
- Installation
- How to use
- Platforms Supported
- Browser usage
* Browser API
* Configuration options for browser
- Additional examples
- Using identify
Install analytics and @analytics/hubspot packages
``bash`
npm install analytics
npm install @analytics/hubspot
The @analytics/hubspot package works in the browser. To use, install the package, include in your project and initialize the plugin with analytics.
Below is an example of how to use the browser plugin.
`js
import Analytics from 'analytics'
import hubSpotPlugin from '@analytics/hubspot'
const analytics = Analytics({
app: 'awesome-app',
plugins: [
hubSpotPlugin({
portalId: '234576'
})
]
})
/ Track a page view /
analytics.page()
/ Track a custom event /
analytics.track('cartCheckout', {
item: 'pink socks',
price: 20
})
/ Identify a visitor /
analytics.identify({
name: 'bob',
email: 'bob@bob.com' // email is required
})
`
After initializing analytics with the hubSpotPlugin plugin, data will be sent into HubSpot whenever analytics.identify, analytics.page, or analytics.track are called.
See additional implementation examples for more details on using in your project.
The @analytics/hubspot package works in the browser
The HubSpot client side browser plugin works with these analytic api methods:
- analytics.identify - Identify visitors and send details to HubSpot
- analytics.page - Sends page views into HubSpot
- analytics.track - Track custom events and send to HubSpot
- analytics.reset - Reset browser storage cookies & localstorage for HubSpot values
`js
import Analytics from 'analytics'
import hubSpotPlugin from '@analytics/hubspot'
const analytics = Analytics({
app: 'awesome-app',
plugins: [
hubSpotPlugin({
portalId: '234576'
})
]
})
`
| Option | description |
|:---------------------------|:-----------|
| portalId customScriptSrc
required - string| The HubSpot Portal (or Hub) Id of your HubSpot account |
| flushOnIdentify
_optional_ - string| Load hubspot script from custom source |
|
_optional_ - boolean| Fire immediate page view to send identify information |
Below are additional implementation examples.
Using in HTML
Below is an example of importing via the unpkg CDN. Please note this will pull in the latest version of the package.
`html
....
`
Using in HTML via ES Modules
Using @analytics/hubspot in ESM modules.
`html
....
`
Important: HubSpot requires an email field for making identify calls.
If your identify call does not contain email HubSpot will not be notified of the new user.
When sending properties with identify calls, all camelCase traits are automatically converted to snake_case. There is one exception to this for firstName & lastName which are sent as firstname & lastname.
Example:
`jsaccount_level
analytics.identify('user-xzy-123', {
email: 'bill@murray.com',
accountLevel: 'pro' // trait will be ``
})