Skip to content

Instantly share code, notes, and snippets.

@JacobLett
Created March 11, 2019 18:05
Show Gist options
  • Save JacobLett/a62a9bc4c644c31f9f99190d29abc02e to your computer and use it in GitHub Desktop.
Save JacobLett/a62a9bc4c644c31f9f99190d29abc02e to your computer and use it in GitHub Desktop.
HubSpot Form Conversion Tracking - Google Analytics Event Script
######## Add to HEAD
<!-- Global site tag (gtag.js) - Google Ads: 802890537 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-802890537"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-ID');
</script>
######## Add to before Closing /Body
<script>
window.addEventListener('message', event => {
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
// Google analytics hubspot form conversion tracking
var hsformID = event.data.id;
var URL = window.location.href;
gtag('event', hsformID, {
'event_category': 'HubSpot Form Submit',
'event_label': URL
});
// console.log("URL " + URL + " FormID " + hsformID);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment