CMS Development

ihashmi
Member

How to send a HubSpot form field using data layer to google tag manager?

SOLVE

I have a HubSpot form in my website where I am trying to get leads from the users. As the webpage is also connected to an active Google Ads account, I need to send the input field which holds the email to Google Tag Manager. To do so first of all, I wrote the following code in my website's <head> section:

<head>
    ...
    <script>
        window.dataLayer = window.dataLayer || [];
    </script>
    ...
</head>

Then, I created a Data Layer variable in Google Tag Manager dashboard which looks like below:

xyZFn.png

 

Finally, in my HubSpot form, I added onFormSubmitted function which looks like below to push the variable and its value (the user's email) to Tag Manager:Site

<script charset="utf-8" type="text/javascript"
    src="//js-eu1.hsforms.net/forms/shell.js"></script>
<script>
    hbspt.forms.create({
        region: "eu1",
        portalId: "xxxxxxxx",
        formId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        onFormSubmitted: function ($form) {
            dataLayer.push({ 'var': String($form.find('input[name="email"]').val()) });
        }
    });
</script>

After adding these pieces of codes, when I check the Google Ads Submit Lead Form Conversion, I still can't see the value. Website

08rNO.png

 

How Can I fix this?

0 Upvotes
1 Accepted solution
Teun
Solution
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

How to send a HubSpot form field using data layer to google tag manager?

SOLVE

Hi @ihashmi ,

 

Could you try the onFormSubmit callback instead of the onFormSubmitted?
Also, try using window.dataLayer.push instead of dataLayer.push.



Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.


View solution in original post

1 Reply 1
Teun
Solution
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

How to send a HubSpot form field using data layer to google tag manager?

SOLVE

Hi @ihashmi ,

 

Could you try the onFormSubmit callback instead of the onFormSubmitted?
Also, try using window.dataLayer.push instead of dataLayer.push.



Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.