Google Adwords Script定时监控高价关键词

近期为了提高Google Adwords管理,采用一系列的Google Adwords Script来实现自动管理。本文分享的脚本目的是为了监控高价关键词,一旦关键词出价超过最高出价限制,将发送邮件通知,并将关键词停掉,同时给关键词打上标签,方便修改,为了做记录,还将关键词保存到Google Sheet,希望这个脚本对您有帮助!交流QQ:5778101

function main() {
var labelName = “High Price Keywords”;
var labelDescription = “High Price Keywords AverageCpc More Than $0.8″;
var labelColor = “red”;
var timerange = “TODAY”;
var eMail = “15818869905@139.com”;
var tableStr=”";
var keywordCount=0;
var subject = “High Price Keywords Alert”;

AdWordsApp.createLabel(labelName, labelDescription, labelColor);

var keywordIter = AdWordsApp.keywords()
.forDateRange(timerange)
.withCondition(“AverageCpc>0.8″)
.withCondition(“Clicks > 1″)
.withCondition(“Status = ENABLED”).get();

tableStr+=”<table width=’1200′ style=’border:solid 1px #000;border-collapse: collapse;font-size:13px;’>”;
tableStr+=”<tr style=’color:#fff;font-weight:850;font-size:18px’>”;
tableStr+=”<th width=’200′ bgcolor=’#005f83′ style=’border:solid 1px #000;’>Campaign</th>”;
tableStr+=”<th width=’300′ bgcolor=’#005f83′ style=’border:solid 1px #000;’>Group</th>”;
tableStr+=”<th width=’300′ bgcolor=’#005f83′ style=’border:solid 1px #000;’>Keywords</th>”;
tableStr+=”<th width=’100′ bgcolor=’#005f83′ style=’border:solid 1px #000;’>AverageCpc</th>”;
tableStr+=”<th width=’100′ bgcolor=’#005f83′ style=’border:solid 1px #000;’>Click</th>”;
tableStr+=”<th width=’100′ bgcolor=’#005f83′ style=’border:solid 1px #000;’>Impression</th>”;
tableStr+=”<th width=’100′ bgcolor=’#005f83′ style=’border:solid 1px #000;’>Cost</th>”;
tableStr+=”</tr>”;

while (keywordIter.hasNext()) {
var keyword = keywordIter.next();
var stats = keyword.getStatsFor(timerange);
keywordCount++;
keyword.applyLabel(labelName);
keyword.pause();
tableStr+=”<tr>”;
tableStr+=”<td style=’border:solid 1px #000;’>”+keyword.getCampaign().getName()+”</td>”;
tableStr+=”<td style=’border:solid 1px #000;’>”+keyword.getAdGroup().getName()+”</td>”;
tableStr+=”<td style=’border:solid 1px #000;’>”+keyword.getText()+”</td>”;
tableStr+=”<td style=’border:solid 1px #000;’>”+stats.getAverageCpc().toFixed(2)+”</td>”;
tableStr+=”<td style=’border:solid 1px #000;’>”+stats.getClicks()+”</td>”;
tableStr+=”<td style=’border:solid 1px #000;’>”+stats.getImpressions()+”</td>”;
tableStr+=”<td style=’border:solid 1px #000;’>”+stats.getCost()+”</td>”;
tableStr+=”</tr>”;
}
tableStr+=”</table>”;

if(keywordCount>0)
sendEmail(eMail,subject,tableStr,keywordCount);
writeToSheet(timerange);
createTask(keywordCount);
}

function createTask(keywordsCount) {
var TASK_LIST_ID = ‘@default’;
var task = Tasks.newTask();
task.title = ‘关键词价格调整’;
task.notes = “有”+keywordsCount+”关键词价格过高,已暂停,等待调整!”;

var dueDate = new Date();
dueDate.setDate(dueDate.getDate() + 5);
task.due = dueDate.toISOString();
var newTask = Tasks.Tasks.insert(task, TASK_LIST_ID);

}

function sendEmail(to,subject,message,keywordCount){
var logoUrl =
‘http://www.57078101.com/wp-content/themes/co1/img/logo.png’;
var logoBlob = UrlFetchApp
.fetch(logoUrl)
.getBlob()
.setName(‘logoBlob’);
MailApp.sendEmail({
to: to,
subject: subject,
htmlBody: “<img src=’cid:logo’><br><br><Br>” +message,
inlineImages:
{
logo: logoBlob
}
});
Logger.log(“有”+keywordCount+”关键词需要调整”);

}
function writeToSheet(timerange){
var today = new Date();
today.getDate();
Utilities.formatDate(today, “PST”, “yyyy-MM-dd”);
var spreadsheet = SpreadsheetApp.create(“High Price Keywords Check for ” + today);
var sheet = spreadsheet.getActiveSheet();
setSheet(sheet);
setHeadings(sheet);
setSheetData(sheet,timerange);
}

function setSheetData(sheet,timerange){
var maxAverageCpc=0.8;
var report = AdWordsApp.report(
“SELECT CampaignName,AdGroupName,AverageCpc,Clicks,Impressions,Cost ” +
” FROM KEYWORDS_PERFORMANCE_REPORT ” +
” WHERE ” +
” AverageCpc > “+maxAverageCpc +
” AND Clicks > 1 ” +
” AND Status = ENABLED ” +

” DURING “+timerange);
var rows = report.rows();

while(rows.hasNext()) {
var row = rows.next();
writeData(sheet,row);
}
}

function setSheet(sheet){

sheet.setColumnWidth(1, 300); // Campaign
sheet.setColumnWidth(2, 300); // Ad Group
sheet.setColumnWidth(3, 300); // Keywords
var cell = sheet.getRange(“D:G”);// Centers Columns D:G
cell.setHorizontalAlignment(“center”);
setHeadings(sheet);
var cell = sheet.getRange(“A:G”);
cell.setFontFamily(“Lato”);
cell.setFontSize(“10″);
}

function setHeadings(sheet){
//sheet.getActiveSheet();
var range = sheet.getRange(“A1:G1″);
range.setFontColor(“White”);
range.setBackground(“#7492af”);
sheet.getRange(“A4″).setValue(“Campaign”);
sheet.getRange(“B4″).setValue(“Ad Group”);
sheet.getRange(“C4″).setValue(“Keywords”);
sheet.getRange(“D4″).setValue(“Average Cpc”);
sheet.getRange(“E4″).setValue(“Click”);
sheet.getRange(“F4″).setValue(“Impression”);
sheet.getRange(“G4″).setValue(“Cost”);
}

function writeData(sheet,dataRow){
var campaignName = dataRow['CampaignName'];
var adGroupName = dataRow['AdGroupName'];
var keywords = dataRow['Keywords'];
var averageCpc = dataRow['AverageCpc'];
var clicks = dataRow['Clicks'];
var impressions = dataRow['Impressions'];
var cost = dataRow['Cost'];
parseFloat(averageCpc);
Math.round(averageCpc);
parseFloat(cost);
sheet.appendRow([campaignName, adGroupName,keywords, averageCpc, clicks, impressions, cost]);
}

发表评论