APIs & Integrations

Hayk
Miembro

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(window.location.search);
            return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
        }

var _hsq = window._hsq = window._hsq || [];
_hsq.push(["identify",{
    email: getParameterByName("email")
}]);

_hsq.push(["trackEvent", {
    id: "000001105495"
}]);

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

Then I click save. So then I open my test page http://wireline-dev-2522899-2522899.hs-sites.com/teste 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?

0 Me gusta
13 Respuestas 13
3PETE
HubSpot Employee
HubSpot Employee

Creating custom events Javascript API

@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.

0 Me gusta
ron_kagan
Colaborador

Creating custom events Javascript API

How would this look with the OnClick wrap?

0 Me gusta
3PETE
HubSpot Employee
HubSpot Employee

Creating custom events Javascript API

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

https://www.w3schools.com/jsref/event_onclick.asp

0 Me gusta
3PETE
HubSpot Employee
HubSpot Employee

Creating custom events Javascript API

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", {
id: "EVENT_ID_HERE",
value: 20.5
}]);

Is the value you are trying to send in h1?

0 Me gusta
Hayk
Miembro

Creating custom events Javascript API

Yes I want to track click events on h1 element.

0 Me gusta
3PETE
HubSpot Employee
HubSpot Employee

Creating custom events Javascript API

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

http://wireline-dev-2522899-2522899.hs-sites.com/teste?email='mail@hubspot.com'

with the quotes most likely percent encoded

0 Me gusta
Hayk
Miembro

Creating custom events Javascript API

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 "'hayk@incredo.co'" is not a valid email address. But anyway I don’t get any event completions with and without quotes.

0 Me gusta
3PETE
HubSpot Employee
HubSpot Employee

Creating custom events Javascript API

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

0 Me gusta
Hayk
Miembro

Creating custom events Javascript API

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

0 Me gusta
3PETE
HubSpot Employee
HubSpot Employee

Creating custom events Javascript API

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

0 Me gusta
Hayk
Miembro

Creating custom events Javascript API

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.

0 Me gusta
3PETE
HubSpot Employee
HubSpot Employee

Creating custom events Javascript API

@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.

0 Me gusta
Hayk
Miembro

Creating custom events Javascript API

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?

0 Me gusta