Script: Geographic data from multiple time periods

A student in my ‘Scripts & Sheets Workshop’ wanted a script to get cost & conversion data at the country level across her account.

I thought the script might be useful & of interest to you as it contains a different way of creating different date ranges & one I hadn’t used before.

I’m sure the coding purists will hate this one, but I fetch the date range to use from a tab in the sheet

(copy the template sheet before you start & enter the URL of your sheet in the ss variable. Template is https://docs.google.com/spreadsheets/d/1s3EkhKOHjfJHFHFIPc9-Y5K2mV6N100P-1ixm9cOtzg/ )


This isn’t a super elegant way of coding - that’s not the point here - the point is it’s often easier to do a task in a sheet if you’re new to coding. And this example will help you grow what you know to be possible & extend your capabilities.

Such as here, we’re finding a data n days ago, formatting it & then creating the string to use in the SQL statement.

The code uses getRange().getValue() to fetch the value in the sheet

AND it uses something called a ‘named range’ to specify where to fetch that data from (rather than using a cell reference like D3).

So if you’re much more comfortable working in sheets than in JavaScript, tuck this idea away in your brain to copy next time you need it.

Happy Coding!

function main() {
  
  // make a copy of the template sheet: https://docs.google.com/spreadsheets/d/1s3EkhKOHjfJHFHFIPc9-Y5K2mV6N100P-1ixm9cOtzg/copy
  //
  let ss = SpreadsheetApp.openByUrl('');  // enter sheet url between the quotes
  let sheet = ss.getSheetByName('dates');
  
  // get named ranges. 30 days not needed
  let range90 = sheet.getRange('between90').getValue();
  let range180 = sheet.getRange('between180').getValue(); 
  let range365 = sheet.getRange('between365').getValue();   
  
  // 30 days
  let query = AdsApp.report(
    `SELECT geographic_view.country_criterion_id, segments.geo_target_region, metrics.cost_micros, metrics.conversions
    FROM geographic_view
    WHERE segments.date DURING LAST_30_DAYS ` ); 
  query.exportToSheet(ss.getSheetByName('30')); 
  
  // 90 days
  let query90 = AdsApp.report(
    `SELECT geographic_view.country_criterion_id, segments.geo_target_region, metrics.cost_micros, metrics.conversions
    FROM geographic_view
    WHERE segments.date BETWEEN ` + range90 ); 
  query90.exportToSheet(ss.getSheetByName('90')); 

  // 180 days
  let query180 = AdsApp.report(
    `SELECT geographic_view.country_criterion_id, segments.geo_target_region, metrics.cost_micros, metrics.conversions
    FROM geographic_view
    WHERE segments.date BETWEEN ` + range180 ); 
  query180.exportToSheet(ss.getSheetByName('180')); 

  // 365 days
  let query365 = AdsApp.report(
    `SELECT geographic_view.country_criterion_id, segments.geo_target_region, metrics.cost_micros, metrics.conversions
    FROM geographic_view
    WHERE segments.date BETWEEN ` + range365 ); 
  query365.exportToSheet(ss.getSheetByName('365')); 
  
} // end main


/*   other fields you can use are:

geo_target_airport
geo_target_canton
geo_target_city
geo_target_county
geo_target_district
geo_target_metro
geo_target_most_specific_location
geo_target_postal_code
geo_target_province
geo_target_region
geo_target_state

*/