Remove sitelink adgroup level

187 views
Skip to first unread message

Merijn Poorthuis

unread,
Dec 31, 2019, 4:54:31 AM12/31/19
to Google Ads Scripts Forum
Hi,

I am using a script and spreadsheet to manage sitelinks on campaign and adgroup level. I am however seeing some inconsistencies when running the script. The script works for all my accounts but one where it acts strange. I use the following code to remove all sitelinks on campaign and adgroup level before adding them again, see below. When using this code in my other accounts it doesnt permanently remove the sitelink, it still exists in account so that I can add it again to the correct ad groups. However sometimes it seems to permanently remove the sitelink and then I lose the sitelink ID and the account gets filled up with removed sitelinks, which I then need to manually remove. The strange thing is that it just sometimes doesnt work and sometimes it does. It also always works for campaign sitelinks but not always for adgroup sitelinks. It works for all my other accounts with the same code but not for this specific account. For this reason I cannot determine what causes the issue. How can we solve this issue?

function removeSitelink()
{
  var camps = AdWordsApp.campaigns().withCondition("CampaignStatus = ENABLED").get();
  var adgrps = AdWordsApp.adGroups().withCondition("CampaignStatus = ENABLED").withCondition("Status = ENABLED").get();
  while(camps.hasNext())
  {
    var camp = camps.next();
    var sitelinks = camp.extensions().sitelinks().get();
    var callouts = camp.extensions().callouts().get();
    while(sitelinks.hasNext())
    {
      var sitelink = sitelinks.next();
      camp.removeSitelink(sitelink);
    }
    
    while(callouts.hasNext())
    {
      var callout = callouts.next();
      camp.removeCallout(callout);
    }
  }
  while(adgrps.hasNext())
  {
    var adgrp = adgrps.next();
    var sitelinks = adgrp.extensions().sitelinks().get();
    var callouts = adgrp.extensions().callouts().get();
    while(sitelinks.hasNext())
    {
      var sitelink = sitelinks.next();
      adgrp.extensions().sitelinks().get().next()..removeSitelink(sitelink);
    }
    
    while(callouts.hasNext())
    {
      var callout = callouts.next();
      adgrp.removeCallout(callout);
    }
  }
}


Merijn Poorthuis

unread,
Dec 31, 2019, 4:57:44 AM12/31/19
to Google Ads Scripts Forum
      adgrp.removeSitelink(sitelink);

Actually messed up on the code in the original post.

Google Ads Scripts Forum Advisor

unread,
Jan 2, 2020, 12:44:47 AM1/2/20
to adwords...@googlegroups.com
Hi Merijn,

Thanks for posting your concern.

Could you confirm if the issue has been resolved by replacing the code adgrp.extensions().sitelinks().get().next()..removeSitelink(sitelink) with the adgrp.removeSitelink(sitelink). If yes, then this is the correct way to fix the issue. With the line of code adgrp.extensions().sitelinks().get().next()..removeSitelink(sitelink), the inconsistency occurs as the next() method is being called twice. Please note that in every iterator, there should only be one next() method implemented.

If the issue is still occurring, please provide the customer ID and the name of the script via Reply privately to author option so that I can further investigate.

Regards,
Ejay
Google Ads Scripts Team

ref:_00D1U1174p._5001UOFYbi:ref
Reply all
Reply to author
Forward
0 new messages