APIs & Integrations

EMartin51
Participant

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Hi,

 

I'm trying to push a form's email value into the dataLayer once a form has been submitted; I've called the variable "hs-form-email" in the code below.  I've been trying lots of different ways, and if I literally just use 'event.data', I can see the names and values in the variable, but I can't find a way to only extract the email value - could someone help?

 

<script type="text/javascript">
  window.addEventListener("message", function(event) {
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
      window.dataLayer.push({
        'event': 'hubspot-form-success',
        'hs-form-guid': event.data.id,
        'hs-form-email' : event.data.email.value
      });
    }
  });
</script>

 

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

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Hi @EMartin51 ,

 

There is probably a better way to retrieve the value from the data object, but this should work:

<script type="text/javascript">
  window.addEventListener("message", function(event) {
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
      window.dataLayer.push({
        'event': 'hubspot-form-success',
        'hs-form-guid': event.data.id,
        'hs-form-email' : $('input[name="email"]').val()
      });
    }
  });
</script>

Not sure why, but I've only been able to manipulate and retrieve form data with jQuery, but I love to see a Vanilla JS solution that actually works.



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

22 Replies 22
adden
Member

Google Tag Manager - Push Form Data to dataLayer

SOLVE

While some answers here are good, you don't need that much JavaScript to do this. 

You can use the Hubspot HTML Listener tag that you're also using to track normal Google Ads conversions and add 

 

 'hs-data': event.data,

 

as @NiklasBuschner did.

So the entire tag would look like this: 

 

 

<script type="text/javascript">
  window.addEventListener("message", function(event) {
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
      window.dataLayer.push({
        'event': 'hubspot-form-success',
        'hs-data': event.data,
        'hs-form-guid': event.data.id
      });
    }
  });
</script>

 

 

This adds all the data in the form to the data layer, you just need to create the variables to get it.

For example, create a new Data Layer variable for email: hs-data.data.submissionValues.email

For first name: hs-data.data.submissionValues.firstname

For last name: hs-data.data.submissionValues.lastname

 

Screenshot 2023-12-06 150355.png

That's it. I've written about it in a bit more depth here: https://adden.agency/blog/set-up-enhanced-conversions-data-layer/

0 Upvotes
NiklasBuschner
Contributor

Google Tag Manager - Push Form Data to dataLayer

SOLVE

You can also dynamically access email input from the data layer with this step by step process.

 

Key to this process is, that you are not reliant on values being extracted from the DOM and you do not need to know at with position the email field is located since the code retrieves it dynamically.

 

1. HTML Tag that triggers on all pages

<script type="text/javascript">
  window.addEventListener("message", function(event) {
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
      window.dataLayer.push({
        'event': 'hubspot-form-success',
        'hs-data': event.data,
        'hs-id': event.data.id
      });
    }
  });
</script>

 

2. Set up data layer variable called HubSpot Data where the name of the data layer variable is exactly hs-data.

 

Bildschirmfoto 2023-01-24 um 11.49.24.png

 

3. Set up custom javascript variable – I call it HubSpot Mail (Forms) – with the following code:

function() {
  var email;
  for (var i = 0; i < {{HubSpot Data}}.data.length; i++) {
    if({{HubSpot Data}}.data[i].name === 'email') {
      email = {{HubSpot Data}}.data[i].value;
    }
  }
  return email;
}

 

4. Use the variable HubSpot Mail (Forms) at your disposal, for example for enhanced conversions in Google Ads.

 

5. Set up data layer variable called HubSpot ID where the name of the data layer variable is exactly hs-id. You can now also use the ID of the form to send it to Google Analytics 4 as a custom parameter to identify the forms later on.

 

Bildschirmfoto 2023-01-24 um 11.49.51.png

 

If you need any clarification about the setup, please reach out anytime!

JNordenskjöld
Member

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Hi, Thanks for the Solution!
However I can´t get it to work completely.
I´m trying to use the variable from the field input as a an Event Parameter in a GA4Event.
(I´m looking to use a custom input field of "Customer_Segment" to diversify my conversion value based on that, but in this case I´m using your email example so as not to do too many things at once)

JNordenskjld_0-1687972536958.png

But when I fill out my form and submit in preview mode, I only get "value: undefined":

SS1.png

In the API CallI can see that the information is there however, but like I said, it doesn´t seem to get picked up by the Event Parameter... 

JNordenskjld_1-1687972932094.png


Do you have any suggestions as to what I should try?

/joel



0 Upvotes
NiklasBuschner
Contributor

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Have you created the hs-data Data Layer Variable? This is necessary for the custom javascript variable to work correctly.

 

