Pulling all campaign proximity and location geotargets across MCC

111 views
Skip to first unread message

lea.h...@commissionsinc.com

unread,
Sep 1, 2020, 11:33:22 AM9/1/20
to Google Ads Scripts Forum
Hello,

We're looking to create a script and a report that identifies accounts where we are either missing proximity targeting or we need to add additional cities to geotargeting.

Would there be a way to repurpose the below script (found in this forum) to log all current geotargets rather than adding a proximity? This is the line that I'm interested in re-writing to log all current geotargets
        campaign.addProximity(latitude, longitude, 12, "MILES"); // set proximity  
 
wanting to incorporate something like this instead-
var proximitySelector = AdsApp.targeting()
    .targetedProximities()
  //  .withCondition("Impressions > 0")
    .forDateRange("LAST_MONTH")
    .orderBy("Clicks DESC");

var proximityIterator = proximitySelector.get();
while (proximityIterator.hasNext()) {
  var proximity = proximityIterator.next();
  …
}

into this SAMPLE SCRIPT found in forum:

var Click_Threshold = 1;
  var RepIterator = AdWordsApp.report("SELECT Id, BidModifier, CampaignName, Clicks, Cost, Conversions, CostPerConversion FROM CAMPAIGN_LOCATION_TARGET_REPORT WHERE Clicks >= 0 DURING LAST_14_DAYS");

var SPREADSHEET_URL = 'SPREADSHEET_URL';
function main() {
  var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
  var sheet = ss.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  for (var i = 0; i < data.length; i++) { // iterate through your list of accounts and locations
    var accountNum = data[i][0];
    var latitude = data[i][1];
    var longitude = data[i][2];

    var accountIterator = MccApp.accounts().withIds([accountNum]).get();

    if (accountIterator.hasNext()) {
      var account = accountIterator.next();
      MccApp.select(account);
      var accountName = account.getName() ? account.getName() : '--';
      var campaignIterator = AdWordsApp.campaigns().get();
      Logger.log('Total campaigns found : ' + campaignIterator.totalNumEntities());
      while (campaignIterator.hasNext()) { // iterate through your campaigns
        var campaign = campaignIterator.next();
        Logger.log(campaign.getName());
        campaign.addProximity(latitude, longitude, 7, "MILES"); // set proximity
      }
    }
  }
}

Thanks in advance for any help!

Lea Harper

Google Ads Scripts Forum Advisor

unread,
Sep 1, 2020, 2:37:50 PM9/1/20
to adwords...@googlegroups.com
Hi Lea,

In order to verify if a campaign has a given proximity, each proximity will have to be checked until a match (or no match) is found. You might use something along the lines of the snippet below when iterating through campaign and checking for each proximity:
 
while (campaignIterator.hasNext()) { // iterate through your campaigns
  var campaign = campaignIterator.next();
  Logger.log(campaign.getName());
  var proximities = campaign.targeting().targetedProximities().get();
  var hasProximity = 0;
  while(proximities.hasNext()) {
    var proximity = proximities.next();
    var lat = proximity.getLatitude();
    var long = proximity.getLongitude();
    if(latitude == lat && longitude == long) 
      hasProxmiity++;
  }
  if(hasProximity) {
    //Additional processing for if the campaign does have the proximity
  }
  else
    //Additional processing for if the campaign does not have the proximity
}

Thanks,
Matt
Google Ads Scripts Team
 

ref:_00D1U1174p._5004Q23wKHl:ref

lea.h...@commissionsinc.com

unread,
Sep 2, 2020, 1:53:26 PM9/2/20
to Google Ads Scripts Forum
thank you, Matt!

lea.h...@commissionsinc.com

unread,
Sep 3, 2020, 6:00:31 PM9/3/20
to Google Ads Scripts Forum
Hi Matt,

What I'm hoping to do is have an MCC script print out all proximity targets and for all enabled campaigns, then print out the location targets (cities, zips, counties, etc) - if it ends up being two separate scripts, that would be fine too. 

Currently, I have this single account script that prints out campaign name and proximity targets example: [Campaign Name] | 25MILES around (44.929413-93.357697) and I'd like to have this on MCC level and also have a column that ads Account Name or Campaign ID so I can identify which account the proximity target is in when this is run through the whole MCC.

Could you give me any pointers on converting this for MCC? Thanks!

function main() {

 
  var proximitySelector = AdsApp.targeting()
    .targetedProximities()
    .forDateRange("LAST_MONTH")
    .orderBy("Clicks DESC");

//var proximityIterator = proximitySelector.get();
//while (proximityIterator.hasNext()) {
 // var proximity = proximityIterator.next();
 //Logger.log(proximity.getName());

  var campaignIterator = AdsApp.campaigns()
  .withCondition("Status = ENABLED")
     .get();
  if (campaignIterator.hasNext()) {
   var campaign = campaignIterator.next();
    var proximityIterator = campaign.targeting().targetedProximities().get();

    while (proximityIterator.hasNext()) {
      var targetedProximity = proximityIterator.next();
      Logger.log(campaign.getName() + " | " + targetedProximity.getRadius() +
            targetedProximity.getRadiusUnits() +
            ' around (' +
            targetedProximity.getLatitude() +
            targetedProximity.getLongitude() + ')');
    }
  }

  
}
  



