Ads script for price extension through sheets

192 views
Skip to first unread message

Deepak Thirumurugan

unread,
Mar 2, 2022, 6:01:22 AM3/2/22
to Google Ads Scripts Forum
Hi,

I was extensively searching for script that can add price extensions to campaign by reading data from sheets but could not find any. But online I found a script that applies call out extension to ad groups by loading data from sheets. Could anyone please help me on changed script for price extension. Please find below the script for callout extension!
Any help is highly appreciated

// 
  1.  
  2.  
  3. function main() {
  4.     var callouts = load_data(SPREADSHEET_URL);
  5.     Logger.log(callouts)
  6.     var callouts_objects = create_callouts(callouts);
  7.     var apply_results = apply_callouts(callouts);
  8. }
  9.  
  10. function apply_callouts(callouts) {
  11.     var results = new Array();
  12.     for (var i = 0; i < callouts.length; i++) {
  13.         if (!callouts[i]["Status"]) continue;
  14.  
  15.         var campaign_filter = callouts[i]["Campaign Contains"].toLowerCase();
  16.         var ad_group_filter = callouts[i]["AdGroup Contains"].toLowerCase();
  17.  
  18.         campaign_filter = campaign_filter == "any" ? false : campaign_filter;
  19.         ad_group_filter = ad_group_filter == "any" ? false : campaign_filter;
  20.  
  21.         var ad_groups_selector = AdsApp.adGroups();
  22.  
  23.         if (campaign_filter) {
  24.             ad_groups_selector.withCondition("CampaignName CONTAINS_IGNORE_CASE '" + campaign_filter + "'");
  25.         }
  26.         if (ad_group_filter) {
  27.             ad_groups_selector.withCondition("Name CONTAINS_IGNORE_CASE '" + ad_group_filter + "'");
  28.         }
  29.         var adGroupIterator = ad_groups_selector.get();
  30.  
  31.         while (adGroupIterator.hasNext()) {
  32.             ad_group = ad_groups_selector.next();
  33.  
  34.             var operation = ad_group.addCallout(callouts[i]["Callout"]);
  35.  
  36.  
  37.             result = {
  38.                 "AdGroupName": ad_group.getName(),
  39.                 "Campaign Contains": campaign_filter,
  40.                 "AdGroup Contains": ad_group_filter,
  41.                 "Result": ad_group.getResult(),
  42.                 "Error": ad_group.getErrors()
  43.  
  44.             }
  45.  
  46.             results.push(result);
  47.  
  48.  
  49.  
  50.         }
  51.     }
  52.  
  53.     return results;
  54.  
  55. }
  56.  
  57.  
  58. function create_callouts(callouts) {
  59.     var results = new Array();
  60.     for (var i = 0; i < callouts.length; i++) {
  61.  
  62.         var calloutBuilder = AdsApp.extensions().newCalloutBuilder();
  63.  
  64.         var mobile_pref = callouts[i]["Mobile Preffered"].toLowerCase() == "any" ? true : false;
  65.         // Create a callout operation.
  66.         var calloutOperation = calloutBuilder
  67.             .withText(callouts[i]["Callout"]) // required
  68.             .withMobilePreferred(mobile_pref) // optional
  69.             .build();
  70.  
  71.         // Optional: examine the outcome. The call to isSuccessful()
  72.         // will block until the operation completes.
  73.         if (calloutOperation.isSuccessful()) {
  74.             // Get the result.
  75.             var callout = calloutOperation.getResult();
  76.             results.push({
  77.                 "Status": true,
  78.                 "Callout": callout,
  79.                 "Campaign Contains": callouts[i]["Campaign Contains"],
  80.                 "AdGroup Contains": callouts[i]["AdGroup Contains"]
  81.             });
  82.         } else {
  83.             // Handle the errors.
  84.             var errors = calloutOperation.getErrors();
  85.             results.push({
  86.                 "Status": true,
  87.                 "Error": errors
  88.             });
  89.         }
  90.  
  91.     }
  92.  
  93.     return results;
  94. }
  95.  
  96. function load_data(url) {
  97.  
  98.     var reportSheet = SpreadsheetApp.openByUrl(url);
  99.     var rows = reportSheet.getDataRange();
  100.  
  101.     var numRows = rows.getNumRows();
  102.     var numCols = rows.getNumColumns();
  103.     var values = rows.getValues(); // values[row_index][column_index]
  104.  
  105.     var row = values[0];
  106.     var headerNames = new Array();
  107.  
  108.     for (var i = 0; i < numCols; i++) {
  109.         var columns_name = row[i];
  110.         headerNames.push(columns_name);
  111.     }
  112.  
  113.     var data = new Array();
  114.     for (var row_index = 1; row_index < numRows; row_index++) {
  115.         var row_data = {};
  116.         if (!values[row_index]) break;
  117.         for (var col_index = 0; col_index < numCols; col_index++) {
  118.             var cell_data = values[row_index][col_index];
  119.             if (!cell_data) continue;
  120.             row_data[headerNames[col_index]] = values[row_index][col_index];
  121.         }
  122.         if (!size_dict(row_data)) continue;
  123.         data.push(row_data);
  124.     }
  125.  
  126.     return data;
  127.  
  128. }
  129.  
  130. function size_dict(d) {
  131.     c = 0;
  132.     for (i in d) ++c;
  133.     return c
  134. }

Thanks in advance!!

Google Ads Scripts Forum Advisor

unread,
Mar 3, 2022, 5:16:10 AM3/3/22
to adwords...@googlegroups.com
Hi Deepak,

Thank you for reaching out to us.

With regard to your concern, I'm afraid the script will not work currently as the team is currently working on to update Google Ads Scripts to support asset-based extensions. I humbly ask for your continued patience and be on the lookout for new updates and releases in scripts through our blog in the meantime.

Regards,
Google Logo
Teejay Wennie
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2Xket7:ref
Reply all
Reply to author
Forward
0 new messages