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:
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>
Hey @PlayaLink, so that we're on the same page is your flow as follows?
User creates survey
Your server returns a response code.
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?
Your HubID
The page that you're implementing this code on
The steps I can go through to reproduce your issue