0
votes

Good Afternoon Everyone

I am in need of developing an adwords script. This will be the first custom script that i have embarked on. All previous scripts have been example scripts from google.

I have an MCC account which contains a number of client accounts. I want a script that runs for each account within the MCC, and queries data for each day in the month. I then want this to be added to a google spreadsheet.

I have written an example of what i would like to happen, but need to pointers on how to get there

  1. Get List of client accounts
  2. Select Account
  3. List item
  4. Select 1st day in month
  5. List item
  6. generate report create a google document, creating a sheet for each day
  7. select next day. Stop loop if last day in month
  8. select next account

I have been performing the above manually for a while now and its starting to get tedious. I also find that the conversions within analytics don't filter down for a couple of days so its replying on me processing a number of ystems to pull the data in.

If i get to the point where the script is working then i will schedule it to run either on the last day of the month or the first day of the following month

Any help would be greatly appreciated

*****UPDATE*****

i have developed the following code but i am having some problem with the output.

I am expecting a google spreadsheet with - a sheet for each day of the month - a list of all campaigns, of all of the child account i have in my mcc - stats relating to each campaign for the relevant day in the month

what i am getting is

  • a sheet for each day of the month
  • the first sheet only is populated with the data
  • the rest of the sheets within the spreadsheet only contain the column headings

    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;
    
    };
    

Any help would be appreciated, thanks in advance guys

2

2 Answers

0
votes

It would have been nice if you would have shown the research that you've done (If any). However, I assume that you have basic programming language. So you can check out the following links for reference, build up on your knowledge and then on your code.

Mcc Account Details and other actions:

1) https://developers.google.com/adwords/scripts/docs/reference/mccapp/mccapp

Sheet Manipulation and moving items to the sheet:

1) https://developers.google.com/apps-script/reference/spreadsheet/

2) https://developers.google.com/apps-script/reference/spreadsheet/sheet

To select next day you should start the loop with the JavaScript Date object and end with suitable condition.

0
votes

here is the answer to the question above,

however i have had to make some changes to the original design

I found that i was hitting the 30 timeout on script running

i then implemented the executeinparallel to resolve it

however due to the size of the accounts that i have it still fails on the timeout

the solution to this is to send the returned data to another function which writes it to the spreadsheet

I will at some point post my answer as per the solution, but i have some other pressing matters to attend to

please feel free to elaborate on my answer as it might help me an others save some time

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;

};