> For the complete documentation index, see [llms.txt](https://docs.spara.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.spara.com/integrations/crm-integrations/setting-up-hubspotutk-cookie-tracking.md).

# Setting Up HubSpotUTK Cookie Tracking

How to set up HubSpot attribution to link contacts captured in Spara to hubspotutk cookie tracking data.

The `hubspotutk` cookie is set by HubSpot's tracking script and stores a unique visitor token used for contact attribution. By calling HubSpot's `identify` method from Spara's JavaScript API `onUserMessageSent()` event, you can associate a visitor's session with their HubSpot contact record.

### Prerequisites

* The **HubSpot tracking script** is installed on your site. [HubSpot's guide to install the tracking code.](https://knowledge.hubspot.com/reports/install-the-hubspot-tracking-code)
* The [**Spara embed script**](https://docs.spara.com/developers/installation/installing-spara-navigator) is installed on your site.

### Add the identify call

Use Spara's `onUserMessageSent()` event (see [Developer: Javascript API](https://docs.spara.com/developers/spara-api/javascript-api)) to extract the visitor's email. Then, call `identify` to merge the visitor to the hubspot cookie data and `trackPageView` to push the identity to HubSpot.

<pre class="language-html"><code class="lang-html"><strong>&#x3C;script type="text/javascript" src="https://app.spara.co/embed-&#x3C;app_id>.js">&#x3C;/script>
</strong>&#x3C;script>
  Spara = {
    onUserMessageSent: function (text) {
      // extract email from user message
      function extractEmail(text) {  
        const match = text.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/);
        return match ? match[0] : null;
      }

      const email = extractEmail(text);

      if (window._hsq &#x26;&#x26; email) {
        window._hsq.push(["identify", { email }]);
        window._hsq.push(["trackPageView"]);
      }
    },
  }
&#x3C;/script>
</code></pre>

**Note:** `identify` sets the identity in HubSpot's tracker memory only. A separate `trackPageView` or `trackEvent` call is required to actually send the data to HubSpot.

### Single-page application (SPA) setup

If your site uses Next.js or React, page navigations don't trigger full reloads, so HubSpot won't automatically track route changes or send identity data.

1. Follow the identify call instructions above.
2. Create a `HubSpotPageView.tsx` component to call `trackPageView` on every route change:

```tsx
'use client';
import { usePathname } from 'next/navigation';
import { useEffect } from 'react';

export default function HubSpotPageView() {
  const pathname = usePathname();

  useEffect(() => {
    if (window._hsq) {
      window._hsq.push(["trackPageView"]);
    }
  }, [pathname]);

  return null;
}
```

3. Add it to your root layout (`app/layout.tsx`):

```tsx
import HubSpotPageView from './HubSpotPageView';

<body>
  <HubSpotPageView />
  {children}
</body>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.spara.com/integrations/crm-integrations/setting-up-hubspotutk-cookie-tracking.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
