|
| 1 | +// outcome: build queries, grab data, push to sheet |
| 2 | +// |
| 3 | +// copy https://docs.google.com/spreadsheets/d/1JS3rce2eoDEfxI0htm19SwNImioiNkzsrndn-CHw5Cs/copy |
| 4 | +// enter the URL of YOUR sheet below in the line that starts 'let ss =' |
| 5 | +// |
| 6 | +// name your script: WebSavvy: pMax charts v8 (updates daily) |
| 7 | +// |
| 8 | +// name your sheet: {client name or code} - pMax charts v8 (updates daily) |
| 9 | +// |
| 10 | + |
| 11 | +function main() { |
| 12 | + |
| 13 | + let ss = SpreadsheetApp.openByUrl(' enter your URL here (between single quotes) after copying the sheet above '); |
| 14 | + |
| 15 | + // define commonly used query elements. wrap with spaces for safety |
| 16 | + let segDate = ' segments.date '; |
| 17 | + let title = ' segments.product_title '; |
| 18 | + let name = ' campaign.name '; |
| 19 | + let impr = ' metrics.impressions '; |
| 20 | + let clicks = ' metrics.clicks '; |
| 21 | + let cost = ' metrics.cost_micros '; |
| 22 | + let conv = ' metrics.conversions '; |
| 23 | + let value = ' metrics.conversions_value '; |
| 24 | + let views = ' metrics.video_views '; |
| 25 | + let cpv = ' metrics.average_cpv '; |
| 26 | + let cType = ' campaign.advertising_channel_type '; |
| 27 | + let agStrength = ' asset_group.ad_strength '; |
| 28 | + let agStatus = ' asset_group.status '; |
| 29 | + let agName = ' asset_group.name '; |
| 30 | + let lgName = ' asset_group_listing_group_filter.case_value.product_custom_attribute.value '; |
| 31 | + let lgType = ' asset_group_listing_group_filter.type '; |
| 32 | + let pMaxOnly = ' AND campaign.advertising_channel_type = "PERFORMANCE_MAX" '; |
| 33 | + let date30 = ' segments.date DURING LAST_30_DAYS '; |
| 34 | + let agFilter = ' asset_group_listing_group_filter.type != "SUBDIVISION" '; |
| 35 | + let order = ' ORDER BY campaign.name '; |
| 36 | + |
| 37 | + let cd = [segDate, name, cost, conv, value, views, cpv, impr, cType] |
| 38 | + let campQuery = 'SELECT ' + cd.join(',') + |
| 39 | + ' FROM campaign ' + |
| 40 | + ' WHERE ' + date30 + pMaxOnly + order ; |
| 41 | + |
| 42 | + let p = [name, title, cost, conv, value, cType] |
| 43 | + let pQuery = 'SELECT ' + p.join(',') + |
| 44 | + ' FROM shopping_performance_view ' + |
| 45 | + ' WHERE ' + date30 + pMaxOnly + order ; |
| 46 | + |
| 47 | + let pd = [segDate, name, title, cost, conv, value, impr, cType] |
| 48 | + let prodQuery = 'SELECT ' + pd.join(',') + |
| 49 | + ' FROM shopping_performance_view ' + |
| 50 | + ' WHERE ' + date30 + pMaxOnly + order ; |
| 51 | + |
| 52 | + let ag = [segDate, name, agName, agStrength, agStatus, lgName, lgType, impr, clicks, cost, conv, value] |
| 53 | + let agQuery = 'SELECT ' + ag.join(',') + |
| 54 | + ' FROM asset_group_product_group_view ' + |
| 55 | + ' WHERE ' + date30 + ' AND ' + agFilter |
| 56 | + |
| 57 | + runReport(campQuery, ss.getSheetByName('raw_campaigns')); |
| 58 | + runReport(pQuery, ss.getSheetByName('raw_products_tot')); |
| 59 | + runReport(prodQuery, ss.getSheetByName('raw_products')); |
| 60 | + runReport(agQuery, ss.getSheetByName('raw_asset_groups')); |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +function runReport(q,sh) { |
| 65 | + const report = AdsApp.report(q); |
| 66 | + report.exportToSheet(sh); |
| 67 | +} |
0 commit comments