How to track traffic source and medium data from Google Analytics to HubSpot forms

I need help setting up a system to capture where visitors come from when they fill out forms on my website. Right now I can see that people are converting but I have no clue if they came from Google search, Facebook, email campaigns, or anywhere else.

What I want to do is grab the source and medium data from each visitor’s session and send it to HubSpot. When someone fills out a contact form or downloads something, I want to know exactly which channel brought them to my site.

Basically I need two custom fields in HubSpot that get updated every time someone visits. One for source and one for medium. Then when they convert, those values get saved permanently so I can run reports later.

Right now all I can see is “50 people downloaded our guide” but I want to know “20 came from Google, 15 from LinkedIn, 10 from our newsletter, and 5 from direct traffic”.

Is there a way to do this with some JavaScript code that connects Google Analytics tracking with HubSpot? I found a couple libraries that might work but I’m not sure how to implement them properly. Any suggestions would be really helpful.

hubspot’s built-in source tracking is ur best bet - it captures most of this automatically. just flip on “track original source” in settings and ur done. won’t be super granular, but beats building something from scratch.

I implemented a similar solution previously. Instead of trying to sync Google Analytics data directly with HubSpot, I recommend utilizing HubSpot’s tracking code, which simplifies the process. Use their built-in UTM parameter capturing alongside some custom JavaScript for traffic sources without UTM tags. A script that captures document.referrer can set hidden fields in your form, recording the source and medium. For organic traffic lacking UTM parameters, analyze the referrer URL to determine whether it’s from Google, Bing, etc. To address the issue of return visitors converting on different sessions, I suggest storing the source data in localStorage with a set expiration. This ensures that the original attribution remains accurate, regardless of subsequent visits. However, be sure to thoroughly test this with various traffic sources, as referrer information may vary based on browser and user privacy settings. Additionally, having fallback values for situations without available referrer data is advisable.

Had this exact problem last year and built a custom solution that’s been working great. You need to intercept GA data before the form submits. Here’s what we did: created a JavaScript function that grabs utm_source, utm_medium, and utm_campaign from the URL when the page loads, then stores everything in session storage. No UTM parameters? We parse the referrer instead. The tricky bit is multi-touch attribution - people visit multiple times before converting. We fixed this by capturing both first-touch and last-touch data in separate HubSpot properties. When someone submits a form, our script auto-fills hidden fields with the attribution data before it hits HubSpot. Took about two weeks to build, but now we can see exactly which channels bring in our best leads. Watch out for iOS Safari’s referrer policy changes though - you’ll lose referrer data from social apps. We added extra logic to handle those cases.