21 Nov, 2026

Connecting a Next.js Website to HubSpot: Forms, Tracking and Lead Source Capture

A visitor finds your site through a LinkedIn ad, reads three pages, and books a demo through a sleek custom form. In HubSpot, the new contact shows up with an "Offline Sources" label, no page views, and no trace of the ad that brought them in. The form worked. The attribution didn't. That's the most common problem with a HubSpot website integration on a Next.js site. Forms are easy to connect, but tracking and lead source data only flow through when three things work together: the HubSpot tracking code, correct page view tracking on client-side navigation, and form submissions that carry the visitor's tracking cookie. This guide explains each piece and how to connect them properly.

Blog
blur blur

Why Next.js Sites Lose Tracking Data

Next.js apps often behave like single-page applications. After the first load, moving between pages happens on the client, without a full page reload.

HubSpot's tracking code records a page view automatically when it first loads. On a traditional site, that happens on every page. On a Next.js site with client-side navigation, it may only happen once. Without extra code, HubSpot sees one page view and misses the rest of the journey.

Custom forms add a second gap. HubSpot connects website activity to a contact through a cookie called hubspotutk. If a custom form submits data without that cookie value, HubSpot can't link the visitor's browsing history to the new contact.

Step 1: Add the Tracking Code the Right Way

Load the HubSpot tracking script once, in your root layout, using Next.js's Script component. That gives you a single, consistent loader across every page.

Then handle client-side navigation. HubSpot's tracking code API includes two functions built for single-page apps: setPath and trackPageView. HubSpot's documentation says to always call setPath before trackPageView, with a path starting with a forward slash.

In the App Router, a small client component can watch route changes and push those two calls. Here's an illustrative example:

 

tsx

"use client";

 

import { usePathname } from "next/navigation";

import { useEffect, useRef } from "react";

 

export function HubSpotPageViews() {

  const pathname = usePathname();

  const isFirstLoad = useRef(true);

 

  useEffect(() => {

    if (isFirstLoad.current) {

      isFirstLoad.current = false;

      return; // HubSpot already counts the first page

    }

 

    const _hsq = ((window as any)._hsq = (window as any)._hsq || []);

    _hsq.push(["setPath", pathname]);

    _hsq.push(["trackPageView"]);

  }, [pathname]);

 

  return null;

}

Test it carefully. Depending on how your app loads the script, the first page view may be recorded twice. Check in HubSpot's page view data on a test contact before going live.

Respect Cookie Consent

If your site uses a consent banner, make sure tracking respects it. HubSpot's tracking code API includes methods for checking privacy consent status, revoking cookie consent, and preventing all tracking. Wire these into your consent tool rather than loading tracking unconditionally.

Step 2: Choose How Forms Connect

There are two main ways to connect forms on a Next.js site.

Option A: Embedded HubSpot Forms

HubSpot's form embed code is the quickest route. Tracking context and submission handling are built in, so less custom code is needed. The trade-off is design control. Embedded forms can be harder to match pixel-for-pixel with a custom design system.

Option B: Custom Forms Through the Forms API

For full control over design and validation, build the form in React and send submissions through HubSpot's Forms API. This is a common HubSpot API integration pattern for Next.js sites.

HubSpot offers two submission endpoints:

  • Unauthenticated endpoint: Supports cross-origin requests from the browser, which keeps setup simple for client-side submissions.
  • Authenticated endpoint: Requires an access token, which suits server-side submissions through a Next.js route handler or server action.

If you use the authenticated route, keep credentials on the server. Never expose tokens in client-side code or public environment variables.

Step 3: Send the Context That Links Visitors to Contacts

This is the step most custom integrations skip. Each Forms API submission can include a context object with:

  • hutk: The value of the visitor's HubSpot cookie, which connects their past activity to the new contact.
  • pageUri and pageName: The page where the form was submitted, useful for reporting on which pages convert.
  • ipAddress: The visitor's IP address, which you can pass when submitting from the server.

HubSpot's developer documentation is clear on why hutk matters: without it, page views and the original source won't populate on the contact. In a server-side setup, read the hubspotutk cookie from the incoming request and pass it through with the submission. If the cookie isn't there, leave hutk out of the submission.

