0

我有以下 google adwords 脚本,它基本上通过从 wunderground 带回天气来自动调整每日 adword 出价预算(因为产品取决于天气)

它工作正常,这对某些人来说可能是一个简单的解决方案,但这不是我的语言。

目前,它从 google adwords 中带回了名为(名为“getBudget”的函数)的实时每日预算。我想要做的是从我已经连接的 Excel 表中带回每日预算。您可以从下面的 excel 表中看到它从 excel 表中带回了活动名称和位置名称。我补充说,它带回了名为 dbudget 的第三列,这很好,但是

我的问题是这条线

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

我想带回那个变量(dudget)并在这里使用它,所以它使用 dbudget 而不是 getbudget

IE

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

0

您似乎想使用已连接的电子表格中的预算值;而不是从 Google AdWords 函数返回的预算值。那是对的吗?

如果是这样,那么您需要做的是将 dbudget 值从您计算的任何位置传递到函数中并使用它。

似乎您在 main() 内部计算 dbudget ,因此您会将其传递给 adjustBids()

由于 adjustBids 在其函数声明中已经将 dbudget 作为第三个参数,因此您不必更改它。相反,您只需更改对 adjustBids 的调用(来自主函数)

adjustBids(campaignName, bidMultiplier, dbudget);

然后像这样更改 adjustBids 函数:

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

请记住,这假设您与电子表格中的广告系列名称与 Google Ad 函数将返回的广告系列名称保持 1:1 的关系。如果存在不匹配,那么事情可能会变得……奇怪。

于 2013-04-04T12:40:35.613 回答