Skip to content

Instantly share code, notes, and snippets.

@paveljasek
Last active March 14, 2017 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paveljasek/95c8858269ab9c8de55cc020d7521ec6 to your computer and use it in GitHub Desktop.
Save paveljasek/95c8858269ab9c8de55cc020d7521ec6 to your computer and use it in GitHub Desktop.
// This AdWords Script makes sure that keywords are managed in exact and broad match modifier campaigns.
// Whenever the script finds an adgroup, keyword or ad text in exact match campaign that was not already
// copied into broad match modifier campaign, it does that and labels the entity accordingly.
// Broad match campaigns need to be created at first place.
// Customize the BidModifier to update bids for broad match ad groups [line 20] and identification of your campaigns [lines 15, 19 and 26]; 
// jasek@google.com
// Known issues: if you have ads with tracking templates, custom parameters or mobile final URLs, please check lines 116-118
// Known issues: if you have keywords with final URLs, please check line 91
function main() {
Logger.log("start");
var SourceCampaignList = AdWordsApp.campaigns()
.withCondition("Name CONTAINS '_exact_'")
.get();
Logger.log("SourceCampaignList" + SourceCampaignList.totalNumEntities());
var MatchType = "_broad_";
var BidModifier = 0.35;
while (SourceCampaignList.hasNext()) {
var SourceCampaign = SourceCampaignList.next();
var SourceCampaignName = SourceCampaign.getName();
Logger.log("- Checking SourceCampaignName: " + SourceCampaignName);
var SourceCampaignRootName = SourceCampaignName.replace(/(.*)(_exact_)/,'$1');
Logger.log("- SourceCampaignRootName: " + SourceCampaignRootName);
var TargetCampaignName = SourceCampaignRootName + MatchType +"";
Logger.log("- TargetCampaignName: " + TargetCampaignName);
try {
var TargetCampaign = AdWordsApp.campaigns().withCondition("CampaignName = '" + TargetCampaignName + "'").get().next();
} catch(e) {
Logger.log("!!! Failed to find destination campaign. Please create this campaign manually: " + TargetCampaignName);
continue; // try out next campaign
}
Logger.log("TargetCampaign getName: " + TargetCampaign.getName());
// monitoring and potentially creating ad groups
var SourceAdGroups = AdWordsApp.adGroups().withCondition("CampaignName = '" + SourceCampaignName + "'").get();
while (SourceAdGroups.hasNext()) {
var SourceAdgroup = SourceAdGroups.next();
var AdgroupName = SourceAdgroup.getName();
var AdgroupBid = SourceAdgroup.bidding().getCpc();
var AdgroupLabels = SourceAdgroup.labels();
Logger.log("-- Source: CampaignName = " + SourceCampaignName + " AdGroupName = " + AdgroupName);
Logger.log("-- Target: CampaignName = " + TargetCampaignName + " AdGroupName = " + AdgroupName);
Logger.log("-- AdgroupName = " + SourceAdgroup.getName() + " AdgroupBid = " + SourceAdgroup.bidding().getCpc());
if (AdWordsApp.adGroups().withCondition("CampaignName = '" + SourceCampaignName + "'").withCondition("LabelNames CONTAINS_NONE ['Copied']").get().totalNumEntities() > 0) {
var adGroupBuilder = TargetCampaign.newAdGroupBuilder();
var adGroupOperation = adGroupBuilder
.withName(AdgroupName)
.withCpc(AdgroupBid*BidModifier)
.withTrackingTemplate(SourceAdgroup.urls().getTrackingTemplate())
.withCustomParameters(SourceAdgroup.urls().getCustomParameters())
.build();
var adGroupResult = adGroupOperation.getResult();
Logger.log("-- Copied adgroup " + TargetCampaignName + " adgroup: " + AdgroupName);
Logger.log("-- Was the adgroup created? " + adGroupOperation.isSuccessful().toString());
SourceAdgroup.applyLabel("Copied");
} else {
Logger.log("-- Skipping this adgroup as it is already copied: Campaign " + TargetCampaignName + " adgroup: " + AdgroupName);
}
// potentially copying keywords into broad match campaign and adding negative exact matches
var Keywords = AdWordsApp.keywords().withCondition("CampaignName = '" + SourceCampaignName + "'").withCondition("AdGroupName = '" + AdgroupName + "'").withCondition("LabelNames CONTAINS_NONE ['Copied']").get();
try {
var TargetAdGroup = AdWordsApp.adGroups().withCondition("CampaignName = '" + TargetCampaignName + "'").withCondition("AdGroupName = '" + AdgroupName + "'").get().next();
} catch(e) {
Logger.log("!!! Failed to retrieve target campaign " + TargetCampaignName + " adgroup: " + AdgroupName);
continue; // try out next ad group, skipping also ad texts
}
while (Keywords.hasNext()) {
var Keyword = Keywords.next();
var KeywordName = Keyword.getText();
var KeywordName2 = KeywordName.replace(/(\[)(.*)(\])/, '$2').replace(/([^ ]+)/g, '+$1');
var KeywordName3 = KeywordName.replace(/(\[)(.*)(\])/, '[$2]');
Logger.log("-- kw: " + KeywordName + " kw2: " + KeywordName2 + " kw3: " + KeywordName3);
// keyword bid? var AdgroupBid = Adgroup.bidding().getCpc();
var keywordOperation = TargetAdGroup.newKeywordBuilder()
.withText(KeywordName2)
//.withFinalUrl(Keyword.urls().getFinalUrl())
.withTrackingTemplate(Keyword.urls().getTrackingTemplate())
.withCustomParameters(Keyword.urls().getCustomParameters())
.build();
Keyword.applyLabel("Copied")
// adding exact match negative keywords
var NegativeKeyword = TargetCampaign.createNegativeKeyword(KeywordName3)
}
// creating ad texts that haven't been copied so far
try {
var Ads = SourceAdgroup.ads().withCondition("Type = 'EXPANDED_TEXT_AD'").withCondition("LabelNames CONTAINS_NONE ['Copied']").get();
} catch(e) {
Logger.log("!!! Failed to retrieve text ads for target campaign " + TargetCampaignName + " adgroup: " + AdgroupName);
continue;
}
Logger.log("Number of expanded text ads: " + Ads.totalNumEntities());
while (Ads.hasNext()) {
var SourceAd = Ads.next();
var adOperation = TargetAdGroup.newAd().expandedTextAdBuilder()
.withHeadlinePart1(SourceAd.getHeadlinePart1())
.withHeadlinePart2(SourceAd.getHeadlinePart2())
.withDescription(SourceAd.getDescription())
.withFinalUrl(SourceAd.urls().getFinalUrl(0))
//.withTrackingTemplate(SourceAd.urls().getTrackingTemplate())
//.withCustomParameters(SourceAd.urls().getCustomParameters())
//.withMobileFinalUrl(SourceAd.urls().getMobileFinalUrl())
.build();
var ad = adOperation.getResult();
Logger.log("Copied ad text with headlines: " + SourceAd.getHeadlinePart1() + " / " + SourceAd.getHeadlinePart2());
Logger.log("Was the ad text created? " + adOperation.isSuccessful().toString());
SourceAd.applyLabel("Copied")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment