Creating Custom Event via GTM

Hi there,

Just wanted some advice as it’s my first time creating a custom event in Hubspot - we want to capture email/phone clicks on our website and use this in our reports. The event is firing but wanted to check if you foresee any issues with this JS code?
This is the JS in hubspot:

_hsq.push([
“trackCustomBehavioralEvent”,
{
name: “pe43976934_email_clicks”,
properties: {
click_url: clickURL,
click_text: clickText,
pageTitle: pageTitle,
pagePath: pagePath
}
}
]);

And this is what’s in GTM:

<script>
function sendToHubSpot(clickURL, clickText, pageTitle, pagePath) {
_hsq.push([
‘trackCustomBehavioralEvent’,
{
name: “pe43976934_email_clicks”,
properties: {
click_url: clickURL,
click_text: clickText,
pageTitle: pageTitle,
pagePath: pagePath
},
},
]);
}

// Ensure the GTM variables are passed correctly
sendToHubSpot(‘{{Click URL}}’, ‘{{Click Text}}’, ‘{{DLV - Page title}}’, ‘{{Page Path}}’);
</script>

Hey @LK8 ,
The JavaScript code you’ve provided for tracking email clicks in HubSpot looks good overall. However, here are a few suggestions to ensure it works smoothly:
1. Ensure that the variables (clickURL, clickText, pageTitle, pagePath) passed to the sendToHubSpot function in GTM ({{Click URL}}, {{Click Text}}, {{DLV - Page title}}, {{Page Path}}) are correctly formatted and match the expected data types in HubSpot.
2. Ensure that the event name (pe43976934_email_clicks) and property names (click_url, click_text, pageTitle, pagePath) match exactly between your GTM script and HubSpot setup
3. Always test it out with some demo content to make sure that the data is flowing as expected.
Do let me know if you are facing any issues while testing.

Hi @SanjayKumar - thank you so much for your feedback!

Noted, the variables for GTM are correct and it looks like it’s matching in Hubspot now.

So far after testing it looks like it’s coming through ok but will keep an eye on it.