0

私は次の Google Adwords スクリプトを持っています。これは基本的に、wunderground から天気を取り戻すことによって毎日の Adword 入札予算を自動的に調整します (製品は天候に依存するため)。

それは問題なく動作し、これは一部の人にとっては簡単な解決策かもしれませんが、これは私の言語ではありません.

現時点では、Google アドワーズから (「getBudget」と呼ばれる関数) と呼ばれるライブの 1 日の予算が返されます。私がやりたいことは、既に接続されている Excel シートから 1 日の予算を取り戻すことです。以下の Excel シートから、キャンペーン名と場所名が Excel シートから返されていることがわかります。dbudget という 3 番目の列が表示されることを付け加えましたが、これは問題ありません。

私の問題はこの行です

if (keyword.getBudget()) { keyword.setBudget(keyword.getBudget() * bidMultiplier); } }

その変数 (dudget) を戻し、ここで使用したいので、getbudget ではなく dbudget を使用します

すなわち

if keyword.dbudget()) { keyword.setBudget(keyword.dbudget() * bidMultiplier); } }

しかし、それは現時点では機能しませんか?

スクリプトは

    ----------------

// Register for an API key at http://www.wunderground.com/weather/api/
// and enter the key below.
var WEATHER_UNDERGROUND_API_KEY = "";

// Create a copy of http://goo.gl/IPZo3 and enter the URL below.
var SPREADSHEET_URL = "";


/**
 * The code to execute when running the script.
 */
function main() {
  var data = getSpreadsheetData(SPREADSHEET_URL);
  for (var i = 0; i < data.length; i++) {
    var row = data[i];
    var campaignName = row[0];
    var locationName = row[1];
    var dbudget = row[2];
    try {
      var weather = getWeather(locationName);
      Logger.log(format('Weather for {0} is {1} °F, {2} mph, and {3} in of rain. Current Daily Budget of £{4}',
          locationName, weather['temp_f'], weather['wind_mph'],
          weather['precip_today_in'], dbudget));
    } catch (error) {
      Logger.log(format('Error getting weather for {0}: {1}', locationName, error));
      continue;
    }
    var bidMultiplier = getBidMultiplier(weather);
    if (bidMultiplier != 1) {
      Logger.log(format('Setting bids to {0}% for campaign "{1}"',
          Math.floor(bidMultiplier * 100), campaignName));
      adjustBids(campaignName, bidMultiplier);
    } else {
      Logger.log(format('No bid changes for campaign "{0}".', campaignName));
    }
  }
}

/**
 * Retrieves the campaign names and weather locations from the spreadsheet.
 * @param {string} spreadsheetUrl The URL of the spreadsheet.
 * @return {Array.<Array.<string>>} an array of campaign names and location
 *     names.
 */
function getSpreadsheetData(spreadsheetUrl) {
  var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
  var sheet = spreadsheet.getSheets()[0];
  var range =
    sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
  return range.getValues();
}

/**
 * Retrieves the weather for a given location, using the Weather Underground
 * API.
 * @param {string} location The location to get the weather for.
 * @return {Object.<string, string>} The weather attributes and values, as
 *     defined in the API.
 */
function getWeather(location) {
  var url = format('http://api.wunderground.com/api/{0}/conditions/q/{1}.json',
      encodeURIComponent(WEATHER_UNDERGROUND_API_KEY),
      encodeURIComponent(location));
  var response = UrlFetchApp.fetch(url);
  if (response.getResponseCode() != 200) {
    throw format('Error returned by API: {1}', response.getContentText());
  }
  var result = JSON.parse(response.getContentText());
  if (!result['current_observation']) {
    throw format('Invalid location: {0}', location);
  }
  return result['current_observation'];
}

/**
 * Determines the bid multiplier to use based on the weather data.
 * @param {Object} weather The weather data to analyze.
 * @return (number) The bid multiplier to apply.
 */
function getBidMultiplier(weather) {
  // Higher score means higher bids.
  var score = 0;
  // Temperature.
  if (weather['temp_f'] < 30) {score-= 5;} 
  else if (weather['temp_f'] >= 30 && weather['temp_f'] <= 35 ) {score-= 4;}
  else if (weather['temp_f'] >= 36 && weather['temp_f'] <= 40 ) {score-= 3;}
  else if (weather['temp_f'] >= 41 && weather['temp_f'] <= 45 ) {score-= 2;}
  else if (weather['temp_f'] >= 46 && weather['temp_f'] <= 50 ) {score-= 1;}
  else if (weather['temp_f'] >= 51 && weather['temp_f'] <= 55 ) {score-= 0;}
  else if (weather['temp_f'] >= 56 && weather['temp_f'] <= 60 ) {score-= 0;}
  else if (weather['temp_f'] >= 61 && weather['temp_f'] <= 65 ) {score+= 1;}
  else if (weather['temp_f'] >= 66 && weather['temp_f'] <= 70 ) {score+= 2;}
  else if (weather['temp_f'] >= 71 && weather['temp_f'] <= 75 ) {score+= 3;}
  else if (weather['temp_f'] >= 76 && weather['temp_f'] <= 80 ) {score+= 4;}
  else if (weather['temp_f'] > 81) {score+= 5;} 

  // Increase/decrease bid by 10% for each score point.
  return 1 + (0.1 * score);
}

/**
 * Adjusts the bids on all keywords in the campaign using the bid multiplier.
 * @param {string} campaignName The name of the campaign.
 * @param {number} bidMultiplier The bid multiplier to use.
 */
function adjustBids(campaignName, bidMultiplier, dbudget) {
  var selector = AdWordsApp.campaigns().withCondition(
      format('CampaignName = "{0}"', campaignName));
  var iterator = selector.get();
  while (iterator.hasNext()) {
    var keyword = iterator.next();
    if (keyword.getBudget()) {
      keyword.setBudget(keyword.getBudget() * bidMultiplier);  }
     }


}

/**
 * Formats a string using "{0}" style placeholders.
 * @param {string} str The format string.
 * @param {...string} var_args The values to insert into the format string.
 * @return {string} The formatted string.
 */
function format(str, var_args) {
  for (var i = 0; i < arguments.length - 1; i++) {
    var reg = new RegExp('\\{' + i + '\\}', 'gm');
    str = str.replace(reg, arguments[i + 1]);
  }
  return str;
}
4

1 に答える 1