So I am trying to retrieve all my marketing emails with statistics. The json response works fine until it comes to my stats.
For example,
var no_sent = object.stats.counter.sent;
Would throw a TypeError: Cannot read property "counter" from undefined.
When I looked at the response, it seems that the stats are not available and that field contains "undefined". All other rows have a stats.counter.sent value.
var go = true;
var hasMore = false;
var offset = 0;
while (go)
{
var url_query = API_URL + "/marketing-emails/v1/emails/with-statistics?limit=300";
if (hasMore)
{
url_query += "&offset=" + offset;
}
var response = UrlFetchApp.fetch(url_query, options).getContentText();
response = JSON.parse(response);
offset = response['offset'];
if (offset > 0)
{
go = false;
}
Logger.log(response);
response.objects.forEach(function(object) {
var created = (object.hasOwnProperty('created')) ? object.created : 'NA';
var createdById = (object.hasOwnProperty('createdById')) ? object.createdById : 'NA';
var sent = object.stats.counter;!
Can you write me the output of this line which you get
Logger.log(response);
I think
response = JSON.parse(response);
is not sufficient for getting into the response parameter. The json response contains one more index of "response" under which all objects are defined. Not sure if your code is traversing upto that level.
I have erased all confidential parts and the some parts are labelled xxxxxxxxxxxxxx to remove the confidential info. As you can see, it is exactly the same as the json response given on the all marketing emails api documentation but except I am not getting stats.counter.sent data, instead it shows an undefined
Everything looks fine in your requests. Not sure what can be a cause here. May be the emails who do have stats data have not been sent any more from the CRM.
Hi, @kawyns1. Apologies for the delayed response; I have been out of the office.
To clarify, when you said:
When I looked at the response, it seems that the stats are not available and that field contains "undefined". All other rows have a stats.counter.sent value.
were you saying that one particular email's stats.counters.sent field (note counters not counter) contains undefined while most or all other email objects you retrieved had "sent":0 or some other valid number?
I'm testing this endpoint for my own account and am not seeing any undefined values:
If you're consistently getting undefined for one or more of your emails, could you confirm the last four digits of your Hub ID and share the email ID with me?
I do not see the undefined values. It just does not appear in the json. However, I see it in the spreadsheet. It is evident that the undefined values are the e-mails that have not been sent.
The question is how to stop it from breaking my code.