If you have consent options on the form, include them in the submission's legal consent fields so HubSpot records them correctly.

Step 4: Capture Lead Source Beyond HubSpot's Defaults

HubSpot's original source tracking works well when the tracking code and UTM parameters are in place. Many teams also want a more detailed record of how leads arrived. A practical approach:

  1. Capture UTM parameters on landing. Read utm_source, utm_medium, utm_campaign, and similar values from the first URL a visitor lands on.
  2. Store them for the session. Save them in first-party storage, so they survive navigation before the visitor fills out a form.
  3. Pass them as hidden fields. Add matching hidden properties to your HubSpot form, such as first-touch campaign or ad click ID.
  4. Keep first-touch and last-touch separate. Use clearly named properties, so later visits don't overwrite how a lead first arrived.

This gives sales and marketing a clear source trail even when visitors browse across several sessions.

Step 5: Put the Data to Work

Once page views, forms, and source data connect reliably, they become useful across HubSpot. Marketing can segment by campaign. Sales can see what a lead read before booking a call. HubSpot lead scoring can use page views, form submissions, and source properties to flag high-intent visitors.

The order matters. Scoring models built on incomplete tracking data push the wrong leads to the top. Fix capture first, then build scoring on top of it.

blur
Worship

Research & Focusing

Faithful

Plan for Execution

reverent

final analysis & Result

A Quick Testing Checklist

Before launch, run through these checks with a test email address:

  • Page views: Browse several pages, submit a form, then confirm every page view appears on the new contact's timeline.
  • Original source: Arrive with a UTM-tagged link and check that the contact's source matches what you expect.
  • Hidden fields: Confirm UTM and campaign properties populate correctly on the contact record.
  • Consent behavior: Decline cookies and confirm tracking stops, then accept and confirm it resumes.
  • Error handling: Submit invalid data and confirm users see helpful messages rather than silent failures.

When to Bring In Help

A developer comfortable with Next.js can often build this integration using HubSpot's documentation. HubSpot integration services tend to help most when you need server-side submissions with secure credentials, multi-step or gated forms, complex consent rules across regions, or custom events and lead scoring built on top of website data.

It's also fair to say that a fully custom setup isn't always necessary. If design flexibility matters less than speed, embedded HubSpot forms with correctly configured tracking may serve you well. If your team prefers managing content inside HubSpot, HubSpot's own CMS is another option, though a well-built Next.js site connects to HubSpot perfectly well.

Getting the Connection Right

A reliable HubSpot website integration on Next.js comes down to three things: installing the HubSpot tracking code, tracking every client-side page view, and passing the visitor's tracking cookie with form submissions when available. Get these right, and every new contact arrives with the context sales and marketing need.

If you'd like help planning or reviewing your HubSpot website integration, 88gravity's HubSpot and development team can work with your engineers to connect forms, tracking, and source capture cleanly.

blur blur
Blog Blog

Frequently Asked Questions

We strive to provide an exceptional quality of service to clients.

Yes. HubSpot's tracking code can record website activity before visitors submit forms, subject to cookie consent. Once a visitor submits a form with the correct tracking cookie, HubSpot can associate eligible browsing activity with their contact record.

Next.js uses client-side navigation, which may prevent HubSpot from automatically recording subsequent page views. Developers should use HubSpot's setPath and trackPageView functions after route changes to ensure each visited page is included in tracking data.

Businesses can capture UTM parameters during a visitor's first landing session and store them in first-party storage. These values can later populate hidden HubSpot form fields, helping preserve campaign information even when visitors navigate multiple pages before submitting a form.

Yes, provided API access tokens remain on the server. Developers should use authenticated submissions through Next.js route handlers or server actions and avoid exposing credentials in browser code or public environment variables. This protects sensitive integration credentials.

Businesses should test page views, form submissions, UTM capture, original source attribution, and cookie consent behavior. Using a test contact, they can check whether browsing activity and campaign details appear correctly in HubSpot before launching the integration.

blur
Let’s Talk