Excluded Location applied in Wrong Campaign

86 views
Skip to first unread message

Marketing Blastness

unread,
Feb 24, 2017, 11:41:47 AM2/24/17
to AdWords Scripts Forum
Hello,
I'm trying to automatically exclude from a campaign the locations that did not convert in the past 60 days.


and everything seem to work, at least on script preview: the countries are correctly selected and the modification preview tells me that X countries are to be stopped in my selected campaign.

When I run the script, though, the locations exclusion is applied....on a different campaign!! Any clue on how it can happens?

Thank you in advance

Matteo

Anthony Madrigal

unread,
Feb 24, 2017, 1:55:46 PM2/24/17
to AdWords Scripts Forum
Hi Matteo,

Could you please reply privately to author your CID, script name, the campaign that should have been excluded and the campaign that was excluded?

Thanks,
Anthony
AdWords Scripts Team

Marketing Blastness

unread,
Feb 27, 2017, 12:16:20 PM2/27/17
to AdWords Scripts Forum
Private message sent.

Thank you

Matteo

Marketing Blastness

unread,
Feb 28, 2017, 6:14:44 AM2/28/17
to AdWords Scripts Forum
Hello Anthony,
considering that apparently there is no reason on your side for the wrong campaign exclusion I'll keep testing the script and let you know if some reason will come out on my side.

Meanwhile, you suggested me to modify the script to check if the location is already excluded from the campaign, but I have no idea on how to make it happen...do you have any piece of advice? I thought I could have used IsTargetingLocation as condition in the query, but the guide at https://developers.google.com/adwords/api/docs/appendix/reports/geo-performance-report says that this parameter is set on true by default...

Thank you very much for any suggestion you could give!

Matteo

Anthony Madrigal

unread,
Feb 28, 2017, 10:29:16 AM2/28/17
to AdWords Scripts Forum
Hello Matteo,

You can check whether the location is already excluded from a campaign after you run the report by iterating through the campaigns excluded locations.

Here is a script that will check whether a location is excluded from a campaign. If it is not already, then it will add it to the campaign. You can modify the script to suit your script.
function main(){
 
var countryToExclude = 2056;
 
var campaignIterator = AdWordsApp.campaigns()
     
.withCondition('Name = "YOUR CAMPAIGN"')
     
.get();
 
if(campaignIterator.hasNext()) {
   
var campaign = campaignIterator.next();
   
var locationIterator = campaign.targeting().excludedLocations().get();
   
var alreadyExcluded = false;
   
while (locationIterator.hasNext()) {
     
var excludedLocation = locationIterator.next();
     
if(excludedLocation.getId() == countryToExclude){
        alreadyExcluded
= true;
       
break;
     
}
   
}
   
if(alreadyExcluded == false)
      campaign
.excludeLocation(countryToExclude);
 
}
}

Please let me know if you run into any issues.

Cheers,
Anthony
AdWords Scripts Team

Marketing Blastness

