Hubspot custom javascript triggered multiple times for one click




I have a click event setup with a Custom Javascript event call. It is linked to a button on a test site (which is within the domains listed for events).

I have made a test today however I got strange results. For some strange reason I get multiple completions and contacts. I suppose its from the “if true” statement, but im not sure.

// Update to ‘true’ if you want to track event on every page view
if (true) {
_hsq.push([“trackEvent”, {
id: “xxxxxxxxxxxx”,
value: null

This code is only embedded on one site currently, however it keeps getting fired. I am currently stumped as to why this happens and would be grateful for some assistance.


@Jost is that if(true) inside of the function that gets run when the button is clicked? if the test case is always true then why did you place it in an if block?


@pmanca good point, the if statement has been removed (i just added it because of the generated embedded JavaScript comment).

I have seen that the embedded hubspot code can then include all the events defined within the “Events” page. As useful as this is I would prefer to put code into individual pages instead of cramming them into one .js file. I have tried replicating this event.

However this raises many questions. If I put in a random ID that hasn’t been generated yet, will it be registered within the Events page? Should the ID be numerical or a Title string?

I have currently taken the api page code and tried attaching it to a click event.

document.getElementById(“mahBtnId”).onclick = function() {
var _hsq = window._hsq = window._hsq || [];

_hsq.push(["trackEvent", {
    id: "Clicked Buy Now button",
    value: 20.5}]);



@Jost And the results of that click event is that it is being called multiple times whenever someone clicks on the “mahBtnId” button? I don’t see any issues in the code pasted above in your first post or the code you copied from the docs.


@pmanca understandably yes, the event is triggered whenever “#mahBtnId” is clicked, however as mentioned above this was on a test site, which only I have access to.

Just to clarify:
When I put the true statement into the generated code it triggered globally (understandably). This is what caused the multiple clicks.

What I do not understand is when I insert the code from the aforementioned API page onto a test page (which has the Hubspot code embedded) I get no results in the events page.

From the test page I have found out:

  • The click event is definetly being triggered (tested with console.log at the beginning and end of the click event).
  • The code copied from the Events JavaScript API page

The code inside the event is:

_hsq.push([“trackEvent”, {
id: “Clicked Buy Now button”,
value: 20.5

Is the example Id incorrect? Am I supposed to write it numerically?

In the page it says I can dynamically create an event using strings, however I do not see it on the hubspot “Events” roster. Am I missing some setup details? Should I track the pageViews in order to setup a click event?

Thanks for the support so far.


@Jost Have you created the corresponding event in the HubSpot platform? The events you trigger need to match up to an event you created in HubSpot. Similar to making a custom form submission it would have to link to a form and a form ID on HubSpot.


@Jost Pretty sure it’s fine to use a custom name / string as the id - the event will get dynamically created on the fly.

However, I’m pretty sure that these on-the-fly events will only show up in the Events list if the user who has completed them is identifiable - in other words, they have converted on a form and HubSpot can tie their email, name, etc to the event via the HubSpot cookie.

More info that might be useful to you:


@pmanca Idd i did make an event and tried embedding the event on a button click on a test site. Somehow I either no clicks or clicks from all domains (when using the default on click event setup). I am now trying to isolate the host site by putting in an if( === “domain i want to track”) .

@etlingfs Thanks for the info. There is a small caveat however, the main thing i want to track is the number of clicks made on a certain button on the site (went down to the very basics). If i can isolate this I’ll most likely move on to forms :]

Im letting the hubspot click tracker run for a few hours and hoping some triggers happen. I have also found out the root of the lack of tracking for the test clicks i have made. The domain is excluded from the tracking list. Im going to have to find some VPN solution in order to test this from inhouse it seems.


@Jost Let us know if your new trial run resulted in anything.


@pmanca Indeed, the click counter is going up!

Looking at the current setup, I will try to make a GTM based trigger simply because the tracking code can become quite big this way and I’m afraid it will effect load times (loads of triggers for sites that arent currently visited).

Thanks for the feedback folks!