Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/hooks/useNewsletter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import version from '../version/version'
import getCookie from '../utils/getCookie'

const PORTAL_ID = import.meta.env.VITE_HUBSPOT_PORTAL_ID
const FORM_GUID = import.meta.env.VITE_HUBSPOT_FORM_GUID
Expand All @@ -7,6 +8,22 @@ const PAGE_NAME = import.meta.env.DEV
? `Mini-dashboard (dev)`
: `Mini-dashboard v${version}`

function getContext({ pageName }) {
const context = {
pageName,
pageUri: typeof window !== 'undefined' ? window.location.href : undefined,
}

// The `hubspotutk` cookie is set by HubSpot's tracking code and ties this
// submission to the visitor's tracked activity and the right contact record.
// Only include `hutk` when the cookie actually exists — HubSpot rejects an
// empty value with INVALID_HUTK, so a cookieless submission must omit it.
const hutk = getCookie('hubspotutk')
if (hutk) context.hutk = hutk

return context
}

function getBody({ email, pageName }) {
return {
fields: [
Expand All @@ -16,9 +33,7 @@ function getBody({ email, pageName }) {
value: email,
},
],
context: {
pageName,
},
context: getContext({ pageName }),
legalConsentOptions: {
consent: {
consentToProcess: true,
Expand Down
9 changes: 9 additions & 0 deletions src/utils/getCookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const getCookie = (name) => {
if (typeof document === 'undefined') return undefined
const value = `; ${document.cookie}`
const parts = value.split(`; ${name}=`)
if (parts.length === 2) return parts.pop().split(';').shift()
return undefined
}

export default getCookie
Loading