function main() { let ss = SpreadsheetApp.openByUrl('https://docs.google.com/test'); // Enter the URL of your sheet between the '' // Define commonly used query elements. Wrap with spaces for safety let segDate = ' segments.date '; let title = ' segments.product_title '; let id = ' segments.product_item_id '; let name = ' campaign.name '; let impr = ' metrics.impressions '; let clicks = ' metrics.clicks '; let cost = ' metrics.cost_micros '; let conv = ' metrics.conversions '; let value = ' metrics.conversions_value '; let views = ' metrics.video_views '; let cpv = ' metrics.average_cpv '; let cType = ' campaign.advertising_channel_type '; let agStrength = ' asset_group.ad_strength '; let agStatus = ' asset_group.status '; let agName = ' asset_group.name '; let lgName = ' asset_group_listing_group_filter.case_value.product_custom_attribute.value '; let lgType = ' asset_group_listing_group_filter.type '; let pMaxOnly = ' AND campaign.advertising_channel_type = "PERFORMANCE_MAX" '; let date30 = ' segments.date DURING LAST_30_DAYS '; let agFilter = ' asset_group_listing_group_filter.type != "SUBDIVISION" '; let order = ' ORDER BY campaign.name '; let cd = [segDate, name, cost, conv, value, views, cpv, impr, cType]; let campQuery = 'SELECT ' + cd.join(',') + ' FROM campaign ' + ' WHERE ' + date30 + pMaxOnly + order; let p = [name, title, cost, conv, value, impr, cType]; let pQuery = 'SELECT ' + p.join(',') + ' FROM shopping_performance_view ' + ' WHERE ' + date30 + pMaxOnly + order; let ag = [segDate, name, agName, agStrength, agStatus, lgName, lgType, impr, clicks, cost, conv, value]; let agQuery = 'SELECT ' + ag.join(',') + ' FROM asset_group_product_group_view ' + ' WHERE ' + date30 + ' AND ' + agFilter; runReport(campQuery, ss.getSheetByName('raw_campaigns')); runReport(pQuery, ss.getSheetByName('raw_products_tot')); runReport(agQuery, ss.getSheetByName('raw_asset_groups')); } function runReport(q, sh) { const report = AdsApp.report(q); report.exportToSheet(sh); }