APIs & Integrations

kawyns1
Participant

TypeError: Undefined on get all marketing emails

SOLVE

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.

0 Upvotes
1 Accepted solution
IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

TypeError: Undefined on get all marketing emails

SOLVE

Hi, @kawyns1.

 

I think I follow.

 

By spreadsheet, you're referring to an email performance export, right? These exports show 0s across the board for published but unsent emails.

 

When you retrieve the statistics for such an email via this endpoint, however, the stats object looks like this:

 

"stats": {
    "counters": {
      "sent": 0
    },
    "deviceBreakdown": {},
    "failedToLoad": false,
    "qualifierStats": {},
    "ratios": {
      "clickratio": 0,
      "clickthroughratio": 0,
      "deliveredratio": 0,
      "openratio": 0,
      "unsubscribedratio": 0,
      "spamreportratio": 0,
      "bounceratio": 0,
      "hardbounceratio": 0,
      "softbounceratio": 0,
      "contactslostratio": 0,
      "pendingratio": 0,
      "notsentratio": 0
    }
  }

While the counters object still includes a "sent": 0 field, , deviceBreakdown is empty ({}), triggering an undefined error for you.

 

Could you add logic that only pulls objects.stats.deviceBreakdown.open_device_type.computer if object.stats.counters.sent is greater than 0?

Isaac Takushi

Associate Certification Manager

View solution in original post

0 Upvotes
13 Replies 13
himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

TypeError: Undefined on get all marketing emails

SOLVE

Hello @kawyns1 

 

Can you please write your piece of code that you are using to make API request?

Thanks

Digital Marketing & Inbound Expert In Growth Hacking Technology
0 Upvotes
kawyns1
Participant

TypeError: Undefined on get all marketing emails

SOLVE
  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;!

@himanshurauthanFor some rows, the objects.stats = undefined

0 Upvotes
himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

TypeError: Undefined on get all marketing emails

SOLVE

Hello @kawyns1 

 

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.

 

Thanks

Digital Marketing & Inbound Expert In Growth Hacking Technology
0 Upvotes
kawyns1
Participant

TypeError: Undefined on get all marketing emails

SOLVE

Well I can't show you the response for obvious reasons but what I can show is you is that I get this:

undefined

when I should be getting something like this:

{deviceBreakdown={}, qualifierStats={}, counters={sent=0}, failedToLoad=false, ratios={bounceratio=0, unsubscribedratio=0, hardbounceratio=0, deliveredratio=0, contactslostratio=0, clickratio=0, softbounceratio=0, pendingratio=0, spamreportratio=0, notsentratio=0, clickthroughratio=0, openratio=0}}

the "stats" part of the json is missing for a few of these records which in turn is missing my code

 

0 Upvotes
himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

TypeError: Undefined on get all marketing emails

SOLVE

@kawyns1 

 

I need the first two lines to see for

response = JSON.parse(response);

No need to send me full response. I am trying to configure one thing only and if you can sent me first 2-3 lines of code it would be helpful

 

Thanks

Digital Marketing & Inbound Expert In Growth Hacking Technology
0 Upvotes
kawyns1
Participant

TypeError: Undefined on get all marketing emails

SOLVE

@himanshurauthan 

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

 

 {total=86, offset=0, objects=[{emailBody={% content_attribute "email_body" %}{{ default_email_body }}{% end_content_attribute %}, absoluteUrl=http://confidential.hs-sites.com/-temporary-slug-xxxxxxxxxxxxxxxxxxxxxxxxxx, vidsIncluded=[], publishDate=1.567711080413E12, canSpamSettingsId=x.xxxxxxxxxxxxxE9, 

 

0 Upvotes
himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

TypeError: Undefined on get all marketing emails

SOLVE

@kawyns1 

 

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.

@IsaacTakushi  can you please here?

 

Thanks

Digital Marketing & Inbound Expert In Growth Hacking Technology
0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

TypeError: Undefined on get all marketing emails

SOLVE

Thanks for tagging me in, @himanshurauthan.

 

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:

 

"stats": {
    "counters": {
      "sent": 1,
      "open": 1,
      "delivered": 1,
      "bounce": 0,
      "unsubscribed": 0,
      "click": 0,
      "dropped": 0,
      "selected": 1,
      "spamreport": 0,
      "suppressed": 0,
      "hardbounced": 0,
      "softbounced": 0,
      "pending": 0,
      "contactslost": 0,
      "notsent": 0
    }

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?

Isaac Takushi

Associate Certification Manager
0 Upvotes
kawyns1
Participant

TypeError: Undefined on get all marketing emails

SOLVE

Hi Isaac,

 

Thanks for the assistance. Yes, I fixed the counters issues. But now it seems I am having issues with getting the deviceBreakdown. Please see below:

 

This is the structure of my counters.sent:

var sent = (object.hasOwnProperty('stats')) ? object.stats.counters.sent : 'NA' ;

 

And for deviceBreakdown:

var open_by_computer = (object.hasOwnProperty('stats')) ? objects.stats.deviceBreakdown.open_device_type.computer : 'NA' ;

I seem to be getting undefined on my finding the open by and click by devices. I'm not sure what is causing this.

 

How can I e-mail you privately so I do not have my info here.

 

Thanks,

Kawyn

 

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

TypeError: Undefined on get all marketing emails

SOLVE

Hey, @kawyns1.

 

Happy to help. When you're printing the response, you're not seeing undefined values, though, right?

 

Feel free to direct message me. If that doesn't work, I'll share my email there.

Isaac Takushi

Associate Certification Manager
0 Upvotes
kawyns1
Participant

TypeError: Undefined on get all marketing emails

SOLVE

Hi @IsaacTakushi ,

 

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.

 

Regards,

Kawyn

0 Upvotes
IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

TypeError: Undefined on get all marketing emails

SOLVE

Hi, @kawyns1.

 

I think I follow.

 

By spreadsheet, you're referring to an email performance export, right? These exports show 0s across the board for published but unsent emails.

 

When you retrieve the statistics for such an email via this endpoint, however, the stats object looks like this:

 

"stats": {
    "counters": {
      "sent": 0
    },
    "deviceBreakdown": {},
    "failedToLoad": false,
    "qualifierStats": {},
    "ratios": {
      "clickratio": 0,
      "clickthroughratio": 0,
      "deliveredratio": 0,
      "openratio": 0,
      "unsubscribedratio": 0,
      "spamreportratio": 0,
      "bounceratio": 0,
      "hardbounceratio": 0,
      "softbounceratio": 0,
      "contactslostratio": 0,
      "pendingratio": 0,
      "notsentratio": 0
    }
  }

While the counters object still includes a "sent": 0 field, , deviceBreakdown is empty ({}), triggering an undefined error for you.

 

Could you add logic that only pulls objects.stats.deviceBreakdown.open_device_type.computer if object.stats.counters.sent is greater than 0?

Isaac Takushi

Associate Certification Manager
0 Upvotes
kawyns1
Participant

TypeError: Undefined on get all marketing emails

SOLVE

This seems like a solution, let me try than and get back to you.

 

Once again, thanks for you help and advice.

 

Kawyn