Hubspot Additional Properties not being set on API

I have the following code in my API call

let postPropertiesData = {};

if (data.additionalProperties) {
 for (let key in data.additionalProperties) {
 postPropertiesData[data.additionalProperties[key].key] = data.additionalProperties[key].value;
 }
}

const postProperties = JSON.stringify(postPropertiesData);

const setProperties = JSON.stringify({
 utk: data.userToken,
 eventName: data.eventName,
 properties: postProperties,
 ocurredAt: getTimestamp(),
});

Now this is the template code I use and then there is a form with an input fields where I can set the property name and the value (it’s set dynamically). It looks like this

However, while it prepares the data is looks like this

don’t mind the actual value itself, that’s dynamically populated and I have checked the variables and they are working.

My question is two-fold:

  1. the structure seems off according to the presentation made in Accounts Dashboard | HubSpot
    1. How do I fix that structure above
  2. also the request doesn’t actually put any data as the request body has “properties”:“{}”,
    1. how do I make sure that it gets set and populated properly?

@YizSeg , are you still working on this?

How does the json body look when you have run this?

@miljkovicmisa , wanna try your hand in this :grinning_face:

G’day @dennisedson @miljkovicmisa

In the hubspot log I get no request header or body even though the call goes through and it works otherwise.

In the debug it put it as

properties: {}

For same reason I’m not getting the properties: {‘hs_asset_type’:‘PDF’} and the rest as planned. The table is functioning as you can see that Google Tag Manager is actually reading the variables properly in the table and I"ve checked in preview mode and the variables are functioning correctly.

Something is happening between that, where is isn’t being converted into an object properly containing all the property names and values. And isn’t being set as part of the request body.

Hello @YizSeg , for your first question in order to create the right structure you will need to pass another object inside the object of the propertiesData so it needs to look like this:

let postPropertiesData = {
 "properties" : []
};

So with the above you will be able to push all the constructed properties correctly inside the properties object as per the documentation, this will look like the following:

// first we do an iteration of the data and store it temporarily in a variable
var tempProperties = {};
if (data.additionalProperties) {
 for (let key in data.additionalProperties) {
 tempProperties[data.additionalProperties[key].key] = data.additionalProperties[key].value;
 }
}

// then we insert all tempProperties in the correct object inside postPropertiesData
postPropertiesData.properties.push(tempProperties);

You can also add all the other key-value pairs in the same manner:

postPropertiesData["utk"] = data.userToken;
postPropertiesData["eventName"] = data.eventName;
postPropertiesData["ocurredAt"] = getTimestamp();

For the second part I would dump the “data” and the “postPropertiesData” to see what data gets stored so you can easily debug this.

@miljkovicmisa thanks for that I tried that, but it came back with a TypeError saying that TypeError: Object has no ‘push’ property. Either way, I found the answer.

I imported the API called makeTableMap

const makeTableMap = require('makeTableMap');

and then instead of using a if and for statements,

I used the API to attach it straight to the object

const setBody = JSON.stringify({
 'portalId':data.portalID,
 'utk': data.userToken,
 'eventName': data.eventName,
 'properties': (data.additionalProperties) ? makeTableMap(data.additionalProperties,'property','value'):{},
 'ocurredAt': getTimestamp(),
});

I tested it, got a successful response and it sent the properties as part of the request body.

Great news @YizSeg , this is a nice workaround, seems like I had a typo in my code and that’s why you got the “push” error, the “properties” key needs to be an object not a key-value pair so the preformatting of the variable should look like this (I also updated my answer):

let postPropertiesData = {
 "properties" : []
};

Cheers!