Hubspot Form Tracking Using GTM



I have used following script to track the Hubspot form through GTM but this script is not working for me. I would like if anyone can help me to fix this issue.



Hi @Roshan,

The dataLayer array must be declared before the GTM snippet in order to work correctly. If you put the Google Tag Manager container snippet above the dataLayer array, Google Tag Manager may malfunction, and will not be able to read the variables defined in the array. I've included some GTM docs below for reference:

Is it possible to add tracking to Hubspot form submit button

@Derek_Gervais I added following datalayer code above the GTM code on the page source code but still Form tracking is not working.

<script> dataLayer = [{ 'formName': 'newsletter' }]; </script>



Hi @Roshan,

In your onFormSubmit callback, can you use window.dataLayer.push() instead of dataLayer.push()?


Added window.dataLayer.push() in the following script but still not working.

<!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//"></script>
<script charset="utf-8" type="text/javascript" src="//"></script>
portalId: 'XXXXXX',
formId: 'f55aac38-d03f-498f-92d6-bba868204969',
onFormSubmit: function($form) {
window.dataLayer = window.dataLayer || [];


'event' : 'hubSpotFormSubmitted',
'formName' : 'newsletter'


Hi @Roshan,

Does this code execute before the GTM snippet is loaded? You mentioned that you were including the dataLayer definition before the GTM snippet, but in your most recent example I can see you defining the dataLayer array. The dataLayer array must be defined before the GTM snippet is called, otherwise it will not behave as expected. If you define the dataLayer before the GTM snippet and then re-define it in the onFormSubmit callback, I would also expect to see strange behavior. Your code should look (roughly) like this:

  dataLayer = [{
    'formname': 'newsletter'


<!-- Google Tag Manager -->
<!-- End Google Tag Manager -->


<!-- HubSpot form embed code -->
<!-- End HubSpot form embed code -->


@Derek_Gervais I follow the same as you mentioned above, as I cannot be shared the client website URL here but I have created one demo Hubspot account and embedded the Form on blogger and implement the same on this.

Please look into in following URLs:

The Hubspot embed code is present on this page:

This is GTM preview mode:

Custom HTML Tag "cHTM - Hubspot Form Tracking" where I added the conditional script and this is the firing tag "Hubspot Form Tracking" this should be run on Form submit success.



Hi @Roshan,

Ok, so I'm admittedly not particularly familiar with the ins-and-outs of GTM, but I noticed that one of the 'HubSpot Form Tracking' tag's firing triggers has the following filter:

_event equals hubSpotFormSubmitted

How exactly is this triggering the 'HubSpot Form Tracking' tag? That event isn't familiar to me, so I'm not sure exactly how that filter should be working.


@Derek_Gervais As you can see here the custom HTML tag " cHTM - Hubspot Form Tracking " in this i have added the conditional script where i have added the event equals hubSpotFormSubmitted.