javascriptadsgoogle-ads-apiairtableadwords-budgetservice

How to get total billed cost from a google ads account using a script?


Does anybody have some sample code that shows how to get the daily, total billed cost (across all campaigns) of a google ads account?

I have been looking for a sample for a while but could not find where you can get the total cost/billed cost for the day (or for a certain period of time).

I am doing this in Js but a sample in any language is okay. I would like to send this data to an airtable base, so if you have any suggestions on how that can be done too, that would be great!

Thanks in advance!


Solution

  • if anyone is looking for a similar solution, found one that works. It gets the total billed cost between the START_DATE and END_DATE which you can specify based on your needs. hope this helps!

    function main() {
      const campaign = getCampaign(CAMPAIGN_NAME);
      const costSoFar = campaign.getStatsFor(
            getDateStringInTimeZone('yyyyMMdd', START_DATE),
            getDateStringInTimeZone('yyyyMMdd', END_DATE)).getCost();
      Logger.log('cost so far: %d', costSoFar)
    }
    
    function getCampaign(campaignName) {
        const selectors = [AdsApp.campaigns(), AdsApp.videoCampaigns(),
            AdsApp.shoppingCampaigns()];
        for (const selector of selectors) {
          const campaignIter = selector
              .withCondition(`CampaignName = "${campaignName}"`)
              .get();
          if (campaignIter.hasNext()) {
            return campaignIter.next();
          }
        }
        throw new Error(`Could not find specified campaign: ${campaignName}`);
      }