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.
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.
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.
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.
There are two main ways to connect forms on a Next.js site.
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.
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:
If you use the authenticated route, keep credentials on the server. Never expose tokens in client-side code or public environment variables.
This is the step most custom integrations skip. Each Forms API submission can include a context object with:
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.
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:
This gives sales and marketing a clear source trail even when visitors browse across several sessions.
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.
Before launch, run through these checks with a test email address:
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.
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.
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.