unread,
Feb 28, 2017, 11:19:46 AM2/28/17
to AdWords Scripts Forum
Hello Anthony,
thank you very much, I'll look into the script you've just sent, even if at first sight this one needs to manually (or remotely) insert the locations already excluded. This sound a bit crazy to me (apologetically of course...I'm a newbie) considering that the campaign data already know which location is used as a target and which one is excluded.
Anyway, doing a step backward, I think that part of the issues is due to the fact that my campaign has no target locations, but are set as "All Countries". Therefore I tried to bulk upload all the countries and then run the script...unfortunately I've had another error that puzzles me..."Impossible to choose as target and exclude at the same time" (probably not the same message you'd get...I'm just translating from italian).
But I do not see where, in my script, I choose any country as target.

The script giving that error is now:

function main() {
/////////////////////////////////////////////////////
//
// This script excludes the countries with no conversions in the past 60 days
// with a cost exceeding val_cost as set in line 31
//
// IMPORTANT: Check targetCampaign at line 9 to get proper value in lines 31 and 43
// IMPORTANT: Check average past performances to set proper val_cost limit at line 31
//
/////////////////////////////////////////////////////
  var TO_NOTIFY = "mymail";
  var targetCampaign = "mycampaignName";
// Calculate the date range "Last 60 Days"
var MILLIS_PER_DAY_PER_60 = 1000 * 60 * 60 * 24 * 60;
var timeZone = AdWordsApp.currentAccount().getTimeZone();
var now = new Date();
var sixtyDaysAgo = new Date(now.getTime() - MILLIS_PER_DAY_PER_60);
var is = Utilities.formatDate(now, timeZone, 'yyyyMMdd');
var was = Utilities.formatDate(sixtyDaysAgo, timeZone, 'yyyyMMdd');
// Extract data for the Last 60 Days
var report = AdWordsApp.report("SELECT Conversions,CampaignName,Clicks,CountryCriteriaId,LocationType,Cost,ConversionValue " + "FROM GEO_PERFORMANCE_REPORT WHERE CampaignStatus='ENABLED' AND LocationType='LOCATION_OF_PRESENCE' DURING "+ was +","+ is,{resolveGeoNames: false});

// Find details of the account
  var currentAccount = AdWordsApp.currentAccount();

// Decide to send mail or not
  var invia_email = false;

  // Extract Rows from Report
  var reports = report.rows();
  while(reports.hasNext()) {
  var thisConvVal = reports.next();
    
// Extracting data
    var val_conversions = thisConvVal.Conversions;
    var val_campaignName = thisConvVal.CampaignName;
var val_country = thisConvVal.CountryCriteriaId;
var val_cost = thisConvVal.Cost;
// Select countries with low performances
// IMPORTANT: Check average past performances to set proper val_cost limit
// IMPORTANT: Check correct campaignName
if(val_campaignName == targetCampaign && val_conversions == 0 && val_cost > 14)
{
// Exclude poor performing countries (no Conversions over the Last 60 Days and cost exceeding val_cost)
var campaignIterator = AdWordsApp.campaigns()
.withCondition('Name = "'+targetCampaign+'"')
.get();
if (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var countryToExclude = Number (val_country);
campaign.excludeLocation(countryToExclude);
  }
 
 // print results in log
 Logger.log("Fermata nazione "+countryToExclude+" (vedi modifiche per nome) per costo = "+val_cost+"€ e nessuna conversione a partire dal "+was+"");
// send alert email if set true on line 10
if(invia_email == true) {
                  MailApp.sendEmail(TO_NOTIFY,"ALERT "+currentAccount.getName()+" Excluded Country "+val_country+"","Your AdWords Script excluded CountryId "+val_country+" because of the low performances over the last 60 days: 0 conversions and "+val_cost+"€ cost.");}
 }      
    }
}



Thanks for any further help....

Matteo

Anthony Madrigal

unread,
Feb 28, 2017, 2:58:18 PM2/28/17
to AdWords Scripts Forum
Hi Matteo,

If you have a campaign that targets All Countries, you will not get anything returned when returning targeted locations for a campaign. If you want to exclude a location that you are targeting, you will first need to remove the targeting.

Below is how you should modify your code to implement the snippet I created to check whether the location is already excluded from the campaign.
var campaignIterator = AdWordsApp.campaigns()
 
.withCondition('Name = "'+targetCampaign+'"')
 
.get();
if (campaignIterator.hasNext()) {
 
var campaign = campaignIterator.next();
 
var countryToExclude = Number (val_country);

 
var locationIterator = campaign.targeting().excludedLocations().get();
 
var alreadyExcluded = false;
 
while (locationIterator.hasNext()) {
   
var excludedLocation = locationIterator.next();
   
if(excludedLocation.getId() == countryToExclude){
      alreadyExcluded
= true;
     
break;
   
}
 
}
 
if(alreadyExcluded == false)
      campaign
.excludeLocation(countryToExclude);
}

Marketing Blastness

unread,
Mar 1, 2017, 10:22:48 AM3/1/17
to AdWords Scripts Forum
Dear Anthony,
it looks like you are going to save my day ;)
The script, as modified, seems to work:

