APIs & Integrations

PlayaLink
Member

Log a custom event with Javascript API

I'm having some trouble wrapping my head around how this JS API works for custom events.

Before I explain what I've done, why don't I explain what I'm trying to do.

We have a webapp that allows users to create surveys. Whenever a user creates a new survey, I would like to add an event to that Contact's timeline and also increment a surveys_launched count on the Contact record.

I've created an event in Reports > Analytics Tools called "Launched A Survey". I saved it and then grabbed the unique id for it. I did not add any code to the Custom JS field because I don't want this code to fire when the HS tracking code loads. I only want it to fire after our server responds with a 200 (i.e. the survey was created successfully).

In our codebase, when we do get a successful server response, I pasted this code in:

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

Where '000006701463' corresponds to the "Launched A Survey" event.

Am I missing something here?

Thanks!!

0 Upvotes
4 Replies 4
cbarley
HubSpot Alumni
HubSpot Alumni

Log a custom event with Javascript API

Hi @PlayaLink, I've been trying to replicate the behavior you're experiencing but am unable to do so. I'm able to successfully make a call to my own account to an event using this code and it registers to that particular event. I can see the call being made in the Network Tab when looking for track.hubspot.com events:

Here's my code:

<button id="myButton">Click Me To Fire Event! (or not)</button>

<script>
  var _hsq = window._hsq = window._hsq || [];
  var button = document.getElementById("myButton");

  function fireEvent(){
    _hsq.push(["trackEvent", {
      id: "000006373220"
    }]);
    alert("event recorded! party!");
  }
  
  button.addEventListener("click", fireEvent, false);

</script>
0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Log a custom event with Javascript API

Hi @PlayaLink, sorry for the delay here. I'm digging into this with my team. I'll let you know when I have more info :slight_smile:

0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Log a custom event with Javascript API

Hey @PlayaLink, so that we're on the same page is your flow as follows?

  1. User creates survey
  2. Your server returns a response code.
  3. After 200 event a HubSpot event is tracked.

If so, this seems doable. If you're using ajax to call to your servers, you should be able to parse the response for the response code and then fire the event to HubSpot. I don't see anything wrong with your code as it stands, though for what it's worth I don't see the _hsq array defined there.

var _hsq = window._hsq = window._hsq || [];

Could you respond with the following so I can dig in further?

  1. Your HubID
  2. The page that you're implementing this code on
  3. The steps I can go through to reproduce your issue

Thank you!

0 Upvotes
PlayaLink
Member

Log a custom event with Javascript API

Hi @Connor_Barley
I do declare _hsq (with the same code you included) just above that code snippet. Just didn't include it in my question test.

When I log _hsq to the console after pushing the trackEvent array, _hsq is still empty, if that helps you.

I'm implementing inside our app code, on my local environment, so can't point you to a marketing site webpage.

But our HubId is 5326761.

0 Upvotes