大家下午好人
我需要开发一个adwords脚本。这将是我开始实施的第一个自定义脚本。以前的所有脚本都是谷歌的示例脚本。
我有一个MCC帐户,其中包含许多客户帐户。我想要一个针对MCC中每个帐户运行的脚本,并查询该月中每天的数据。然后,我希望将其添加到Google电子表格中。
我已经写了一个我想要发生的事情的例子,但需要指出如何到达那里
我已经手动执行了上述操作一段时间了,它开始变得单调乏味。我还发现,分析中的转换不会过滤掉几天,所以它会回复我处理一些系统来提取数据。
如果我到达脚本工作的地步,那么我会安排它在该月的最后一天或下个月的第一天运行
非常感谢任何帮助
***** UPDATE *****
我开发了以下代码,但我的输出有问题。
我期待着一个谷歌电子表格 - 每月每天的工作表 - 我的mcc中所有子帐户的所有广告系列的列表 - 与当月相关日期的每个广告系列相关的统计信息
我得到的是
电子表格中的其余工作表仅包含列标题
var row = 2;
var reportMonth;
var reportYear;
var reportDays;
var reportMonthName;
function main() {
var mccAccount = AdWordsApp.currentAccount();
var childAccounts = MccApp.accounts().get();
reportMonth = getPreviousMonth();
Logger.log("Report Month" + reportMonth);
reportYear = new Date().getFullYear()
if (reportMonth = 12) {
reportYear = reportYear - 1;
}
Logger.log("Report Year" + reportYear);
reportDays = daysInMonth(reportMonth,reportYear);
Logger.log("Report Days" + reportDays);
reportMonthName = monthNumberToName(reportMonth);
Logger.log("Month Name" + reportMonthName);
// Create a new spreadsheet (will have private access for you only).
var reportName = "Weekly QS Report – " + reportMonthName;
var spreadsheet = SpreadsheetApp.create(reportName);
for (i=1; i < (reportDays+1);i++) {
var reportDay;
if (i < 10) {
reportDay = "" + "0" + i;
}
else
{
reportDay = i;
}
var reportDate = "" + reportYear + reportMonth + reportDay;
Logger.log("Create Sheet" + reportDate);
var sheet = spreadsheet.insertSheet()
sheet.setName(i)
// Write header row.
sheet.getRange("A1").setValue("Account");
sheet.getRange("B1").setValue("Campaign");
sheet.getRange("C1").setValue("Impressions");
sheet.getRange("D1").setValue("Clicks");
sheet.getRange("E1").setValue("Cost");
while (childAccounts.hasNext()) {
var childAccount = childAccounts.next();
MccApp.select(childAccount);
// Select campaigns under the client account
var campaignIterator = AdWordsApp.campaigns().get();
// Write body of report.
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
Logger.log("Report Date" + reportDate);
var stats = campaign.getStatsFor(reportDate,reportDate);
sheet.getRange("A" + row).setValue(childAccount.getName());
sheet.getRange("B" + row).setValue(campaign.getName());
sheet.getRange("C" + row).setValue(stats.getImpressions());
sheet.getRange("D" + row).setValue(stats.getClicks());
sheet.getRange("E" + row).setValue(stats.getCost());
row ++;
}
}
}
MccApp.select(mccAccount);
}
function getDateInfo() {
var premonth = getPreviousMonth();
var dayIn = daysInMonth(premonth,year);
}
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
function getPreviousMonth() {
var d = new Date();
var n = d.getMonth();
n = n - 1;
if (n = -1) {
n = 12;
}
return n;
}
function monthNumberToName (monthnum) {
var months = [];
months[1] = 'january';
months[2] = 'february';
months[3] = 'march';
months[4] = 'april';
months[5] = 'may';
months[6] = 'june';
months[7] = 'july';
months[8] = 'august';
months[9] = 'september';
months[10] = 'october';
months[11] = 'november';
months[12] = 'december';
var myMonthName = months[monthnum];
return myMonthName;
};
任何帮助都将不胜感激,感谢先生们
答案 0 :(得分:0)
如果您已经展示了您已经完成的研究(如果有的话),那就太好了。但是,我假设您有基本的编程语言。因此,您可以查看以下链接以供参考,建立在您的知识基础之上,然后是您的代码。
Mcc帐户详情及其他行动:
1)https://developers.google.com/adwords/scripts/docs/reference/mccapp/mccapp
工作表操作和将项目移动到工作表:
1)https://developers.google.com/apps-script/reference/spreadsheet/
2)https://developers.google.com/apps-script/reference/spreadsheet/sheet
要选择第二天,您应该使用JavaScript Date对象开始循环并以适当的条件结束。
答案 1 :(得分:0)
以上是上述问题的答案,
但是我不得不对原始设计进行一些更改
我发现我在脚本运行时遇到了30超时
然后我实施了executeinparallel来解决它
但是由于我拥有的帐户大小,它在超时时仍然失败
解决方案是将返回的数据发送到另一个函数,该函数将其写入电子表格
我会在某个时候根据解决方案发布我的答案,但我还有其他一些紧迫的事情需要关注
请随时详细说明我的答案,因为这可能会帮助我节省一些时间
var row = 2;
var reportMonth;
var reportYear;
var reportDays;
var reportMonthName;
function main() {
var accountSelector = MccApp.accounts()
.withLimit(50)
.withCondition("Impressions > 100")
.forDateRange("LAST_MONTH")
.orderBy("Clicks DESC");
accountSelector.executeInParallel("processClientAccount", "afterProcessAllClientAccounts");
}
function processClientAccount() {
var clientAccount = AdWordsApp.currentAccount();
reportMonth = getPreviousMonth();
Logger.log("Report Month" + reportMonth);
reportYear = new Date().getFullYear();
if (reportMonth = 12) {
reportYear = reportYear - 1;
}
Logger.log("Report Year" + reportYear);
reportDays = daysInMonth(reportMonth,reportYear);
Logger.log("Report Days" + reportDays);
reportMonthName = monthNumberToName(reportMonth);
Logger.log("Month Name" + reportMonthName);
// Create a new spreadsheet (will have private access for you only).
var reportName = clientAccount.getName() + " Weekly QS Report – " + reportMonthName;
var spreadsheet = SpreadsheetApp.create(reportName);
for (i=1; i < (reportDays+1);i++) {
row = 2;
var reportDay;
if (i < 10) {
reportDay = "" + "0" + i;
}
else
{
reportDay = i;
}
var reportDate = "" + reportYear + reportMonth + reportDay;
Logger.log("Create Sheet" + reportDate);
var sheet = spreadsheet.insertSheet();
sheet.setName(i)
// Write header row.
row = row + 2;
Logger.log("Writing Header Row:" + row)
sheet.getRange("A" + row).setValue("Account");
sheet.getRange("B" + row).setValue("Campaign");
sheet.getRange("C" + row).setValue("Impressions");
sheet.getRange("D" + row).setValue("Clicks");
sheet.getRange("E" + row).setValue("Cost");
sheet.getRange("F" + row).setValue("Average CPC");
sheet.getRange("G" + row).setValue("CTR");
sheet.getRange("H" + row).setValue("Avg. Pos");
sheet.getRange("I" + row).setValue("Avg. PageView");
sheet.getRange("J" + row).setValue("Avg. Time");
sheet.getRange("K" + row).setValue("Conv");
sheet.getRange("L" + row).setValue("Conv Rate");
row ++;
// Select campaigns under the client account
var campaignIterator = AdWordsApp.campaigns().get();
// Write body of report.
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
Logger.log("Report on " + clientAccount.getName() + "," + campaign.getName() + " on " + reportDate);
var stats = campaign.getStatsFor(reportDate,reportDate);
sheet.getRange("A" + row).setValue(clientAccount.getName());
sheet.getRange("B" + row).setValue(campaign.getName());
sheet.getRange("C" + row).setValue(stats.getImpressions());
sheet.getRange("D" + row).setValue(stats.getClicks());
sheet.getRange("E" + row).setValue(stats.getCost());
sheet.getRange("F" + row).setValue(stats.getAverageCpc());
sheet.getRange("G" + row).setValue(stats.getCtr());
sheet.getRange("H" + row).setValue(stats.getAveragePosition());
sheet.getRange("I" + row).setValue(stats.getAveragePageviews());
sheet.getRange("J" + row).setValue(stats.getAverageTimeOnSite());
sheet.getRange("K" + row).setValue(stats.getConvertedClicks());
sheet.getRange("L" + row).setValue(stats.getClickConversionRate());
row ++;
}
}
// optionally, return a result, as a text.
return "";
}
function afterProcessAllClientAccounts(results) {
for (var i = 0; i < results.length; i++) {
var result = results[i].getReturnValue();
// Process the result further
}
}
function getDateInfo() {
var premonth = getPreviousMonth();
var dayIn = daysInMonth(premonth,year);
}
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
function getPreviousMonth() {
var d = new Date();
var n = d.getMonth();
n = n - 1;
if (n = -1) {
n = 12;
}
return n;
}
function monthNumberToName (monthnum) {
var months = [];
months[1] = 'january';
months[2] = 'february';
months[3] = 'march';
months[4] = 'april';
months[5] = 'may';
months[6] = 'june';
months[7] = 'july';
months[8] = 'august';
months[9] = 'september';
months[10] = 'october';
months[11] = 'november';
months[12] = 'december';
var myMonthName = months[monthnum];
return myMonthName;
};