function main() {
/////////////////////////////////////////////////////
//
// This script excludes the countries with no conversions in the past 60 days
// with a cost exceeding val_cost as set in line 31
//
// IMPORTANT: Check targetCampaign at line 12 to get proper value in lines 43 and 47
// IMPORTANT: Check average past performances to set proper val_cost limit at line 43
if(val_campaignName == targetCampaign && val_conversions == 0 && val_cost > 20)
{
// Exclude poor performing countries (no Conversions over the Last 60 Days and cost exceeding val_cost)
var campaignIterator = AdWordsApp.campaigns()
.withCondition('Name = "'+targetCampaign+'"')
.get();
if (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var countryToExclude = Number (val_country);
var locationIterator = campaign.targeting().excludedLocations().get();
var alreadyExcluded = false;
while (locationIterator.hasNext()) {
var excludedLocation = locationIterator.next();
if(excludedLocation.getId() == countryToExclude){
alreadyExcluded = true;
break;
}
}
if(alreadyExcluded == false)
campaign.excludeLocation(countryToExclude);
  }
 
 // print results in log
 Logger.log("Fermata nazione "+countryToExclude+" (vedi modifiche per nome) per costo = "+val_cost+"€ e nessuna conversione a partire dal "+was+"");
// send alert email if set true on line 10
if(invia_email == true) {
                  MailApp.sendEmail(TO_NOTIFY,"ALERT "+currentAccount.getName()+" Excluded Country "+val_country+"","Your AdWords Script excluded CountryId "+val_country+" because of the low performances over the last 60 days: 0 conversions and "+val_cost+"€ cost.");}
 }      
    }
}


Only thing is that the Log still reports includes also the already excluded countries...any idea why it happens?


Anthony Madrigal

unread,
Mar 1, 2017, 1:33:15 PM3/1/17
to AdWords Scripts Forum
Hi Matteo,

I do not see logs that include reports with already excluded countries. I added some Logger.logs in your script so you can see how it is behaving. As far as I can see, the script should run as intended.

Let me know if there are any issues.

Regards,
Anthony
AdWords Scripts Team

Marketing Blastness

unread,
Mar 2, 2017, 10:54:18 AM3/2/17
to AdWords Scripts Forum
Dear Anthony,
in fact there are still issues. Please check the script "Poor Countries to be Checked".

In order to have a proper check in both Modifications and Log here's what I did: I went to the Geographic Dimension of my Target Campaign and I've chosen as test country to be excluded Lettonia (Latvia), that in the past 60 days (from january 1st) got 0 conversions and a cost of 0.05€. I've then changed the script accordingly on line 43:

if(val_campaignName == targetCampaign && val_conversions == 0 && val_cost == 0.05)

and, to have a double check on the Log, I've changed line 68 from:

 Logger.log("Fermata nazione "+countryToExclude+" (controlla modifiche per nome) per costo = "+val_cost+"€ e nessuna conversione a partire dal "+was+"");

to:

 Logger.log("Fermata nazione "+countryToExclude+" ("+excludedLocation.getName()+") per costo = "+val_cost+"€ e nessuna conversione a partire dal "+was+"");

The output of the script preview has been puzzling: the script was ready to stop Latvia, and the Log said:

Fermata nazione 2428 (San Marino) per costo = 0.05€ e nessuna conversione a partire dal 20170101

While 2428 is the right country ID for Latvia and San Marino is one of the 3 countries already excluded before the run of the script.

How did this happen?

Sorry for bothering you so much, but I really can't figure out how to have this script working!!!

Thank you,

Matteo

Anthony Madrigal

