HubSpot Forms Integration Guide (Legacy Forms)

The instructions below apply to Cookie-Based integration.

Not sure which instructions to follow? Start here: How to Check Which GA Connector Integration Type You’re Using.

Add Google Analytics data to Hubspot forms with GA Connector’s Hubspot integration.

If you're using HubSpot forms built with the newest Visual editor, please click here for step-by-step instructions .

Prerequisites

  • Installed either GA Connector WordPress plugin or GA Connector script on your website
HTML
<script data-cfasync="false" type="text/javascript" src="https://tracker.gaconnector.com/gaconnector.js"></script>
<script>
function setGaconnectorHiddenFields() {
    var gaFields = gaconnector.getCookieValues();
    for (var fieldName in gaFields) {
        var selectors = 'form input[name="' + fieldName + '"], form input#' + fieldName + ', form input#field_' + fieldName + ', form input[name="' + fieldName.toLowerCase() + '"], form input#' + fieldName.toLowerCase() + ', form input#field_' + fieldName.toLowerCase() + ', input[value="gaconnector_' + fieldName + '"],';
        selectors += 'form textarea[name="'+fieldName+'"], form textarea#'+fieldName+', form textarea#field_'+fieldName + ', form textarea[name="'+fieldName.toLowerCase()+'"], form textarea#'+fieldName.toLowerCase()+', form textarea#field_'+fieldName.toLowerCase()+', form textarea.'+fieldName+', form textarea[name="param['+fieldName+']"]'+", form textarea[id^='field_"+fieldName+"']";
        var inputs = document.querySelectorAll(selectors);
        if (inputs === null) {
            continue;
        } else if (typeof inputs.length === 'undefined') {
            inputs.value = gaFields[fieldName];
        } else {
            for (var i = 0; i < inputs.length; i++) {
                inputs[i].value = gaFields[fieldName];
            }
        }
    }
}

gaconnector.setCallback(setGaconnectorHiddenFields);
setInterval(setGaconnectorHiddenFields, 1000);
</script>

Creating GA Connector custom fields/properties in Hubspot

If you haven't created custom properties yet for GA Connector in Hubspot, follow the steps below on how to add them.

  1. While on your Hubspot Dashboard, go to Settings > Properties > Create Property
  2. Using the table below as a guide, add the GA Connector label as the label and click on the "code" icon beside the label field to add the equivalent input_id as Internal name (e.g. All Traffic Sources - all_traffic_sources). You can use Single-Line text as Field type for all GA Connector fields.
  3. Repeat for all GA Connector fields that you need to track.

Label Input ID Label Input ID
All Traffic Sources all_traffic_sources Last Click Campaign lc_campaign
Browser browser Last Click Channel lc_channel
City (from IP address) city Last Click Content lc_content
Country (from IP address) country Last Click Landing Page lc_landing
First Click Campaign fc_campaign Last Click Medium lc_medium
First Click Channel fc_channel Last Click Referrer lc_referrer
First Click Content fc_content Last Click Source lc_source
First Click Landing Page fc_landing Last Click Term lc_term
First Click Medium fc_medium Last Click Timestamp lc_timestamp
First Click Referrer fc_referrer Longitude longitude
First Click Source fc_source Latitude latitude
First Click Term fc_term Number of Website Visits page_visits
First Click Timestamp fc_timestamp Operating System OS
Google Analytics CID GA_Client_ID Device device
Google Analytics Measurement ID GA_Measurement_ID Region region
Google Analytics Session ID GA_Session_ID Pages Visited pages_visited_list
Google Click Identifier gclid Time Spent on Website time_passed
IP Address ip_address Time Zone time_zone

GA Connector adds UTM information, geolocation, referral information to your Hubspot leads.

Setting up your form

  1. Add the GA Connector properties you've created to your form and make the field hidden
  2. After adding all the GA Connector properties you need, hit Update

Embedding your form to your Website

Instead of using the default embed code that Hubspot has provided. You will need to use the code below:

HTML
    <script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
    <script>
        hbspt.forms.create({
            region: "xxx",
            portalId: "xxxx",
            formId: "xxxxx",
            onFormReady: function($form) {
                function checkGaconnector() {
                    gaFields = gaconnector.getCookieValues();
                    return !!gaFields.lc_source;
                }
                var gaFields;
                var interval = setInterval(function() {
                    if (checkGaconnector()) {
                        for (var fieldName in gaFields) {
                            var selectors = 'input[name="' + fieldName + '"], input#' + fieldName + ', input#field_' + fieldName + ', input[name="' + fieldName.toLowerCase() + '"], input#' + fieldName.toLowerCase() + ', input#field_' + fieldName.toLowerCase() + ', input[value="gaconnector_' + fieldName.toLowerCase() + '"],     ';
                            selectors += 'textarea[name="' + fieldName + '"], textarea#' + fieldName + ', textarea#field_' + fieldName + ', textarea[name="' + fieldName.toLowerCase() + '"], textarea#' + fieldName.toLowerCase() + ', textarea#field_' + fieldName.toLowerCase() + ', textarea.' + fieldName + ', textarea[name="param[' + fieldName + ']"]' + ", textarea[id^='field_" + fieldName + "']";
                            var inputs;
                            if ($form.nodeName) {
                                inputs = $form.querySelectorAll(selectors);
                            } else {
                                inputs = $form[0].querySelectorAll(selectors);
                            }
                            if (inputs === null) {
                                continue;
                            } else if (typeof inputs.length === 'undefined') {
                                inputs.value = gaFields[fieldName];
                            } else {
                                for (var i = 0; i < inputs.length; i++) {
                                    inputs[i].value = gaFields[fieldName];
                                }
                            }
                        }
                        clearInterval(interval);
                    }
                }, 1000);
            }
        });
        
    </script>

Note: Replace the region, portalId, and formId on lines 4, 5 & 6 with your own. You can find these details on the default embed code that Hubspot provided.