I already got so much help in this forum and maybe you guys can help me once more.
I am currently creating a Dashboard for multiple google ads accounts and am running into multiple problems.
The general idea is, that for each customer that holds a specific label there will be a number of charts created in a spreadsheet.
My current approach is to get all the reports for the accounts in parallel and merge them (sequentially) into an Array, then I want to create the charts(sequetially) from this array.
My current problem is, that I dont know how to merge all the reports in the array. I think I have to use a callback function once the report is pulled, because I want to have all charts in on sheet next to each other, so they have to be prcessed in sequence (correct me if i am wrong)
my current approach looks like this:
var SCRIPT_LABEL = 'LABEL';
function run() {
var Spreadsheet_URL = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var ss = SpreadsheetApp.openByUrl(Spreadsheet_URL);
var report = getReportforAccount();
}
// this will execute your script sequentially accounts and is only used for accounts in excess of 50
function executeInSequence(sequentialIds, executeSequentiallyFunc) {
sequentialIds.forEach(function (accountId) {
var account = MccApp.accounts().withIds([accountId]).get().next();
MccApp.select(account);
executeSequentiallyFunc();
});
}
// out custom main function responsible for executing the run function
function main() {
try {
var accountSelector = MccApp.accounts().orderBy('Name');
if (SCRIPT_LABEL) {
accountSelector = accountSelector.withCondition("LabelNames CONTAINS '" + SCRIPT_LABEL + "'");
}
var accountIterator = accountSelector.get();
var accountIds = [];
while (accountIterator.hasNext()) {
var account = accountIterator.next();
accountIds.push(account.getCustomerId());
}
var parallelIds = accountIds.slice(0, 50);
var sequentialIds = accountIds.slice(50);
MccApp.accounts()
.withIds(parallelIds)
.executeInParallel('run');
if (sequentialIds.length > 0) {
executeInSequence(sequentialIds, run);
}
}
catch (exception) {
Logger.log('Running on non-MCC account.');
run();
}
}
function insertCharts(reportArray,chartSheet){
//TODO create charts from report and insert them into Spreadsheet
}
function getReportforAccount() {
var report = AdWordsApp.report("SELECT Date, Clicks, Conversions, Cost, AllConversionValue, Device FROM ACCOUNT_PERFORMANCE_REPORT DURING LAST_30_DAYS"); //
var rows = report.rows();
while (rows.hasNext()) {
var row = rows.next();
var clicks = row["Clicks"];
var conversions = row["Conversions"];
var cost = row["Cost"];
var allConversionValue = row["AllConversionValue"];
var device = row["Device"];
var date = row["Date"];
}
return report;
}
My idea now is to add a callback function to run() that will merge all the results in one array, then another function that creats the charts from said array and then inserts them into the spreadsheet. Unfortunately i have not worked with callbacks and i am puzzled as where to put the callback function so that it will actually run sequentially.
Any help is greatly appreciated.