unread,
Mar 2, 2017, 11:22:00 AM3/2/17
to AdWords Scripts Forum
Hey Matteo,

I added some additional logs to your script. It will list all the excluded locations in your campaign.
Belgium 2056
Germany 2276
Vatican City 2336
Italy 2380
San Marino 2674

Since San Marino was the last location it checked, that is the value that is being returned for excludedLocation.getName(). As you can see from the logs and preview, Latvia was not found in your campaign, so it is being added as an excluded location.

Marketing Blastness

unread,
Mar 3, 2017, 5:09:42 AM3/3/17
to AdWords Scripts Forum
Ok, now I think I understood what happens, but I think I did not explain myself well enough.

What I was trying to achieve was having in log both the ID and the Name of the countryToExclude, trying to use a different var considering that I've used the conditional declaration {resolveGeoNames: false} in the Query in order to correctly process the exclusion.

Considering that campaign.targeting().targetedLocations().get(); on a campaign with no target countries (All World) retrieves just an empty cell, I've had to use the campaign "prova" to get the right name for the countryToExclude ID as you can see in my modified script. This solution is something I'd prefer not to use, because it will force me to add a test campaign in all my accounts...can you find a better solution for this problem?

Thank you again,

Matteo

Anthony Madrigal

unread,
Mar 3, 2017, 2:00:55 PM3/3/17
to AdWords Scripts Forum
Hi Matteo,

I'm afraid the only way I can think of retrieving the country name and Id would be to run two reports, one where resolveGeoNames: false and resolveGeoNames: true. This will at least solve the issue of having to create a test campaign under all your accounts. 

Hope this helps.

Marketing Blastness

unread,
Mar 6, 2017, 5:08:35 AM3/6/17
to AdWords Scripts Forum
Thank you. It is the same way I've been able to think to. Looks like I'm learning ;)

Thank you A LOT for everything!!!

Matteo

Anthony Madrigal

unread,
Mar 6, 2017, 9:18:06 AM3/6/17
to AdWords Scripts Forum
Hi Matteo,

Yes, it seems like you are :) No problem, it was a pleasure to help out.

Feel free to reach out in the future if you have any other issues or questions regarding AdWords Scripts.

Marketing Blastness

unread,
Mar 8, 2017, 6:07:13 AM3/8/17
to AdWords Scripts Forum
Dear Anthony,
more issues on the matter...I'm sorry...

Problem is that Query resolves a countryCriteriaID that has no correspondence in https://developers.google.com/adwords/api/docs/appendix/geotargeting.

As you will see I drafted in the script an array of countries to have a better email output. Unfortunately countryCriteriaID 2364 is still undefined. Could you please explain to me how this happens?

Thank you again

Matteo

Anthony Madrigal

unread,
Mar 8, 2017, 10:03:24 AM3/8/17
to AdWords Scripts Forum
Hi Matteo,

The countryCriteriaId 2364 refers to Iran. Some countries, such as Iran, as non-targetable but can still show up in reports. You can modify your report query to exclude this Id so that there are no issues when trying to target/exclude them.

Marketing Blastness

unread,
Mar 9, 2017, 4:46:38 AM3/9/17
to AdWords Scripts Forum
Dear Anthony,
thank you again for the explanation. Does not make sense to me, but it's ok.

Now, I thought the script was OK and ready to run, but the last test on countries with no conversions in the past 12 months had this results:

- the email have been sent with a list of 65 excluded country
- the countries with no conversions are in fact 118

...can you explain me how this happened?

Thank you

Matteo

Anthony Madrigal

unread,
Mar 9, 2017, 1:46:02 PM3/9/17
to AdWords Scripts Forum
Hi Matteo,

Could you please provide me with a screenshot of where you are seeing 118 countries? You can do so using reply privately to author.

Marketing Blastness

unread,
Mar 10, 2017, 3:43:57 AM3/10/17
to AdWords Scripts Forum
Private message sent.
Reply all
Reply to author
Forward
0 new messages