How to exclude location with google ads script

59 views
Skip to first unread message

Nanon Bindusa

unread,
Jul 24, 2024, 3:34:26 PM7/24/24
to Google Ads Scripts Forum
I want to exclude location from google ads script but  log is tell
Ga: Invalid input: { "id": "2840" }. Expected an ExcludedLocation or an object with fields { id: an integer } at main (Code:13:14)
what should I do next

my script is 

function main() {
  var campaignId = '...;
  var excludedLocationId = '...';

  var campaignIterator = AdsApp.campaigns()
                               .withIds([campaignId])
                               .get();
 
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();

    // Exclude the specified location
    campaign.excludeLocation(excludedLocationId);

    Logger.log('Excluded United States from campaign ID: ' + campaignId);
  } else {
    Logger.log('Campaign not found with ID: ' + campaignId);
  }
}

Google Ads Scripts Forum

unread,
Jul 24, 2024, 5:05:52 PM7/24/24
to Google Ads Scripts Forum
Hi,

Thank you for reaching out to the Google Ads Scripts support team.

I would like to inform you that the function “campaign.excludeLocation(excludedLocationId)” expects an integer input. To resolve the error, I would suggest that you replace your code “var excludedLocationId = '2840';” with “var excludedLocationId = 2840;”.

I hope this helps! Kindly get back to us with the below details if you still face any issues.
  • Google Ads account ID/CID
  • Name of the affected script
You can share the requested details via Reply privately to the author option or a direct private reply to this email. If this option is not available on your end, you may send it through our email (googleadsscr...@google.com) instead.

Thanks,
Google Ads Scripts team

Nils Rooijmans

unread,
Jul 25, 2024, 2:37:50 AM7/25/24
to Google Ads Scripts Forum
you could also convert the string to an integer before passing it to 'campaign.excludeLocation(excludedLocationId)' function.

Here's one way to do so:

let str = "123"; 
let num = parseInt(str); 
console.log(num); // Output: 123


Hope this helps,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Google Ads Scripts Forum Advisor

unread,
Jul 25, 2024, 6:51:24 AM7/25/24
to adwords...@googlegroups.com
Hi,

Thank you Nils for providing the solution.

I would suggest you follow the solution provided by Nils and get back to us with the below details if you still face any issues.
  • Google Ads account ID/CID
  • Name of the affected script
You can share the requested details via Reply privately to the author option or a direct private reply to this email.
 
This message is in relation to case "ref:!00D1U01174p.!5004Q02vE7ma:ref" (ADR-00259508)

Thanks,
 
Google Logo Google Ads Scripts Team


Reply all
Reply to author
Forward
0 new messages