Year over year/ Month to Date campaign performance data pull

166 views
Skip to first unread message

QF

unread,
May 10, 2019, 3:35:45 PM5/10/19
to Google Ads Scripts Forum
Hi everyone, I wondering if it was possible to code a MTD data pull from the previous year. My end goal is to compare this year's data to last year's data in the same MTD time frame. Any help or suggestions will be greatly appreciated. Thanks!

googleadsscrip...@google.com

unread,
May 10, 2019, 4:26:09 PM5/10/19
to QF via Google Ads Scripts Forum, Google Ads Scripts Forum
Hello,

Yes, this is possible. Google Ads reports uses AWQL to query reports. The DURING clause can accept two dates representing a date range, formatted as an 8 digit integer (YYYYMMDD). This guide provides information on working with dates and times in Ads scripts. Below is a sample script that pulls cost data at the campaign level from the campaign performance report. See comments in the code for details:

function main() {
  
  
var today = new Date(); //Store today's date
  
var timeZone = AdsApp.currentAccount().getTimeZone(); //Get timezone of current account
  
var formattedToday = parseInt(Utilities.formatDate(today, timeZone, 'yyyyMMdd')); //Format today's date to meet AWQL requirements
  
var formattedFirstDayOfMonth = parseInt(Utilities.formatDate(today, timeZone, 'yyyyMM01')) //Format first of the month to meet AWQL requirements
  
  
var query = AdsApp.report("SELECT CampaignName, Cost FROM CAMPAIGN_PERFORMANCE_REPORT DURING " + formattedFirstDayOfMonth + "," + formattedToday); //Sample query
  
  
var rows = query.rows()
  
  
//Logs campaign name and cost of the campaign for month to date
  
while(rows.hasNext()) {
  
    
var row = rows.next();
    
Logger.log("Campaign: " + row['CampaignName'] + " cost: " + row['Cost']);    
      
  
}
}

Regards,
Matt
Google Ads Scripts Team

Hi everyone, I wondering if it was possible to code a MTD data pull from the previous year. My end goal is to compare this year's data to last year's data in the same MTD time frame. Any help or suggestions will be greatly appreciated. Thanks!

--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scripts+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/b496cfc6-2484-400a-b76e-570b4e9db1ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

QF

unread,
May 13, 2019, 2:02:33 AM5/13/19
to Google Ads Scripts Forum
Hi Matt, I've tried tweaking the code to pull last year's MTD data but keep getting an error. Posting the code & error down below. If you could point me in the right direction that'd be great. Thank you!

  var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
  var minus = MILLIS_PER_DAY * 365
  var now = new Date();
  var timeZone = AdsApp.currentAccount().getTimeZone(); 
  var firstDay = new Date(now.getFullYear(), now.getMonth(), 1);
  var today = new Date(now.getTime());
      
  var from = firstDay - minus;
  var to = today - minus;
   
  Logger.log('Campaign Data: ' + from + ' until: ' + to);
  
  
        var report = AdWordsApp.report(     line 47

            "SELECT AccountDescriptiveName, CampaignName, Clicks, AverageCpc, Ctr, Cost, SearchImpressionShare, SearchRankLostImpressionShare, SearchBudgetLostImpressionShare, Impressions FROM CAMPAIGN_PERFORMANCE_REPORT " +
            'WHERE  Impressions > 0 ' + 'DURING ' + Utilities.formatDate(from, timeZone, 'yyyyMMdd') + ','
            + Utilities.formatDate(to, timeZone, 'yyyyMMdd'));


5/13/2019 1:58:53 AMCannot find method formatDate(number,string,string). (file Code.gs, line 47)
    



On Friday, May 10, 2019 at 4:26:09 PM UTC-4, googleadsscripts-forumadvisor wrote:
Hello,

Yes, this is possible. Google Ads reports uses AWQL to query reports. The DURING clause can accept two dates representing a date range, formatted as an 8 digit integer (YYYYMMDD). This guide provides information on working with dates and times in Ads scripts. Below is a sample script that pulls cost data at the campaign level from the campaign performance report. See comments in the code for details:

function main() {
  
  
var today = new Date(); //Store today's date
  
var timeZone = AdsApp.currentAccount().getTimeZone(); //Get timezone of current account
  
var formattedToday = parseInt(Utilities.formatDate(today, timeZone, 'yyyyMMdd')); //Format today's date to meet AWQL requirements
  
var formattedFirstDayOfMonth = parseInt(Utilities.formatDate(today, timeZone, 'yyyyMM01')) //Format first of the month to meet AWQL requirements
  
  
var query = AdsApp.report("SELECT CampaignName, Cost FROM CAMPAIGN_PERFORMANCE_REPORT DURING " + formattedFirstDayOfMonth + "," + formattedToday); //Sample query
  
  
var rows = query.rows()
  
  
//Logs campaign name and cost of the campaign for month to date
  
while(rows.hasNext()) {
  
    
var row = rows.next();
    
Logger.log("Campaign: " + row['CampaignName'] + " cost: " + row['Cost']);    
      
  
}
}

Regards,
Matt
Google Ads Scripts Team

googleadsscrip...@google.com

unread,
May 13, 2019, 12:46:30 PM5/13/19
to QF via Google Ads Scripts Forum, Google Ads Scripts Forum
Hello,

The method, formatDate, expects parameters with types: date object, string, string. You create date objects in your code, but the arithmetic in the following lines:

  var from = firstDay - minus;
  var to = today - minus;

has the number 'minus' subtracting from the date objects 'firstDay' and 'today'. So, the variables 'from' and 'to' are given type, number, which is not expected by formateDate. The arithmetic should be a part of the Date argument as shown here.

Regards,
Matt
Google Ads Scripts Team

Reply all
Reply to author
Forward
0 new messages