lea.h...@commissionsinc.com

unread,
Sep 3, 2020, 6:02:09 PM9/3/20
to Google Ads Scripts Forum
Forgot to say that I'd like the proximity targets for all account in the MCC to be uploaded into a Google Sheet

Google Ads Scripts Forum Advisor

unread,
Sep 3, 2020, 10:54:53 PM9/3/20
to adwords...@googlegroups.com
Hi Lea,

I work with Matt and let me provide support to your concern.

If you want this script to work on a MCC account, then you can put the whole code inside the account selector and iterator. You will also need to implement the select(account) method so that the script will know the next account on which to operate.

Moving forward on uploading the returned information of proximities to Google spreadsheet, I would recommend inserting this sample code to the script.

Let me know if you have further questions.

Regards,
Google Logo
Ernie John Blanca Tacata
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q23wKHl:ref

lea.h...@commissionsinc.com

unread,
Sep 8, 2020, 5:12:01 PM9/8/20
to Google Ads Scripts Forum
Thank you, Ernie John.
I used the below, and it's working. It takes about 30 minutes and there are some accounts it doesn't get through, so I'm now trying to exclude some of our accounts and campaigns based on Account Name and Campaign Name. After I added .withCondition("Name DOES_NOT_CONTAIN 'New'"); to the Account Selector and 
  .withCondition("CampaignName DOES_NOT_CONTAIN_IGNORE_CASE 'Main' AND CampaignName DOES_NOT_CONTAIN_IGNORE_CASE 'Remarketing'") to the campaign iterator, the script seems to be running much more slowly and is not getting through as many accounts before timing out. Is that something that would slow down the script? Should I use something like a Label instead to narrow down which accounts the script looks at, rather than using Account Name and CampaignName? Thanks!
 
function main() {

 var accountSelector = AdsManagerApp
     .accounts()
     .withCondition("Clicks > 300")
     .forDateRange("LAST_30_DAYS")
     .orderBy("Clicks DESC")
     .withCondition("Name DOES_NOT_CONTAIN 'New'");

  var accountIterator = accountSelector.get();
 while (accountIterator.hasNext()) {
   var account = accountIterator.next();
 //}

AdsManagerApp.select(account);
//Now in the individual account rather than MCC account
 
  var proximitySelector = AdsApp.targeting()
    .targetedProximities()
    .forDateRange("LAST_MONTH")
    .orderBy("Clicks DESC");

  var campaignIterator = AdsApp.campaigns()
  .withCondition("Status = ENABLED")
  .withCondition("CampaignName DOES_NOT_CONTAIN_IGNORE_CASE 'Main' AND CampaignName DOES_NOT_CONTAIN_IGNORE_CASE 'Remarketing'")
     .get();
  if (campaignIterator.hasNext()) {
   var campaign = campaignIterator.next();
    var proximityIterator = campaign.targeting().targetedProximities().get();

    while (proximityIterator.hasNext()) {
      var targetedProximity = proximityIterator.next();
      Logger.log(campaign.getName() + " | " + targetedProximity.getRadius() +
            targetedProximity.getRadiusUnits() +
            ' around (' +
            targetedProximity.getLatitude() +
            targetedProximity.getLongitude() + ')' + campaign.getId());
   
//append rows to a spreadsheet
//function appendARow() {
  // Name of the specific sheet in the spreadsheet.
  var SHEET_NAME = 'proximity';

  var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
  var sheet = ss.getSheetByName(SHEET_NAME);

 sheet.appendRow([targetedProximity.getRadius(),targetedProximity.getLatitude(), targetedProximity.getLongitude(), campaign.getId(), campaign.getName()]);
}

    }
 }
}
  

Google Ads Scripts Forum Advisor

unread,
Sep 8, 2020, 10:07:39 PM9/8/20
to adwords...@googlegroups.com
Hi Lea,

If you've added filters to your script, it should execute faster. However, it appears that the filters implemented in the script targeted the accounts and campaigns with a lot of proximities and this is the possible reason why it is running slowly. With this, you can add more filters such as the Label that you've mentioned so that the script will process subsets of accounts and campaigns. Then, after the successful execution, you can change the filters so that the script will process another accounts and campaigns.

lea.h...@commissionsinc.com

unread,
Sep 9, 2020, 2:02:22 PM9/9/20
to Google Ads Scripts Forum
Thanks, Ernie John! We're going to try splitting the campaigns up by daily budget, or by account labels, to break the accounts into sets to avoid the script timing out.
Reply all
Reply to author
Forward
0 new messages