0

From the following question, I try to use part of the following code in an Adwords script

function runMe() {
  var startTime= (new Date()).getTime();

  //do some work here

  var scriptProperties = PropertiesService.getScriptProperties();
  var startRow= scriptProperties.getProperty('start_row');
  for(var ii = startRow; ii <= size; ii++) {
    var currTime = (new Date()).getTime();
    if(currTime - startTime >= MAX_RUNNING_TIME) {
      scriptProperties.setProperty("start_row", ii);
      ScriptApp.newTrigger("runMe")
               .timeBased()
               .at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
               .create();
      break;
    } else {
      doSomeWork();
    }
  }

  //do some more work here

}

but I got ReferenceError: "PropertiesService" is not defined. (line 170) from google-apps-script. How could I fix that issue? Can you tell me if it works for you in Adwords?

UPDATED

Here is the function I build in considering the previous function :

function adjustCPCmax() {
    //min CPC
    var minCPC = 0.50;

    var accountIterator = MccApp.accounts().get();
    var mccAccount = AdWordsApp.currentAccount();
    while(accountIterator.hasNext()) { 
        var account = accountIterator.next();
        Logger.log('============================================== ' + account.getName() + ' ====================================================')
        MccApp.select(account)

        var campaignIterator = AdWordsApp.campaigns().get();
        while (campaignIterator.hasNext()) {
            var campaign = campaignIterator.next();
            try {
                var maxCPC = getMaxCPC(account, campaign)
            }
            catch(e) {
            }

            if (maxCPC) {
                var startTime= (new Date()).getTime();

                Logger.log('The entrence worked with max CPC : ' + maxCPC + '\n')
                keywordIterator = campaign.keywords().get();
                while (keywordIterator.hasNext()) {
                    var keyword= keywordIterator.next()
                    var keywordId = Number(keyword.getId()).toPrecision()

                    Logger.log('THE NAME OF THE KEYWORDID IS ' + keywordId + '\n')

                    var report = AdWordsApp.report(
                               'SELECT Id, Criteria, CampaignName, CpcBid, FirstPageCpc, FirstPositionCpc, TopOfPageCpc, Criteria ' +
                               'FROM   KEYWORDS_PERFORMANCE_REPORT ' +
                               'WHERE ' + 
                               'Id = ' + keywordId);
                    var rows = report.rows();
                    while(rows.hasNext()) {
                        var row = rows.next();
                        var keywordIdReport = row['Id'];
                        var keywordNameReport = row['Criteria'];
                        var campaignName = row['CampaignName'];
                        var cpcBid = row['CpcBid'];
                        var firstPageCpc = row['FirstPageCpc'];
                        var firstPositionCpc = row['FirstPositionCpc'];
                        var topOfPageCpc = row['TopOfPageCpc'];

                        Logger.log('INFO')
                        Logger.log(keyword.getText())
                        Logger.log(keywordId)
                        Logger.log(keywordNameReport)
                        Logger.log(keywordIdReport + '\n')

                        if (keywordId === keywordIdReport) {

                            if (firstPositionCpc && (firstPositionCpc > 0 && firstPositionCpc <= maxCPC)) {
                                var newCPC = firstPositionCpc;
                            } else if (topOfPageCpc  && (topOfPageCpc > 0 && topOfPageCpc <= maxCPC)) {
                                var newCPC = topOfPageCpc;
                            } else if (firstPageCpc && (firstPageCpc > 0 && firstPageCpc <= maxCPC )) {
                                var newCPC = firstPageCpc;
                            } else {
                                var newCPC = minCPC;
                            }

                            Logger.log('KeywordIdReport :' + keywordIdReport)
                            Logger.log('campaignName :' + campaignName)
                            Logger.log('CPCbid :' + cpcBid)
                            Logger.log('firstPositionCpc : ' + firstPositionCpc)
                            Logger.log('topOfPageCpc : ' + topOfPageCpc)
                            Logger.log('firstPageCpc : ' + firstPageCpc)
                            Logger.log('NewCPC : ' + newCPC + '\n')

                            keyword.bidding().setCpc(newCPC)
                            break;
                        }
                    }
                    var REASONABLE_TIME_TO_WAIT = 4*6000;
                    var MAX_RUNNING_TIME = 1*6000;
                    try {
                        var scriptProperties = PropertiesService.getScriptProperties();
                    }
                    catch(e){
                        Logger.log(e)
                    }
                    var startRow= scriptProperties.getProperty('start_row');
                    for(var ii = startRow; ii <= size; ii++) {
                        var currTime = (new Date()).getTime();
                        if(currTime - startTime >= MAX_RUNNING_TIME) {
                            scriptProperties.setProperty("start_row", ii);
                            ScriptApp.newTrigger("runMe")
                                .timeBased()
                                .at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
                                .create();
                            break;
                        }
                    }
                }
            }
        }
    }
    MccApp.select(mccAccount);
}

From that function, what could I do to fix that problem? It is bizarre because I could find documentation on the subject.

Dave
  • 85
  • 10

1 Answers1

1

You are using AdWords Script which is different from Google Apps Script even though they share many APIs. Google Apps Script has access to Properties Service, AdWords Script does not. Evidence: the AdWords Script reference (screenshot) does not include Properties Service among Script Services.

You are using script properties to store one number, the current row number. An alternative is to store it in a spreadsheet, since AdWords Script has access to Spreadsheet service. So, at the beginning of the script you would have

var ss = SpreadsheetApp.openByUrl('...'); // url of some spreadsheet
var sheet = ss.getSheets()[0];    // first sheet in it
var cell = sheet.getRange("A1");  // storing the number in A1 cell

and then within the body of the script, replace

var startRow = scriptProperties.getProperty('start_row');

by

var startRow = cell.getValue() || 1;  // 1 by default 

and also replace

scriptProperties.setProperty("start_row", ii);

by

cell.setValue(ii);

This should do it.

  • Is there a way to modified `runMe()` so that it works inside an adwords script? I thought of using `Queuing` as workaround, but I can't found out how? – Dave Nov 15 '17 at 05:51
  • Thanks for your answer. The thing is the created spreadsheet needs to be temporary. So at each iteration, the script is first created and deleted at the end. Could you fix that? – Dave Nov 15 '17 at 14:23
  • Apps Script cant delete files. And I don't see any need for spreadsheet to be temporary. Just reset the stored number to 1 when all rows are processed. –  Nov 15 '17 at 17:32
  • Ok, and also `ScriptApp` is not supported by Adwords. Do you have a solution for that? – Dave Nov 15 '17 at 19:19
  • No. Without a ScriptApp you can't create triggers, so can't use this method at all. Sorry, you'll have to rethink your report-generating approach to make it run within the allowed time. Split the data into parts, or sample it at random, or something else. –  Nov 15 '17 at 19:38
  • https://stackoverflow.com/questions/47313181/exceeded-maximum-execution-time-in-google-adwords This question is for you. – Dave Nov 15 '17 at 19:45