YoY & MoM Comparison

101 views
Skip to first unread message

Henry Freedman

unread,
Feb 22, 2021, 5:46:39 PM2/22/21
to Google Ads Scripts Forum
Hey friends,

I'm trying to make a script that will help to identify performance changes over time in accounts. I've gotten it to work for the pre-defined times, but I'd like to be able to also do month over month and year over year comparisons. Here's a snip of the script so far: 

var outputSheet = Employee_URL.getSheetByName("Script Output"); 
outputSheet.clearContents();
outputSheet.getRange(1, 1, output.length, output[0].length).setValues(output);

var mccAccount = AdsManagerApp.ManagedAccount;

 

  while (Iterator.hasNext()) {
    var account = Iterator.next();
    AdsManagerApp.select(account);
    var statsM = account.getStatsFor('THIS_MONTH');
    var stats30 = account.getStatsFor('LAST_30_DAYS');
    var stats7 = account.getStatsFor('LAST_7_DAYS');
    var stats1 = account.getStatsFor('YESTERDAY');
     
    outputSheet.appendRow([account.getCustomerId(), statsM.getCost(), stats30.getCost(), stats7.getCost(), stats1.getCost(), account.getCurrencyCode()]);




What is the easiest way to compare the last 30 days spend to the previous 30 days & the same 30 day period last year so I can add as more columns to my outputSheet.appendRow? Thanks for your time. 

Google Ads Scripts Forum Advisor

unread,
Feb 23, 2021, 12:31:41 AM2/23/21
to adwords...@googlegroups.com

Hi Henry,

Thanks for raising this to us.

The easiest way to determine those stats depending on the date range is by using the getStatsFor() method. To compare the last 30 days, you can simply use the getStatsFor(dateRange) method having a predefined date range. However, to compare the previous 30 days, you can use the getStatsFor(dateFrom, dateTo) method wherein you would need to define your dateFrom and dateTo ranges.

Regards,

Google Logo
Mark Kevin Albios
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2CTi10:ref

Henry Freedman

unread,
Feb 23, 2021, 12:47:37 AM2/23/21
to Google Ads Scripts Forum
Hi Mark,

Thanks for your quick reply! I think I should have been more clear, I am trying to have this script run every morning on it's own so I think I can't use the dateFrom, dateTo format. How do I define the comparison window as the 30 days before the last 30 days & the same 30 day window of last year without putting in a specific date?

Google Ads Scripts Forum Advisor

unread,
Feb 23, 2021, 2:57:24 AM2/23/21
to adwords...@googlegroups.com

Hi Henry,

 

Harry here, teammate of Mark on the Google Ads Scripts Team. Allow me to assist you this time.

 

It is not possible to define the comparison window as the 30 days before the last 30 days & the same 30 day window of last year by using the LAST_30_DAYS date range. This said range will only filter data in between of the last 30 days not including today. Please see Date ranges for your reference.

 

With that said, you would have to use the getStatsFor(dateFrom, dateTo). If you would like to do this dynamically, then you would just have to make use of the JavaScript Date object. I haven't done this myself, but this should be possible. For example, generate the from and to date ranges for the month of January, February and March respectively.

 

Thanks,

Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2CTi10:ref

Henry Freedman

unread,
Feb 23, 2021, 12:10:24 PM2/23/21
to Google Ads Scripts Forum
I think I've resolved this using the date object, thank you both for your time!
Reply all
Reply to author
Forward
0 new messages