function main() {
// Fetch All campaigns
var campaignsIterator = AdWordsApp.campaigns().get();
// Iterate AdGroups
while (campaignsIterator.hasNext()) {
var campaign = campaignsIterator.next();
var adGroupsIterator = campaign.adGroups().get();
while (adGroupsIterator.hasNext()) {
var adGroup = adGroupsIterator.next();
// Fetch kewwords for each AdGroups
var keywordsIterator = AdWordsApp.keywords().withCondition('AdGroupName = "' + adGroup.getName() + '"').get();
if (keywordsIterator.totalNumEntities() > 0) {
var arrSort = [];
while (keywordsIterator.hasNext()) {
var keyword = keywordsIterator.next();;
var stats = keyword.getStatsFor("INSERT_DATE_RANGE_HERE");
var impressions = stats.getImpressions();
var arrayImpresssion = [impressions, keyword];
arrSort.push(arrayImpresssion);
}
// Sort by Impressions
arrSort.sort(function(a, b) {
return a[0] - b[0];
});
// Get the most impressions keyword in the AdGroup
var arrSize = arrSort.length - 1;
var mostImpKW = arrSort[arrSize][1];
// Perform the single keyword ad group creation here using Keywordbuilder and AdGroupBuilder
arrSort = [];
}
}
}
}function main() {
var MIN_IMPRESSIONS = 100; var MIN_CLICKS = 5; var LAST_N_DAYS = 7; var report = 'KEYWORDS_PERFORMANCE_REPORT'; var cols = ['AdGroupId', 'Id', 'KeywordMatchType', 'Impressions', 'Clicks']; var options = { includeZeroImpressions : false }; var dateTo = getAdWordsFormattedDate(0, 'yyyyMMdd'); var dateFrom = getAdWordsFormattedDate(LAST_N_DAYS, 'yyyyMMdd'); var DATE_RANGE = dateFrom + ',' + dateTo;
var query = ['select', cols.join(','), 'from', report, 'where CampaignStatus = ENABLED AND AdGroupStatus = ENABLED AND Status = ENABLED', 'and Impressions >= ' + MIN_IMPRESSIONS + ' ', 'and Clicks >= ' + MIN_CLICKS + ' ', 'and KeywordMatchType = "Exact"', 'during', DATE_RANGE].join(' '); var reportIter = AdWordsApp.report(query, options).rows();
var kwIds = []; while(reportIter.hasNext()) { var reportRow = reportIter.next(); kwIds.push([reportRow.AdGroupId, reportRow.Id]); log('Keywords Found: ' + reportRow.AdGroupId + "-" + reportRow.Id); }
log('Keywords Found: ' + kwIds.length); var kws = AdWordsApp.keywords() .withIds(kwIds).get(); log('Keywords Found: ' + kws.totalNumEntities()); while (kws.hasNext()) { var kw = kws.next(); addKeywordToNewAdGroup(campaignName, kw) } exit();}
function addKeywordToNewAdGroup(keyword) {
var campaign = keyword.getCampaign(); var campaignName = campaign.getName(); var adGroupName = keyword.getAdGroup().getName(); var adGroupIterator = AdWordsApp.adGroups().withCondition('CampaignName = "' + campaignName + '" AND Name = "' + adGroupName + '"').get(); if (adGroupIterator.hasNext()) { var adGroup = adGroupIterator.next(); var adGroupBuilder = campaign.newAdGroupBuilder(); var adGroupOperation = adGroupBuilder .withName("SKAG | " & keyword.getText() & " | EXACT") .withStatus("ENABLED") .withCpc(0.3) .build();
if(adGroupOperation.isSuccessful()) { var keywordOperation = adGroup.newKeywordBuilder() .withText(keyword.getText()) .withCpc(0.5) .build();
// Remove the Keyword from original campaign if(keywordOperation.isSuccessful()) { Logger.log("Added keyword to Adgroup and now removing " + keyword.getText()); keyword.remove();
// Copy ads across to new adgroup var adsIterator = adGroup.ads().get(); while (adsIterator.hasNext()) { var ad = adsIterator.next(); // how do I copy the ads across? } } } } }
function getAdWordsFormattedDate(d, format){ var date = new Date(); date.setDate(date.getDate()); return Utilities.formatDate(date,AdWordsApp.currentAccount().getTimeZone(),format);}
function log(msg) { var time = Utilities.formatDate(new Date(),AdWordsApp.currentAccount().getTimeZone(), 'dd-MM-yyyy HH:mm:ss'); Logger.log(time + ' – ' + msg);}Thanks for that Hiroyuki.
How do I fill the various parts of the ad for call ads? One the help documentation it seems to refer to text ads rather than call ads.
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "AdWords Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/c9ac6b0f-e9ab-47ec-b76e-9a038a7a4bde%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
var adOperation = adGroupNew.getResult().newTextAdBuilder() .withHeadline("headline of ad") .withDescription1("first line of ad description") .withDescription2("second line of ad description") .withDisplayUrl(ad.getDisplayUrl()) .withFinalUrl("http://www.example.com") .build();
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "AdWords Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scripts+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/96224766-04ac-4164-a456-084924351772%40googlegroups.com.
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "AdWords Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/eef95bf9-9f57-4b4e-85af-242cfadcfccc%40googlegroups.com.
if (adGroupOperation.isSuccessful()) {
var adGroup = adGroupOperation.getResult();
var keywordBuilder = adGroup.newKeywordBuilder();
var keywordOperation = keywordBuilder
.withText('[' + keyword.getText() + ']')
.withCpc(0.30)
.build();
}Could you show me what the code would look like? I don't understand your answer.
That worked Hiroyuki – one last qu, I’m trying to get the adgroup name by doing this:
Logger.log("Added keyword to Adgroup " + adGroup.getName() + " and now removing " + keyword.getText());
It’s returning this:
05/11/2018 15:57:15 | Added keyword to Adgroup null and now removing [free number] |
Any idea what I’m doing wrong?
From: Hiroyuki Miyauchi (AdWords Scripts Team) via AdWords Scripts Forum <adwords-scripts+APn2wQfnD0zes0cm...@googlegroups.com>
Sent: 05 November 2018 09:58
To: AdWords Scripts Forum <adwords...@googlegroups.com>
Subject: Re: Create SKAGs
Hello Del,
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/37ad4910-b8ff-4da9-930e-ab4f9383a86f%40googlegroups.com.
Hi Hiroyuki
After checking the groups produced by the code below I’m finding it’s not working correctly – the groups are named:
AdGroup ID: 61378052133 Created at 2018-11-05T07:59:42.732-08:00
From: Hiroyuki Miyauchi (AdWords Scripts Team) via AdWords Scripts Forum <adwords-scripts+APn2wQfnD0zes0cm...@googlegroups.com>
Sent: 05 November 2018 09:58
To: AdWords Scripts Forum <adwords...@googlegroups.com>
Subject: Re: Create SKAGs
Hello Del,
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/37ad4910-b8ff-4da9-930e-ab4f9383a86f%40googlegroups.com.
function getAdWordsFormattedDate(d, format){
var date = new Date();
date.setDate(date.getDate());
return Utilities.formatDate(date,AdWordsApp.currentAccount().getTimeZone(),format);
}function getAdWordsFormattedDate(d, format){
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
var now = new Date();
var date = new Date(now.getTime() - d * MILLIS_PER_DAY);
var timeZone = AdWordsApp.currentAccount().getTimeZone();
return Utilities.formatDate(date, timeZone, format);
}That’s solved that problem – thanks Hiroyuki.
It leaves one problem behind – the ads are not being copied across. Any thoughts why that is happening?
From: Hiroyuki Miyauchi (AdWords Scripts Team) via AdWords Scripts Forum <adwords-scripts+APn2wQfnD0zes0cm...@googlegroups.com>
Sent: 12 November 2018 05:37
To: AdWords Scripts Forum <adwords...@googlegroups.com>
Subject: Re: Create SKAGs
Hello Del,
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "AdWords Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/dbb69210-a5e7-419e-a193-5172b25ebda1%40googlegroups.com.
if (adGroupOperation.isSuccessful()) {
var newAdGroup = adGroupOperation.getResult();
var keywordBuilder = adGroup.newKeywordBuilder();
var keywordOperation = keywordBuilder
.withText('[' + keyword.getText() + ']')
.withCpc(0.30)
.build();
// Remove the Keyword from original campaign
if (keywordOperation.isSuccessful()) {
Logger.log("Added keyword to Adgroup " + adGroup.getName() + " and now removing " + keyword.getText());
keyword.remove();
// Copy ads across to new adgroup
var adsIterator = adGroup.ads().withCondition('Type=EXPANDED_TEXT_AD').get();
while (adsIterator.hasNext()) {
var ad = adsIterator.next();
var adOperation = newAdGroup.newAd().expandedTextAdBuilder()
.withHeadlinePart1(ad.getHeadlinePart1())
.withHeadlinePart2(ad.getHeadlinePart2())
.withDescription(ad.getDescription())
.withPath1(ad.getPath1())
.withPath2(ad.getPath2())
.withFinalUrl(ad.urls().getFinalUrl())
.build();
var ad = adOperation.getResult();
}
}
}That fixed it – thanks!
One remaining error – on the following lines
var ad = adOperation.getResult();
if (ad.isSuccessful()) {
Logger.log("Added ad to Adgroup " + ad.getHeadlinePart1() + " and url " + ad.urls().getFinalUrl());
}
I get the error – I can’t see what I’m doing wrong:
14/11/2018 12:28:54TypeError: Cannot find function isSuccessful in object [EXPANDED_TEXT_AD]. (file Code.gs, line 97)
I also am trying to access the description line 2 that is now available for expanded text ads – how do I do that?
From: Hiroyuki Miyauchi (AdWords Scripts Team) via AdWords Scripts Forum <adwords-scripts+APn2wQfnD0zes0cm...@googlegroups.com>
Sent: 13 November 2018 04:10
To: AdWords Scripts Forum <adwords...@googlegroups.com>
Subject: Re: Create SKAGs
Hello Del,
The reason why the script does not copy any ads is because the script tries to fetch ads from the newly created adGroups which don't have any ads (line 80). Since you are using the same variable name adGroup at line 57 and 67, if you want to fetch ads from the adGroup which the keyword belong to, you would need to change the variable name adGroup at line 67. Also, some of the methods to fetch ads information are incorrect. You may refer to the updated code below. However, feel free to modify based on your preferences.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/bd12d487-af39-4001-ad5d-ffa3a3a587c4%40googlegroups.com.
Hi Hiroyuki,
This part doesn’t seem to create the keyword – the adgroup is created ok, as are the ads, just the keyword isn’t created – any thoughts why?
var newAdGroup = adGroupOperation.getResult();
var keywordBuilder = adGroup.newKeywordBuilder();
var keywordOperation = keywordBuilder
.withText(keyword.getText())
.withCpc(0.30)
.build();
From: Hiroyuki Miyauchi (AdWords Scripts Team) via AdWords Scripts Forum <adwords-scripts+APn2wQfnD0zes0cm...@googlegroups.com>
Sent: 13 November 2018 04:10
To: AdWords Scripts Forum <adwords...@googlegroups.com>
Subject: Re: Create SKAGs
Hello Del,
The reason why the script does not copy any ads is because the script tries to fetch ads from the newly created adGroups which don't have any ads (line 80). Since you are using the same variable name adGroup at line 57 and 67, if you want to fetch ads from the adGroup which the keyword belong to, you would need to change the variable name adGroup at line 67. Also, some of the methods to fetch ads information are incorrect. You may refer to the updated code below. However, feel free to modify based on your preferences.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/bd12d487-af39-4001-ad5d-ffa3a3a587c4%40googlegroups.com.
I get the error – I can’t see what I’m doing wrong:
14/11/2018 12:28:54TypeError: Cannot find function isSuccessful in object [EXPANDED_TEXT_AD]. (file Code.gs, line 97)
You are encountering the error because the getResult() method returns an Ad entity and it does not have isSuccessful() function. If you want to check if the result of the operation you would need to update your code like below.
if (adOperation.isSuccessful()) {
var ad = adOperation.getResult();
Logger.log("Added Adgroup: " + "SKAG | " + keyword.getText().replace("[", "").replace("]", "") + " | EXACT" + " Headline: " + ad.getHeadlinePart1() + " and url " + newURL);
}
I also am trying to access the description line 2 that is now available for expanded text ads – how do I do that?
You may utilize getDescription2() method to get the second description of the expanded text ad.
This part doesn’t seem to create the keyword – the adgroup is created ok, as are the ads, just the keyword isn’t created – any thoughts why?
It seems the part that you provided has been already updated. Could you confirm if you resolved the issue?