Error in script execution "An error occurred. Please try again later"

283 views
Skip to first unread message

Pere Munar

unread,
Apr 17, 2024, 12:15:09 PM4/17/24
to Google Ads Scripts Forum
Hello! 

I have the script that I post below. I place it in several different accounts and in some of them I get the error "An error occurred. Please try again later" several times (one for every campaign) and in some it runs without errors.

What the script does is to set the ad group final url suffix to a certain value, putting the name of the campaign, the name of the ad group and other fix utms.

I tryed to change a campaign's name and execute the script, and for that particular campaign it performs well, but I get the error for the rest of campaigns that had not changed.

Can someone help me understand the errors?

Thanks!

Here is the script code:

function main() {
   var SuffixTemplate="utm_source=google&utm_medium=pl&utm_campaign={CampaignName}&utm_adgroup={AdGroupName}&utm_ad={creative}&utm_term={keyword}";
 
                var _CAMPAIGN_CONTAINS="";               //Filter by Campaign name  
                var _ADGROUP_CONTAINS="";               //Filter by Adgroup name
                var STATUS="ENABLED";                    //ENABLED, PAUSED
 
                if (SuffixTemplate.search(/{AdGroupName}|{CampaignName}/g)==-1) {
                               Logger.log("Enter at least one of the {CampaignName} or {AdGroupName} parameter in the tracking template");
                               return
                }  
                if (SuffixTemplate.search("{CampaignName}")>0&&SuffixTemplate.search("{AdGroupName}")==-1) {
                               var campaignIterator=_CAMPAIGN_CONTAINS==""?AdsApp.campaigns().withCondition("Status = "+STATUS).get():AdsApp.campaigns().withCondition("Name contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Status = "+STATUS).get();
                               if (!campaignIterator.hasNext()) {
                                               Logger.log("No Campaigns matched with this condition");
                                               return
                               }
                               while (campaignIterator.hasNext()) {
                                               var campaign=campaignIterator.next();
                                               var campaigntemplate=SuffixTemplate.replace(/{CampaignName}/g,campaign.getName().replace(/\s/g,'%20'))
                                               campaign.urls().setFinalUrlSuffix(campaigntemplate);
                                               Logger.log(campaign.getName()+" => "+campaigntemplate)
                               }
                }
                if (SuffixTemplate.search("{AdGroupName}")>0) {
                               var adgroupIterator = {hasNext:function(){return false}}
                               if (_ADGROUP_CONTAINS==""&&_CAMPAIGN_CONTAINS=="") {
                                               adgroupIterator=AdsApp.adGroups().withCondition("Status = "+STATUS).get();
                               } else if (_ADGROUP_CONTAINS==""&&_CAMPAIGN_CONTAINS!=="") {
                                               adgroupIterator=AdsApp.adGroups().withCondition("CampaignName contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Status = "+STATUS).get();
                               } else if (_ADGROUP_CONTAINS!==""&&_CAMPAIGN_CONTAINS!=="") {
                                               adgroupIterator=AdsApp.adGroups().withCondition("CampaignName contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();
                               } else if (_ADGROUP_CONTAINS!==""&&_CAMPAIGN_CONTAINS=="") {
                                               adgroupIterator=AdsApp.adGroups().withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();
                               }
 
                               if (!adgroupIterator.hasNext()) {
                                               Logger.log("No Campaigns/Adgroups matched with this condition");return
                               }
                               while (adgroupIterator.hasNext()) {  
                                   var adgroup=adgroupIterator.next();
                                               var adgrouptemplate=SuffixTemplate.replace(/{AdGroupName}/g,adgroup.getName().replace(/\s/g,'%20'))
                                               if (SuffixTemplate.search("{CampaignName}")>0) {
                                                               adgrouptemplate=adgrouptemplate.replace(/{CampaignName}/g,adgroup.getCampaign().getName().replace(/\s/g,'%20'))
                                               }
                                               adgroup.urls().setFinalUrlSuffix(adgrouptemplate);
                                               Logger.log(adgroup.getCampaign().getName()+" => "+adgroup.getName()+" => "+adgrouptemplate)
                               }
                }
}

Pere Munar

unread,
Apr 18, 2024, 5:45:14 AM4/18/24
to Google Ads Scripts Forum
Hi again!

I have found what the problem was: the script was working fine but throwing errors when trying to act on campaigns that had been removed. These campaigns cannot be edited, so the script could not perform its job on them.

Thanks anyway to everyone!

Cheers,

Pere

Google Ads Scripts Forum Advisor

unread,
Apr 18, 2024, 9:13:34 AM4/18/24
to adwords...@googlegroups.com

Hi Pere,

Thank you for reaching out to the Google Ads Scripts team. 

Upon checking your query, I understand that you are facing an “An error occurred. Please try again later” error while executing the same script in different accounts. Please provide the below details for further investigation.

  • Google Ads account ID / CID.
  • Name of the script (Tools and Settings > Bulk Actions > Scripts).
  • Error details or a screenshot of the issue (if any).
You can send the details via Reply privately to the author option, or direct private reply to this email.
 
This message is in relation to case "ref:!00D1U01174p.!5004Q02tPCZp:ref" (ADR-00231673)

Thanks,
 
Google Logo Google Ads Scripts Team


Reply all
Reply to author
Forward
0 new messages