Best way to replace currentAccount = AdsApp.currentAccount() to get selected campaigns

94 views
Skip to first unread message

Thomas Jönsson

unread,
Mar 3, 2020, 8:13:20 AM3/3/20
to Google Ads Scripts Forum
Howdy and thanks for a good forum.

So I'm getting data for a lot of different periods. This was setup using AdsApp.currentAccount() but I would now like to run it on just a selection of campaigns. I have below tried using campaign name in a separate function but of course I can use labels instead if it's considered better, what would you way is best solution?

Previous working code:

     var currentAccount = AdsApp.currentAccount();
     var day_2_1_ago_stats= currentAccount.getStatsFor(date_range2to1Daysago);
     var day_2_1_ago_cost = Math.round(day_2_1_ago_stats.getCost());
     var day_32_ago_stats = currentAccount.getStatsFor(date_range32to32Daysago);
     var acc_statsYesterday = currentAccount.getStatsFor('YESTERDAY');  
     var acc_costYesterday = Math.round(acc_statsYesterday.getCost());
     var day_32_ago_impressions = Math.round(day_32_ago_stats.getImpressions());
     var yesterday_impresssion = Math.round(acc_statsYesterday.getImpressions());
...

Seems best option would be to build a function to collect data for each dates and for each type of stats? I can only return for example cost in one function and to return impressions I have to build another function, correct?


Considered solution could be something like:

 var day_2_1_ago_cost = campaignCosts(campaignName,date_range2to1Daysago) 


function campaignCosts(campaignName,date_range2to1Daysago) {

          var campaignIterator = AdsApp.campaigns()
    .withCondition("CampaignName CONTAINS '" + campaignName + "'")
      .get();
  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var stats = campaign.getStatsFor(date_range2to1Daysago);
    var cost = stats.getCost();
    return cost;
    }
 }



Secondly in the above function the date range won't work, it's setup like:
      var date_range2to1Daysago = date2daysago + ', ' + yesterday;
and will look like: 20200301, 20200302

Is there a way to "convert" these to the correct format so it works within var stats = campaign.getStatsFor(date_range2to1Daysago);
using the date as two separate strings, "START" and "END" works, like this: var stats = campaign.getStatsFor(date2daysago,yesterday);
However I've already created lots of different date ranges already in use and I prefer to continue use these.

Thanks in advance

Google Ads Scripts Forum Advisor

unread,
Mar 3, 2020, 12:53:30 PM3/3/20
to adwords-scripts+apn2wqe6rgx71nt8...@googlegroups.com, adwords-scripts+apn2wqe6rgx71nt8...@googlegroups.co, adwords...@googlegroups.com
Hi Thomas,

Your solution is a reasonable approach. You may also consider using reports, specifically the campaign performance report.

If I understand your second question, the date range:
  
  var date_range2to1Daysago = date2daysago + ', ' + yesterday;

Will return a string of the form, "yyyymmdd, yyyymmdd". Since the getStats date range expects two separate date strings as arguments. You could use the split function to cast date_range2to1Daysago as a two element array (split by the comma and space):

  var new_date_format = date_range2to1Daysago.split(", "); // will be of form: [ 'yyyyMMdd' , 'yyyyMMdd' ]
  var stats = entity.getStatsFor(new_date_format[0]. new_date_format[1]); // where entity is your campaign / account

Regards,
Matt
Google Ads Scripts Team


ref:_00D1U1174p._5001UXTvtX:ref
Reply all
Reply to author
Forward
0 new messages