Firing custom behavioural event through GTM

Hello,

I am trying to fire a custom behavioural event through GTM, but it seems to be erroneously firing on all pages, and not when the tag fires in GTM.

All the pages on my website have the Hubspot tracking code.

I have created a custom javascript event in the “Custom Behavioral Events” menu, and retrieved the following code:

_hsq.push([
 'trackCustomBehavioralEvent',
 {
 name: "pe26533797_ebook_form_complete",
 properties: {
 // give each property a value so we know what they mean
 form_type: null
 },
 },
]);

I have inserted this into a custom HTML tag in GTM and created my required triggers:

When previewing the GTM container, I have tested that the tag fires correctly under the right conditions. However, when I view the contact record in Hubspot, the event is not recorded in the activity log.

Additionally, the same custom behavioural event seems to be firing on all pages regardless of my tag triggers even though I have not published the container with my new tag.

Any assistance would be greatly appreciated.

Hello @LMcNair, thank you for posting in our Community!

When creating the tag in google manager did you do the following:

  • Below Tag Configuration, click Triggering to set up a firing rule. If you don’t set up a firing rule, the tag will not fire and HubSpot analytics data will not be collected.

  • For the tag to fire on all pages with the Google Tag Manager code installed (recommended), select the option for All Pages.

We also recommend reaching out to Google and ensuring everything is connected correctly as our insight on Google Tag Manager is limited.

I want to invite our top experts to this conversation @Leonard0 @Ben_M @danmoyle, any recommendations for @LMcNair?

Thank you,

Pam


Additionally, the same custom behavioural event seems to be firing on all pages regardless of my tag triggers even though I have not published the container with my new tag.


I’m seeing the same issue where it fires on all pages. However, when that custom behavioral event is published to the contact record, it doesn’t contain any of the additional custom properties that have been defined for that event.

In the screenshot below, we see that the custom behavioral event fires at the same time as the page view event. It also only contains the 14 standard properties without any additional custom properties that would be pushed by the tracking script.

_hsq.push([
 'trackCustomBehavioralEvent',
 {
 name: "pe21775395_folloze_activity",
 properties: {
 // give each property a value so we know what they mean
 folloze_activity_data: activityData,
 folloze_asset_id: assetId,
 folloze_asset_name: assetName,
 folloze_board_name: FollozeState.initialState.board.name,
 folloze_board_id: FollozeState.initialState.board.id,
 folloze_activity_type: activityType
 },
 },
]);

We are experiencing the same issue. Did you find any solution to this?

I have the same problem. Did you find any solution to this issue?

Hey there, the following method worked for me

1. Change the if condition in the code HubSpot provides and save it

// 'true' tracks this event on every page view
var trigger = false;
if (true && trigger) {
 _hsq.push([
 'trackCustomBehavioralEvent',
 {
 name: "pe23968119_test_event_1",
 properties: {
 // give each property a value so we know what they mean

 },
 },
 ]);
}

2. In your custom HTML tag, change the trigger variable to true

// 'true' tracks this event on every page view
var trigger = true;
if (true && trigger) {
 _hsq.push([
 'trackCustomBehavioralEvent',
 {
 name: "pe23968119_test_event_1",
 properties: {
 // give each property a value so we know what they mean

 },
 },
 ]);
}

This will ensure that the event fires only when the tag fires instead of on every page view

Let me know if it works for you!

Hi!

thanks for your reply. I tried it, but it doesn´t work for me. The event doesn´t fire.
I added
var trigger = false;
if (true && trigger) {
to the event snippet in hubspot and saved.
I added
var trigger = true;
if (true && trigger) {
to the HTML tag in GTM
My tag is firing in GTM, but the event is not firing in Hubspot.
I appreciate any ideas and tips!

Hey, can you try implementing a new event with the above method instead of editing the existing one.

Also, make the base HubSpot tag fires on every page before firing the event tag.

The base code can be found under settings > Tracking & Analytics > Tracking Code

Thanks for your reply. No change, its still not working. :frowning:

No further suggestions, you can reach to me via mail and then maybe we can schedule a call to debug this live.

joyan.bhathena@datavinci.services

Hi Erhard1, I’ve experiencing the same issue, but discovered what could be the issue:
- The Hubspot tracking code has to be fully loaded for a custom behavorial event to show in Hubspot, even if the tag fires correctly in GTM.

- When you use a custom event that happens a lot of seconds after page load, your event will probably fire correctly, but when you fire a tag directly after pageload, for example DOM ready, Window Loaded or Page View, the tag in GTM will fire, but events won’t show up in Hubspot.

- I’ve worked arround this by inserting a code in Hubspot that pushes a dataLayer event in GTM named ‘hubspot_script_loaded’ that I can use to fire my tags.

- You can insert this code in Hubspot directly by going to Settings > Tracking & Analytics > Tracking Code > Customize Javascript and then insert the following code:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
‘event’: ‘hubspot_script_loaded’
});
Then you will now in GTM when the Hubspot script is loaded and use that to fire your tags. I hope this can help you!

