Delete keywords with 0 Impression

104 views
Skip to first unread message

Tuong Nguyen

unread,
Nov 13, 2013, 9:56:41 AM11/13/13
to adwords...@googlegroups.com
Hi guys,
I'm a newbie for Adwords Scripts, just started to try learning & writing some codes
I want to get the keywords ' impressions for 2 period, then I compare them, if they equal 0, I will delete them. Here is my script:

function main() {
  
  var keywordsIterator = AdWordsApp.keywords()
  .withCondition("Status = ENABLED")
  .withCondition("AdGroupName CONTAINS '|'")
  .withCondition("CampaignStatus = ENABLED")
  .forDateRange("20130101", "20131112")
  .get();
  
  while(keywordsIterator.hasNext()){
  keywordsIterator.hasNext();
  var keyword = keywordsIterator.next();
  var impression1 = keyword.getStatsFor("20130101", "20131031").getImpressions();
  var impression2 = keyword.getStatsFor("20131101", "20131112").getImpressions();
    if(impression1 = impression2 = 0){
      keyword.remove();
 
  }
}
}

It turns out have sth wrong here, I haven't still figured out yet but I believe must be the line "if(impression1 = impression2 = 0)". Would you guys please have a look and help me with this! 
Really appreciate!

Anash Oommen

unread,
Nov 13, 2013, 12:51:18 PM11/13/13
to adwords...@googlegroups.com
Hi Tuong,

The syntax would be 

if (impressions1 == 0 && impressions2 == 0) {
  keyword
.delete();
}

Cheers,
Anash P. Oommen,
AdWords Scripts Team

Tuong Nguyen

unread,
Nov 13, 2013, 2:27:24 PM11/13/13
to adwords...@googlegroups.com
Hi Anash,
Thank you very much for your reply, the scripts ran fine but it cannot manage to delete all of the keywords that match the 'Delete' condition.
For ex., my Account has around 50k keywords with 0 impressions that needed to be deleted but for first time running, it deleted only 19k, then I ran it second time and it removed only 11k...
Any idea how to fix the issue?
Thanks in advance

DuracellTomi

unread,
Nov 13, 2013, 3:32:48 PM11/13/13
to adwords...@googlegroups.com
Hi Tuong,

The issue is related (imho) to the limits of AdWords Scripts: each script's run time is limited to 30 minutes.
There is no way to exceed this limit.

What you can do is to query stats for shorter date range and/or create the script in multiple instances and schedule them to run next to each other (using hours) 

Cheers,
Thomas Geiger

Ilya

unread,
Nov 14, 2013, 4:44:35 PM11/14/13
to adwords...@googlegroups.com
One thing to try that could help: filter out the keywords in your selector for one date range, and only use the manual condition for the other.

function main() {
 
 
var keywordsIterator = AdWordsApp.keywords()
   
.withCondition("Status = ENABLED")
   
.withCondition("AdGroupName CONTAINS '|'")
   
.withCondition("CampaignStatus = ENABLED")

     
// only getting keywords with 0 impressions in first date range
   
.withCondition("Impressions = 0")

   
.forDateRange("20130101", "20131031")
   
.get();
 
 
while(keywordsIterator.hasNext()){
    keywordsIterator
.hasNext();
   
var keyword = keywordsIterator.next();

   
// not needed anymore
   
// var impression1 = keyword.getStatsFor("20130101", "20131031").getImpressions();

   
var impression2 = keyword.getStatsFor("20131101", "20131112").getImpressions();

   
if(impression2 == 0){
      keyword
.remove();
   
}
 
}
}

Tuong Nguyen

unread,
Nov 14, 2013, 11:16:23 PM11/14/13
to adwords...@googlegroups.com
Hi All,
Thank you very much for your helpful support.
@Thomas: The issue is that the script ran in 5 mins, and it said "success" but did not manage to delete all the keywords, so I ran it and the second time still had the same issue, it stopped after 6 mins, then I had to run it again till no keywords are found with the "delete" condition
@llya: thanks, I will try your script

Tuong

Anash Oommen

unread,
Nov 17, 2013, 8:08:48 PM11/17/13
to adwords...@googlegroups.com
I'd make another suggestion - try storing the matching keywords in an array and then deleting them outside the loop. I wonder if the keywordIterator is skipping items if you are deleting the keywords in that iterator itself

var matchingKeywords = [];

while(keywordsIterator.hasNext()){
   
...
   
if(impression2 == 0){
      matchingKeywords
.push(keyword);
   
}
 
}

for (i = 0; i < matchingKeywords.length; i++) {
  matchingKeywords
[i].remove();
}

Cheers,
Anash P. Oommen,
AdWords Scripts Team

Reply all
Reply to author
Forward
0 new messages