How to convert micros to money values?

Are you migrating scripts from the old environment to the new one?

If so, you might want to read Yan’s question below.

Fellow list member Yan Lahud sent in this question:

“How do I convert micros into currency values? The old solution with ‘returnMoneyInMicros’ doesn’t work anymore.”
(shared with permission, question paraphrased for clarity)

Yan is right.
We have lost some functionality when trying to retrieve data from the Reports API.

In the “old” script environment we were able to retrieve data via AWQL queries and the AdsApp.report function.
That function allowed for the optional argument (‘returnMoneyInMicros’) to indicate whether or not to represent money in micros (‘1370000’) or in currency (‘1.37’).

Here’s an example:

 var report = AdsApp.report(
     'SELECT CampaignName, Cost ' +
     'FROM   CAMPAIGN_PERFORMANCE_REPORT ' +
     'DURING 20220101,20220610', {
       includeZeroImpressions: true,
       returnMoneyInMicros: false
     });

The report would contain the cost data of your campaigns, in nicely readable currency values.

BAD NEWS:
This optional argument (‘returnMoneyInMicros’) is not allowed when the query uses GAQL. From now on, all money values are represented in micros.
Note, when running an old fashioned AWQL query it is automatically converted to a GAQL query and thus won’t support the ‘returnMoneyInMicros’ argument anymore.

If you want to get the same result as before we need to convert the money micros values into currency values.
This is easily accomplished by dividing the micro value by 1000000.

    function main() {
       
      var query =
          "SELECT " +
          "campaign.name, metrics.cost_micros " +
          "FROM campaign "+
          "WHERE campaign.status = ENABLED AND segments.date DURING THIS_MONTH";
     
      try {
        var result = AdsApp.search(query);
    
        while (result.hasNext()) {
          var row = result.next();
          var campaignName = row.campaign.name;
          var costInMicros = row.metrics.costMicros;
          var costInCurrency = convertMicrosToCurrency(costInMicros, true);
          Logger.log("Campaign name: '%s' | Cost in Micros: %s | Cost in currency: %s", campaignName, costInMicros, costInCurrency);
        }    
      } catch (e) {
       Logger.log("### ERROR: "+e);
      }    
    }
    
    
    function convertMicrosToCurrency(micros, rounded) {
      if (rounded == true) { 
        // return currency value with 2 decimals
        return Number(Math.round(micros/1000000+'e2')+'e-2');
      } else if (rounded == false)  {
        // return currency value without any rounding
        return micros/1000000;
      }
    }

NOTE:
to confuse us a little bit more, Google decided to return _all_ money values in “micros” format, despite the fact that _some_ fields do contain the suffix “micros” (ex. metrics.cost_micros), while others don’t (ex. metrics.average_cpc).

Happy scripting.

 

Join thousands of PPC geeks who already have access:

If the button above isn’t working for you, you can sign up here to get access.