Besides the info I just posted: when making custom behavioural events in Hubspot but implementing them in GTM, you can also delete the code you get in Hubspot and just insert a comment that says you track this event through GTM.

Sending Custom Events to HubSpot with GTM (Detailed Version - Customizable)

This guide details how to send a custom event to HubSpot using Google Tag Manager (GTM) with dynamic values. You’ll replace the placeholder event name with the actual name provided by HubSpot in your script.

Here’s what we’ll cover:

  1. Finding Your HubSpot Event Name
  2. Creating a GTM Variable for Dynamic Values
  3. Building the Custom HTML Tag with Dynamic Properties
  4. Setting Up the Trigger to Fire the Event

1. Finding Your HubSpot Event Name:

  • Locate the HubSpot script within your website code. This script typically resides in the <head> section of your website’s HTML.
  • Look for the event name within the script. It might be defined as a variable or passed as part of the _hsq.push function.

2. Creating a GTM Variable for Dynamic Values:

  • In GTM, navigate to the “Variables” section.
  • Click “New” and choose “Built-In Variable.”
  • Select “Click Element” from the available options.
  • Name your variable something descriptive, like “ClickedElementDetails.”
  • Save the variable.

This variable will capture details about the element the user clicked, allowing you to access dynamic values like the element’s text content or class name.

3. Building the Custom HTML Tag with Dynamic Properties:

  • In GTM, navigate to the “Tags” section.
  • Click “New” and choose “Custom HTML.”
  • Paste the following code into the HTML field, replacing YOUR_HUBSPOT_EVENT_NAME with the actual event name found in your HubSpot script:

JavaScript

_hsq.push([ 'trackCustomBehavioralEvent', {
 name: "YOUR_HUBSPOT_EVENT_NAME", // Replace with your actual HubSpot event name
 properties: {
 cta_name: {{ClickElementDetails.textContent}}, // Use ClickedElementDetails variable for dynamic text content
 latency: {{ClickElementDetails.className.indexOf('primary') !== -1 ? 'high' : 'low'}}, // Example using class name for dynamic latency (replace with your logic)
 }
}]);

Use code with caution.

content_copy

Explanation:

  • We removed the if condition entirely.
  • We use the {{ClickElementDetails.textContent}} variable to dynamically populate the cta_name property with the text content of the clicked element.
  • The latency property uses a conditional statement to assign “high” if the clicked element’s class name contains “primary” (replace with your own logic for determining latency).

4. Setting Up the Trigger to Fire the Event:

  • In the same Custom HTML tag, navigate to the “Triggering” section.
  • Click “New” and choose the trigger type based on your desired behavior. Common options include:
    • Click - All Elements: Fires when any element is clicked. (Not recommended for specific CTA clicks)
    • Click - Link Clicks: Fires when a link is clicked.
    • Click - Element Clicks: Fires when a specific element (with a class or ID) is clicked. (Recommended for targeted CTA clicks)
  • Configure the trigger settings based on your chosen trigger type.
  • In the Custom HTML tag, set the “Firing Trigger” to the trigger you just created.

Save the tag and publish your GTM container.

Additional Notes:

  • Replace the logic in the latency property with your own logic to determine the appropriate value based on the clicked element.
  • You can create additional GTM variables to capture other dynamic information from the clicked element and use them in the event properties.
  • Consider using a more granular trigger to avoid sending the event on every page load.

By following these steps, you can configure GTM to send custom events to HubSpot with dynamic property values whenever the user clicks a specific element. This allows you to capture more relevant data about your user interactions and leverage it within HubSpot.

Sharing with HubSpot Community:

When sharing this guide with the HubSpot community, emphasize the importance of using the correct event name provided by HubSpot in their script. Highlight the flexibility of using GTM variables to create dynamic event properties for more insightful data collection.