How to remove locations from campaign

174 views
Skip to first unread message

Paco Banuelos

unread,
Sep 20, 2022, 12:05:13 AM9/20/22
to Google Ads Scripts Forum
I am trying to retrieve and delete locations from a campaign so I can add new ones via a spreadsheet.

This throws an error "TypeError: Cannot find function targeting in object [CampaignSelector]. (file Code.gs, line 17)"

function update_locations(locations){

 var campaign = AdsApp.campaigns().withIds([999999999]);
 var locations = campaign.targeting().targetedLocations().get();
 while (locations.hasNext()) {
   var location = locations.next();
 
 }
   
 
}

Martijn Kraan

unread,
Sep 20, 2022, 4:09:15 PM9/20/22
to Google Ads Scripts Forum
Hi Paco,

I'm not from the official Google Ads Scripts Support team, but there's some room for improvement in your code.
Maybe you can try the code below?

If you want to remove the applied locations, you can extend the script with "location.remove();" in the while loop (see screenshot below).

Good luck!
Gr, Martijn

------------------------------

function main() {

    var campaign = AdsApp
        .campaigns()
        .withIds([980722014])
        .get();

    var locations = campaign
        .next()
        .targeting()
        .targetedLocations()

        .get();

    while (locations.hasNext()) {
        var location = locations.next();
        console.log(location.getName());
    }
}

------------------------------

remove_applied_locations.png

Paco Banuelos

unread,
Sep 20, 2022, 5:21:28 PM9/20/22
to Google Ads Scripts Forum
Thanks! This worked, quick follow up, I would like to add another function to add a new set of locations.

I already figured out how to load data from a google sheet with locations (criteriaID)

I am trying to add these to one campaign. Right now I have tried the following (new_locations is the loaded data, CriteriaID is the name of the column with locations)

function update_locations(new_locations){
  for(var i = 0; i < new_locations.lenght; i++){
    var operation = campaign.addLocation(new_locations[i]["CriteriaID"]);

Thanks!

Martijn Kraan

unread,
Sep 21, 2022, 7:16:46 AM9/21/22
to Google Ads Scripts Forum
Ha Paco,

Seems like a good setup. Is the update_locations function working?

Paco Banuelos

unread,
Sep 21, 2022, 11:58:30 AM9/21/22
to Google Ads Scripts Forum
It doesn't,  it deletes the locations but doesn't add new ones.  here's the whole script, the spreadsheet has a column with header called CriteriaID with Google's location ids - thanks!

var SPREADSHEET_URL = "https://docs.google.com/spreadsheets/d/55555/"

function main() {
    var campaign = AdsApp.videoCampaigns().withIds([55555]);
    var new_locations = load_data(SPREADSHEET_URL);
    var wipe_locations = remove_locations(campaign);
    var locations_push = update_locations(new_locations);
}

function remove_locations(campaign){
  var campaign = campaign.get().next();

  var locations = campaign.targeting().targetedLocations().get();
  while (locations.hasNext()) {
   var location = locations.next();
   location.remove();
 }
   
    }

function load_data(url){
    var reportSheet = SpreadsheetApp.openByUrl(url);
    var rows = reportSheet.getDataRange();

    var numRows = rows.getNumRows();
    var numCols = rows.getNumColumns();
    var values = rows.getValues(); //values [row_index][column_index]

    var row = values [0];
    var headerNames = new Array();

    for(var i = 0; i < numCols; i++){
        var columns_name = row[i];
        headerNames.push(columns_name);
    }

var data = new Array();
for(var row_index = 1; row_index < numRows; row_index++){
    var row_data = {};
    if(!values[row_index]) break;
    for(var col_index = 0; col_index < numCols; col_index++){
      var cell_data = values[row_index][col_index];
      if(!cell_data) continue;
      row_data[headerNames[col_index]] = cell_data;
    }
  if(!size_dict(row_data)) continue;
  data.push(row_data);
}

 return data;

}

function size_dict(d){
    c = 0;
    for (i in d) ++c;
    return c

Martijn Kraan

unread,
Sep 21, 2022, 1:34:38 PM9/21/22
to Google Ads Scripts Forum
Hi Paco,

Maybe this example script will help:

------------------------------

function main() {

    var campaign = AdsApp
        .campaigns()
        .withIds([980722014])
        .get()
        .next();

    campaign.addLocation(1010543, 1.15);

}

------------------------------

The 1.15 is the bid modifier (in this case 15%) but there are other way's to apply the addLocation method (see https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_campaign#addLocation_1)

However, when I preview this script it generates an error:

add_location_preview.png

However, if I execute the script, it does apply the location (and sais "Succesful").

add_location_execute.png

Good luck!

Paco Banuelos

unread,
Sep 21, 2022, 4:13:06 PM9/21/22
to Google Ads Scripts Forum
This works, thanks! Formatting location Id from spreadsheet seems particular but I think I have figured it out.

Google Ads Scripts Forum Advisor

unread,
Sep 22, 2022, 1:22:00 AM9/22/22
to adwords...@googlegroups.com
Hello Everyone,

This is Maia from the Google Ads Scripts Team.

@Martijn – Thank you for your insights and solutions that you have provided for Paco’s concern. Our team greatly appreciates your efforts.

@Paco – Based on the flow of this thread, it appears that the codes provided by Martijn has worked for you. Thus, would you be able to confirm if you have other concerns regarding your script/s? If so, kindly provide us the following information below privately, so that we can further investigate and replicate any specific issues:
  • Google Ads CID
  • Script Name
  • Shareable link (guide) of your spreadsheet, if applicable.
  • Screenshot that showcases the issue/errors

If the `Reply privately to author` option is not available on your end, you may send those through the email alias `googleadsscr...@google.com` instead.

Best Regards,
Google Logo
Angel Maia
Google Ads Scripts Team
 


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