AI-generated Key Takeaways
-
These Google Ads scripts demonstrate how to target, retrieve, and remove location targeting for campaigns, specifically using France as an example with location ID 2250.
-
targetFrance
applies a location target (France) and a bid modifier of +50% to a specified campaign. -
getTargetedLocations
returns a list of all locations targeted by a given campaign. -
untargetFrance
removes the location target for France from a specified campaign.
Target a campaign for a country
function targetFrance(campaignName) { const campaignIterator = AdsApp.campaigns() .withCondition(`campaign.name = "${campaignName}"`) .get(); if (!campaignIterator.hasNext()) { throw new Error(`No campaign with name "${campaignName}" found`); } const campaign = campaignIterator.next(); // Target France (location id = 2250) and set a bid modifier of +50%. See // https://developers.google.com/google-ads/api/reference/data/geotargets // for details. campaign.addLocation(2250, 1.5); }
Get the list of locations targeted by a campaign
function getTargetedLocations(campaignName) { const campaignIterator = AdsApp.campaigns() .withCondition(`campaign.name = "${campaignName}"`) .get(); if (!campaignIterator.hasNext()) { throw new Error(`No campaign with name "${campaignName}" found`); } const campaign = campaignIterator.next(); return campaign.targeting().targetedLocations().get(); }
Remove a targeted location from a campaign
function untargetFrance(campaignName) { const campaignIterator = AdsApp.campaigns() .withCondition(`campaign.name = "${campaignName}"`) .get(); if (!campaignIterator.hasNext()) { throw new Error(`No campaign with name "${campaignName}" found`); } const campaign = campaignIterator.next(); // Remove targeting for France (location id = 2250). See // https://developers.google.com/google-ads/api/reference/data/geotargets // for details. const targetedLocationIterator = AdsApp.targeting() .targetedLocations() .withIds([[campaign.getId(), 2250]]).get(); if (targetedLocationIterator.hasNext()) { targetedLocationIterator.next().remove(); } }