When in doubt, please show more of your setup so I can assist better.

JNordenskjöld
Member

Google Tag Manager - Push Form Data to dataLayer

SOLVE

yes, I have the complete setup as your instructions
Below you can see the complete setup, with my customer Segment input instead of Email. Maybe I´m missing something...?

Thanks!!!

Form submission Listener:

JNordenskjld_0-1688045509305.png


Data Layer Variable:

JNordenskjld_1-1688045569642.png


Java Script Variable:

JNordenskjld_2-1688045663162.png


HubSpot ID variable

JNordenskjld_3-1688045730817.png


GA4 Event Form Success Tag

JNordenskjld_4-1688045922650.png

 

0 Upvotes
NiklasBuschner
Contributor

Google Tag Manager - Push Form Data to dataLayer

SOLVE

In the form submission listener, you have to use onFormSubmit instead of onFormSubmitted. Please try that and let me know if it works.

0 Upvotes
JNordenskjöld
Member

Google Tag Manager - Push Form Data to dataLayer

SOLVE

No, unfortunately this doesnt help I still get "undefined"....

0 Upvotes
HSSW
Participant

Google Tag Manager - Push Form Data to dataLayer

SOLVE

edit: works for me; thanks!

 

the solution was referring to the 'onFormSubmit' and not the 'onFormSubmitted' event!

0 Upvotes
SebKoppjes
Participant | Partner
Participant | Partner

Google Tag Manager - Push Form Data to dataLayer

SOLVE

This should be the accepted solution in my opinion.

 

The one Teun mentions isn't working for me.

Jaycee_Lewis
Community Manager
Community Manager

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Hey, @SebKoppjes 👋 Please consider clicking the Accept Solution button for @NiklasBuschner's post. I'm sure he'd appreciate it. And it will add it to the top of this post. 

Thank you! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

SebKoppjes
Participant | Partner
Participant | Partner

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Isnt it the opening post that can do that only? I dont see the button?

0 Upvotes
NiklasBuschner
Contributor

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Thanks @SebKoppjes highly appreciated!

0 Upvotes
Teun
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

Google Tag Manager - Push Form Data to dataLayer

SOLVE

This is indeed a great solution.

I made a quick video about sending Tagmanager events from HubSpot: https://youtu.be/s3Ie-tKn0hM

Hope this helps someone.



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.


Haccount
Participant

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Hey @NiklasBuschner,

 

Thanks a lot for sharing this solution.

 

Worked perfectly on my end by just doing small tweaks on the CSJ to retrieve other fields from the form.

 

I'm glad I came across your post.

 

Cheers.  

mateostabio
Member

Google Tag Manager - Push Form Data to dataLayer

SOLVE

This is exactly what I needed. I am getting errors when trying to preview GTM.  How did you link these variables to Google Ads?

NiklasBuschner
Contributor

Google Tag Manager - Push Form Data to dataLayer

SOLVE

You create a new variable from the User-provided Data type and select your HubSpot Mail (Forms) variable for email and optionally a phone variable for phone.

 

Bildschirmfoto 2023-02-02 um 13.26.53.png

 

Then you add this variable to your Google Ads conversion tag from the option "Include user-provided data from your website".

 

Bildschirmfoto 2023-02-02 um 13.30.28.png

 

Finally, you just have to activate enhanced conversion in the Google Ads conversion configuration.

 

I hope this helps.

ELobato2
Member

Google Tag Manager - Push Form Data to dataLayer

SOLVE

How did you add it? Because i tried too like @Teun say but Listener don't appear when i submit a form.  If i try without email data, it works but adding that additional code breaks the HTML for me. 

0 Upvotes
Teun
Solution
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Hi @EMartin51 ,

 

There is probably a better way to retrieve the value from the data object, but this should work:

<script type="text/javascript">
  window.addEventListener("message", function(event) {
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
      window.dataLayer.push({
        'event': 'hubspot-form-success',
        'hs-form-guid': event.data.id,
        'hs-form-email' : $('input[name="email"]').val()
      });
    }
  });
</script>

Not sure why, but I've only been able to manipulate and retrieve form data with jQuery, but I love to see a Vanilla JS solution that actually works.



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.


ANeita
Participant

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Can you please help provide an example of this but for the hubspot meetings form submission?

0 Upvotes
ELobato2
Member

Google Tag Manager - Push Form Data to dataLayer

SOLVE

Hi Teun, would yo help me how to add this? I got some problem because when i add that code that you posted here. No more hubspot-success-form fires.  Its an Custom HTML Tag. 

 

 

0 Upvotes