Getting NaN when dividing numbers

96 views
Skip to first unread message

Petra Manos

unread,
Sep 14, 2020, 9:24:01 PM9/14/20
to Google Ads Scripts Forum

Hi all, I'm getting a NaN when dividing two legitimate numbers. Does anyone know what is wrong with my script, or is this a bug in scripts?

 var report = AdsApp.report(
    "SELECT AdGroupName, AllConversionValue, Cost, Clicks " +
    "FROM   ADGROUP_PERFORMANCE_REPORT " +
    "WHERE  Clicks > 100 " +
    "DURING 20200614,20200914");

  var rows = report.rows();
  while (rows.hasNext()) {
    var row = rows.next();
    
    var name = row["AdGroupName"];
    Logger.log('AdGroup Name: ' + name);
    var allConversionValue = row["AllConversionValue"];
    Logger.log('All Conv. Value: ' + allConversionValue);
    var cost = row["Cost"];
    Logger.log('Cost: ' + cost);
    var clicks = row["Clicks"];
    Logger.log('Clicks: ' + clicks);
    var allConversionROAS = allConversionValue / cost;
    Logger.log('ROAS: ' + allConversionROAS);
    Logger.log("------\n");
  }

This is the output:

9/15/2020 10:48:57 AM
AdGroup Name: High Margin Brands 6X

9/15/2020 10:48:57 AM
All Conv. Value: 3,856.32

9/15/2020 10:48:57 AM
Cost: 763.95

9/15/2020 10:48:57 AM
Clicks: 813

9/15/2020 10:48:58 AM
ROAS: NaN

9/15/2020 10:48:58 AM
------
Message has been deleted

Petra Manos

unread,
Sep 14, 2020, 9:40:24 PM9/14/20
to Google Ads Scripts Forum
OK, I think this is because of the comma in the All Conv. Value. If I parse 3,856.32 as a float I get 3.0.

Google Ads Scripts Forum Advisor

unread,
Sep 14, 2020, 11:48:01 PM9/14/20
to adwords...@googlegroups.com
Hi Petra,

Thank you for the posting your concern.

You are correct that the comma in the returned value of All Conv. Value field is causing the NaN answer. With this, I would suggest using a javascript method, to remove the comma (,) in the returned value before dividing it with other values.

Regards,
Google Logo
Ernie John Blanca Tacata
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q24bMi5:ref

Nils Rooijmans

unread,
Sep 16, 2020, 3:01:32 AM9/16/20
to Google Ads Scripts Forum
Here's a function you can use  to remove the comma (,) in the returned value and get a Float instead of a String value:

function numericalize(string){
  return parseFloat(string.toString().replace(/\,/g, ''));

Hope this helps, 

Nils Rooijmans 
See my Google Ads Scripts FAQ to avoid the same mistakes i made: https://nilsrooijmans.com/google-ads-scripts-faq/



Reply all
Reply to author
Forward
0 new messages