React HubSpot Tracking Code Hook
A custom hook to use the HubSpot Tracking Code API function.
With this custom hook, you can easily use the HubSpot Tracking Code API function.
Features
Next features
Guide
Installation
First step, install the HubSpot tracking code in your React project following the HubSpot documentation about it.
Tip: You can copy and paste the script code directly in the public/index.html
file or use the Google Tag Manager to install as a pixel.
then,
$ yarn add react-hubspot-tracking-code-hook
$ npm install react-hubspot-tracking-code-hook
How to use
This library is using the global function window._hsq
that's initialized from HubSpot tracking code.
Basic
import React from "react";
import { useTrackingCode } from "react-hubspot-tracking-code-hook";
const App = () => {
const { setPathPageView, setIdentity } = useTrackingCode();
setPathPageView("/home");
setIdentity("your-email@provider.com");
setContentType("landing-page");
return <div>My app</div>;
};
API
useTrackingCode
provides the functions from HubSpot tracking code;
Names | Params | Required | Info |
---|
setContentType | contentType: string | x | Additional code to bucket data into HubSpot page types (standard-page, site-page, landing-page, listing-page, blog-post, or knowledge-article) |
setPathPageView | pathName: string | YES | Set the path to the current page will be treated. This function should be used to update the current page whenever a page is loaded. After using this function to update the path, it will call the setTrackPageView function to track the view of the current page. |
setTrackPageView | x | x | This function is called when the tracking code is loaded on a page, but you can manually call this function to track subsequent views in a single page application. |
setIdentity | (email: string, otherProperties: object) | email: YES | This function is used to identify a visitor to your site. The unique identifier is an email address. If there is an existing contact record for that email address, it will be updated. Otherwise, a new contact record will be created. In both cases, the analytics data collected for the visitor will be associated with the contact record. |
setTrackEvent | ({ eventId: string, value: string or number }) | eventId: YES | This function is used to track an event using JavaScript and HubSpot's tracking code. You can use events to track specific activities completed by visitors on your site. Tracked events can show up in contacts' timelines. |
License
react-hubspot-tracking-code-hook
is under MIT License
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!