Creating custom events Javascript API


So I am trying to create a custom event to track clicks but it doesn’t work. Please tell me what I am missing. My website is in hubspot so it already has the tracking code. Then I go to Reports -> Events click create new event, select custom event click javascript API and put this code there

function getParameterByName(name) {
            var match = RegExp('[?&]' + name + '=([^&]*)').exec(;
            return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

var _hsq = window._hsq = window._hsq || [];
    email: getParameterByName("email")

_hsq.push(["trackEvent", {
    id: "000001105495"

_hsq.push(["trackClick", "h1", "000001105495"]);

Then I click save. So then I open my test page and click on the h1 title >“Enter the on-page title” then I go to the events and nothing is tracked completions are 0 contacts are 0 customers are 0. What am I doing wrong here?


@hayk Are you calling the function above? You should be calling the function somewhere and pass in the name of the html element.


I am not sure how to do that. Can you provide an example please?


Can you show me your screen where you made the Event? What did you use for the selector? Did you choose ID or class?


I go to events then click create event

I select Custom Event as the type of event and click Add Custom Javascript

It brings me this place to wirte JS. I write it and click create event.


@hayk In the drop down when you make a new event you could chose the create a clickable event. That might be an easier way to create the functionality you are looking for.


Well my exact aim is not this I just want to figure out how the Javascript API works. So do you have any guesses what I am doing wrong there ?
EDIT: Is there anything else I need to do after creating the events? Like add them somewhere in the pages , or as far as I get it they added to the pages as soon as I create them, didn’t they?


Are you passing in an email as a query string? The function should be pulling in an email from the URL that would look something like''

with the quotes most likely percent encoded


Yes I do that, I tried with and without quotes. And when I do that with quotes I get this warning from v2.js
v2.js:4 The specified value "''" is not a valid email address. But anyway I don’t get any event completions with and without quotes.


Looking at your last two hsq.push calls. We might need to try and merge them so they follow closer to this example.

_hsq.push(["trackEvent", {
value: 20.5

Is the value you are trying to send in h1?


Yes I want to track click events on h1 element.


@hayk I got an event to register using the following code.

_hsq.push(["trackEvent", {
id: "000001411558",
value: "clicked the h1"

Are you wrapping your code in an OnClick event as well? You need to have something call the code on each click or else it will only fire once on page load.


How would this look with the OnClick wrap?


@ronkagan here is a link to learn more about onClick.