Skip to content

Instantly share code, notes, and snippets.

@mtt87
Last active September 7, 2021 10:24
Show Gist options
  • Save mtt87/62740891522022e003ab7adeb3b7c74c to your computer and use it in GitHub Desktop.
Save mtt87/62740891522022e003ab7adeb3b7c74c to your computer and use it in GitHub Desktop.
Hubspot track users after they signup with social login

We use social login (Google, GitHub and Microsoft as Identity Providers) and we wanna understand users that signup where they came from. The flow looks like this

  1. User lands on the website for example from a campaign (utm params in the url)
  2. User accepts the hubspot tracking banner
  3. User clicks on the signup button
  4. User is redirected to our "developer portal" where we trigger a social login with their selected provider. We have the hubspot tracking code running also on our developer portal.
  5. User finishes the signup and lands on our internal console dashboard

Problem

We are pushing a new contact to Hubspot on step 5 but at that point we lose all the information about how the user landed in first place on our website, if it was direct traffic or ads campaign etc because we are creating the contact from our backend calling the Hubspot API.

Solution

We noticed that when someone would send a form from the website with their email, Hubspot would create a contact and automatically capture a lot of information (probably thanks to the tracking script)

We decided to send a "hidden form" via the legacy Forms API endpoint. https://legacydocs.hubspot.com/docs/methods/forms/submit_form

const hutk = Cookies.get("hubspotutk");
await fetch(
  "https://api.hsforms.com/submissions/v3/integration/submit/8403675/7448d6fd-a54d-4969-a6c5-49cda797448d",
  {
    headers: {
      "content-type": "application/json"
    },
    method: "post",
    body: JSON.stringify({
      fields: [
        {
          name: "email",
          value: email
        }
      ],
      context: {
        hutk: hutk,
        pageUri: window.location.href,
        pageName: document.title
      }
    })
  }
);

Once we send this, we can see a new contact in Hubspot and sometimes this has the correct informations about the source, which ad campaign lead them to the website etc, but sometimes this doesn't work and we don't know why.

We send this temporary email anonymous-{uuid}@tru.id param to our backend when the user signup and on step 5 we reconcile by PATCHING the user contact on Hubspot replacing the temporary email with the correct user email.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment