আবহাওয়া দ্বারা বিড

বিডিং আইকন

আবহাওয়ার উপর নির্ভর করে নির্দিষ্ট পণ্য এবং পরিষেবাগুলির চাহিদা ব্যাপকভাবে পরিবর্তিত হয়। উদাহরণস্বরূপ, ঠাণ্ডা এবং বৃষ্টিপাতের তুলনায় ব্যবহারকারীরা গরম, রৌদ্রোজ্জ্বল দিনে বিনোদন পার্কের তথ্য অনুসন্ধান করার সম্ভাবনা অনেক বেশি। একটি বিনোদন পার্ক কোম্পানী আবহাওয়া সুন্দর হলে তাদের বিড বাড়াতে চাইতে পারে, কিন্তু প্রতিদিন তা করার জন্য অনেক ম্যানুয়াল কাজের প্রয়োজন হবে। Google বিজ্ঞাপন স্ক্রিপ্টের সাহায্যে, যদিও, প্রোগ্রাম্যাটিকভাবে আবহাওয়ার তথ্য আনা এবং কয়েক মিনিটের মধ্যে বিডগুলি সামঞ্জস্য করা সম্ভব।

এই স্ক্রিপ্ট প্রচারাভিযানের তালিকা এবং তাদের সংশ্লিষ্ট অবস্থান সংরক্ষণ করতে Google স্প্রেডশীট ব্যবহার করে। প্রতিটি অবস্থানের জন্য OpenWeatherMap API এ একটি কল করা হয় এবং কিছু মৌলিক নিয়ম ব্যবহার করে আবহাওয়ার অবস্থা গণনা করা হয়। যদি একটি নিয়ম সত্যে মূল্যায়ন করা হয়, তাহলে প্রচারের জন্য অবস্থানের লক্ষ্যমাত্রায় একটি সংশ্লিষ্ট অবস্থান বিড গুণক প্রয়োগ করা হয়।

কিভাবে এটা কাজ করে

স্প্রেডশীট থেকে ডেটা পড়ার মাধ্যমে স্ক্রিপ্টটি কাজ করে। স্প্রেডশীট তিনটি পৃথক শীট গঠিত:

1. ক্যাম্পেইন ডেটা

নিয়মের একটি সেট নির্ধারণ করে যে বিড মডিফায়ারগুলি প্রচারাভিযানে প্রয়োগ করা হবে যখন আবহাওয়ার অবস্থা পূরণ হয়। এখানে প্রয়োজনীয় কলাম আছে:

  • ক্যাম্পেইনের নাম : প্রচারের নাম পরিবর্তন করতে হবে।
  • আবহাওয়ার অবস্থান : যে অবস্থানের জন্য আবহাওয়ার অবস্থা পরীক্ষা করা হবে।
  • আবহাওয়ার অবস্থা : যে আবহাওয়ার জন্য এই নিয়মটি প্রয়োগ করতে হবে।
  • বিড মডিফায়ার : আবহাওয়ার শর্ত পূরণ হলে লোকেশন বিড মডিফায়ার প্রয়োগ করা হবে।
  • এতে সংশোধক প্রয়োগ করুন : বিড সংশোধকটি শুধুমাত্র আবহাওয়ার অবস্থানের সাথে মেলে এমন প্রচারাভিযান জিও লক্ষ্যে প্রয়োগ করা হবে বা সমস্ত প্রচারাভিযানের জিও লক্ষ্যমাত্রাগুলিতে প্রয়োগ করা হবে।
  • সক্রিয় : একটি নিয়ম সক্রিয় করতে Yes এবং এটি নিষ্ক্রিয় করতে No উল্লেখ করুন।

উদাহরণ

নিম্নলিখিত উদাহরণে তিনটি প্রচারাভিযান রয়েছে।

স্প্রেডশীট স্ক্রিনশট, শীট এক

পরীক্ষা প্রচারাভিযান 1 একটি সাধারণ ব্যবহারের দৃশ্যকল্পকে চিত্রিত করে। প্রচারণাটি বোস্টন, এমএকে লক্ষ্য করে এবং এর দুটি নিয়ম রয়েছে:

  1. 1.3 এর একটি বিড মডিফায়ার প্রয়োগ করুন যদি বোস্টনের আবহাওয়া, MA Sunny হয়।
  2. 0.8 এর একটি বিড মডিফায়ার প্রয়োগ করুন যদি বোস্টন, MA এর আবহাওয়া Rainy হয়।

টেস্ট ক্যাম্পেইন 2-এর টেস্ট ক্যাম্পেইন 1-এর মতো একই বিডিং নিয়ম রয়েছে, কিন্তু কানেকটিকাটকে লক্ষ্য করে।

টেস্ট ক্যাম্পেইন 3ও একই বিডিং নিয়ম ব্যবহার করে কিন্তু ফ্লোরিডাকে লক্ষ্য করে। যেহেতু ফ্লোরিডার আবহাওয়ার নিয়মগুলি সমগ্র রাজ্যে ম্যাপ করা হয়েছে, যেটি প্রচারাভিযানটি স্পষ্টভাবে লক্ষ্য করে এমন একটি অবস্থান নয়, তাই 'অ্যাপ্লাই মডিফায়ার টু' All Geo Targets সেট করা হয়েছে যাতে প্রচারাভিযানের লক্ষ্যগুলি যে শহরগুলিকে প্রভাবিত করে।

2. আবহাওয়ার তথ্য

এই শীট প্রচারাভিযানের ডেটা শীটে ব্যবহৃত আবহাওয়ার পরিস্থিতি সংজ্ঞায়িত করে। নিম্নলিখিত কলাম প্রয়োজন:

  • অবস্থার নাম : আবহাওয়ার অবস্থার নাম (উদাহরণস্বরূপ, Sunny )।
  • তাপমাত্রা : ফারেনহাইট তাপমাত্রা।
  • বৃষ্টিপাত : বৃষ্টি, মিলিমিটারে, গত ৩ ঘণ্টায়।
  • বায়ু : বাতাসের গতি, ঘণ্টায় মাইল।

স্প্রেডশীট স্ক্রিনশট, শীট দুই

উপরে দেখানো শীট দুটি আবহাওয়ার অবস্থা সংজ্ঞায়িত করে:

  1. Sunny : তাপমাত্রা 65 থেকে 80 ডিগ্রি ফারেনহাইটের মধ্যে, গত তিন ঘণ্টায় 1 মিমি বৃষ্টিপাতের নিচে, এবং বাতাসের গতিবেগ 5 মাইল প্রতি ঘণ্টার কম।
  2. Rainy : গত তিন ঘণ্টায় বৃষ্টিপাত 0 মিলিমিটারের উপরে এবং বাতাসের গতিবেগ 10 মাইল প্রতি ঘণ্টার কম।

আবহাওয়ার অবস্থা

আবহাওয়ার অবস্থা সংজ্ঞায়িত করার সময়, নিম্নরূপ মান উল্লেখ করুন:

  • below x : নির্দিষ্ট মান below x (উদাহরণস্বরূপ, below 10 )
  • above x : নির্দিষ্ট মান above x (উদাহরণস্বরূপ, above 70 )
  • x to y : নির্দিষ্ট মান হল x এবং y এর মধ্যে, অন্তর্ভুক্ত (উদাহরণস্বরূপ, 65 to 80 )

আপনি যদি একটি ঘর খালি রাখেন, তাহলে সেই প্যারামিটারটি গণনায় বিবেচনা করা হয় না। সুতরাং আমাদের উদাহরণে, যেহেতু Rainy আবহাওয়ার একটি খালি তাপমাত্রা কলাম আছে, তাই এই আবহাওয়ার অবস্থা গণনা করার সময় তাপমাত্রা বিবেচনা করা হবে না।

আবহাওয়ার অবস্থা গণনা করার সময় আবহাওয়ার অবস্থাগুলি একসাথে AND-ed হয়৷ এই উদাহরণের জন্য, Sunny আবহাওয়ার অবস্থা নিম্নরূপ মূল্যায়ন করা হয়:

const isSunny = (temperature >= 65 && temperature <= 80) && (precipitation < 1) && (wind < 5);

3. আবহাওয়া অবস্থান ডেটা

এই শীট ক্যাম্পেইন ডেটা শীটে ব্যবহৃত আবহাওয়ার অবস্থানগুলিকে সংজ্ঞায়িত করে এবং দুটি কলাম নিয়ে গঠিত:

  • আবহাওয়ার অবস্থান : এটি একটি আবহাওয়া অবস্থানের নাম, যা OpenWeatherMap API দ্বারা বোঝা যায়।
  • জিও টার্গেট কোড : এটি একটি জিও টার্গেট কোড, যা Google বিজ্ঞাপন দ্বারা বোঝা যায়।

স্ক্রিপ্টটি আপনাকে একটি একক আবহাওয়া অবস্থানের জন্য একাধিক জিও টার্গেট কোড নির্দিষ্ট করতে দেয় কারণ আবহাওয়ার অবস্থানগুলি সবসময় Google বিজ্ঞাপনে উপলব্ধ টার্গেটিং বিকল্পগুলির মতো দানাদার হয় না। একাধিক ভূ-অবস্থানে একটি একক আবহাওয়ার অবস্থান ম্যাপিং একই আবহাওয়ার অবস্থানের সাথে একাধিক সারি কিন্তু প্রতিটি সারির জন্য ভিন্ন ভিন্ন জিও কোডের মাধ্যমে করা যেতে পারে।

স্প্রেডশীট স্ক্রিনশট, শীট তিন

এই উদাহরণের জন্য, তিনটি আবহাওয়ার অবস্থান সংজ্ঞায়িত করা হয়েছে:

  1. Boston, MA : জিও কোড 10108127
  2. Connecticut : জিও কোড 1014778 , 1014743 , এবং 1014843 , কানেকটিকাটের তিনটি শহরের সাথে সম্পর্কিত
  3. Florida : জিও কোড 21142

প্রক্সিমিটি টার্গেটিং

Matching Geo Targets ব্যবহার করে প্রচারাভিযানের নিয়ম TARGETING পতাকা ব্যবহার করে টার্গেট করা অবস্থানে , টার্গেটেড প্রক্সিমিটি বা উভয় ক্ষেত্রেই প্রয়োগ করা যেতে পারে।

লোকেশন টার্গেটিং একটি লোকেশন আইডির সাথে একটি জিও কোডের সাথে মেলে।

প্রক্সিমিটি টার্গেটিং যাচাই করে যে নির্দিষ্ট অক্ষাংশ এবং দ্রাঘিমাংশ স্থানাঙ্কগুলি হ্যাভারসাইন সূত্র ব্যবহার করে প্রক্সিমিটি ব্যাসার্ধের মধ্যে রয়েছে।

স্ক্রিপ্ট যুক্তি

তিনটি শীট থেকে নিয়ম পড়ার মাধ্যমে স্ক্রিপ্ট শুরু হয়। তারপরে এটি প্রচারাভিযান পত্রক থেকে ক্রমানুসারে প্রতিটি নিয়ম কার্যকর করার চেষ্টা করে।

প্রতিটি সঞ্চালিত নিয়মের জন্য, স্ক্রিপ্ট চেক করে যে প্রচারাভিযানটি নির্দিষ্ট অবস্থানকে লক্ষ্য করে কিনা। যদি এটি করে, তাহলে স্ক্রিপ্ট বর্তমান বিড সংশোধক পুনরুদ্ধার করে।

এরপরে, OpenWeatherMap API এ একটি কল করে সেই অবস্থানের আবহাওয়ার অবস্থা পুনরুদ্ধার করা হয়। তারপরে আবহাওয়ার অবস্থার নিয়মগুলি মূল্যায়ন করা হয় যে লোকেশনের আবহাওয়া নিয়মে যা উল্লেখ করা আছে তার সাথে মেলে কিনা। যদি এটি করে, এবং নতুন বিড সংশোধক বর্তমান বিড সংশোধক থেকে আলাদা, স্ক্রিপ্ট সেই অবস্থানের জন্য বিড সংশোধককে সংশোধন করে।

আবহাওয়ার অবস্থার সাথে মেলে না, যদি বিড সংশোধকের মান একই হয়, বা যদি নিয়মের প্রয়োগ মডিফায়ার Matching Geo Targets তবে প্রচারটি নিয়মের সাথে ম্যাপ করা অবস্থানগুলিকে লক্ষ্য করে না।

সেটআপ

  • openweathermap.org- এ একটি API কী-এর জন্য নিবন্ধন করুন।
  • টেমপ্লেট স্প্রেডশীটের একটি অনুলিপি তৈরি করুন এবং আপনার প্রচারাভিযান এবং আবহাওয়ার নিয়মগুলি সম্পাদনা করুন৷
  • নিচের সোর্স কোড দিয়ে একটি নতুন স্ক্রিপ্ট তৈরি করুন।
  • স্ক্রিপ্টে OPEN_WEATHER_MAP_API_KEY , SPREADSHEET_URL , এবং TARGETING ভেরিয়েবল আপডেট করুন৷
  • প্রয়োজন অনুযায়ী চালানোর সময়সূচী।

সোর্স কোড

// Copyright 2015, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @name Bid By Weather
 *
 * @overview The Bid By Weather script adjusts campaign bids by weather
 *     conditions of their associated locations. See
 *     https://developers.google.com/google-ads/scripts/docs/solutions/weather-based-campaign-management#bid-by-weather
 *     for more details.
 *
 * @author Google Ads Scripts Team [adwords-scripts@googlegroups.com]
 *
 * @version 2.0
 *
 * @changelog
 * - version 2.0
 *   - Updated to use new Google Ads Scripts features.
 * - version 1.2.2
 *   - Add support for video and shopping campaigns.
 * - version 1.2.1
 *   - Added validation for external spreadsheet setup.
 * - version 1.2
 *   - Added proximity based targeting.  Targeting flag allows location
 *     targeting, proximity targeting or both.
 * - version 1.1
 *   - Added flag allowing bid adjustments on all locations targeted by
 *     a campaign rather than only those that match the campaign rule
 * - version 1.0
 *   - Released initial version.
 */

// Register for an API key at http://openweathermap.org/appid
// and enter the key below.
const OPEN_WEATHER_MAP_API_KEY = 'INSERT_OPEN_WEATHER_MAP_API_KEY_HERE';

// Create a copy of https://goo.gl/A59Uuc and enter the URL below.
const SPREADSHEET_URL = 'INSERT_SPREADSHEET_URL_HERE';

// A cache to store the weather for locations already lookedup earlier.
const WEATHER_LOOKUP_CACHE = {};

// Flag to pick which kind of targeting "LOCATION", "PROXIMITY", or "ALL".
const TARGETING = 'ALL';

/**
 * According to the list of campaigns and their associated locations, the script
 * makes a call to the OpenWeatherMap API for each location.
 * Based on the weather conditions, the bids are adjusted.
 */
function main() {
  validateApiKey();
  // Load data from spreadsheet.
  const spreadsheet = validateAndGetSpreadsheet(SPREADSHEET_URL);
  const campaignRuleData = getSheetData(spreadsheet, 1);
  const weatherConditionData = getSheetData(spreadsheet, 2);
  const geoMappingData = getSheetData(spreadsheet, 3);

  // Convert the data into dictionaries for convenient usage.
  const campaignMapping = buildCampaignRulesMapping(campaignRuleData);
  const weatherConditionMapping =
      buildWeatherConditionMapping(weatherConditionData);
  const locationMapping = buildLocationMapping(geoMappingData);

  // Apply the rules.
  for (const campaignName in campaignMapping) {
    applyRulesForCampaign(campaignName, campaignMapping[campaignName],
        locationMapping, weatherConditionMapping);
  }
}

/**
 * Retrieves the data for a worksheet.
 *
 * @param {Object} spreadsheet The spreadsheet.
 * @param {number} sheetIndex The sheet index.
 * @return {Array} The data as a two dimensional array.
 */
function getSheetData(spreadsheet, sheetIndex) {
  const sheet = spreadsheet.getSheets()[sheetIndex];
  const range =
      sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
  return range.getValues();
}

/**
 * Builds a mapping between the list of campaigns and the rules
 * being applied to them.
 *
 * @param {Array} campaignRulesData The campaign rules data, from the
 *     spreadsheet.
 * @return {!Object.<string, Array.<Object>> } A map, with key as campaign name,
 *     and value as an array of rules that apply to this campaign.
 */
function buildCampaignRulesMapping(campaignRulesData) {
  const campaignMapping = {};
  for (const rules of campaignRulesData) {
    // Skip rule if not enabled.

    if (rules[5].toLowerCase() == 'yes') {
      const campaignName = rules[0];
      const campaignRules = campaignMapping[campaignName] || [];
      campaignRules.push({
          'name': campaignName,

          // location for which this rule applies.
          'location': rules[1],

          // the weather condition (e.g. Sunny).
          'condition': rules[2],

          // bid modifier to be applied.
          'bidModifier': rules[3],

          // whether bid adjustments should by applied only to geo codes
          // matching the location of the rule or to all geo codes that
          // the campaign targets.
          'targetedOnly': rules[4].toLowerCase() ==
                          'matching geo targets'
      });
      campaignMapping[campaignName] = campaignRules;
    }
  }
  Logger.log('Campaign Mapping: %s', campaignMapping);
  return campaignMapping;
}

/**
 * Builds a mapping between a weather condition name (e.g. Sunny) and the rules
 * that correspond to that weather condition.
 *
 * @param {Array} weatherConditionData The weather condition data from the
 *      spreadsheet.
 * @return {!Object.<string, Array.<Object>>} A map, with key as a weather
 *     condition name, and value as the set of rules corresponding to that
 *     weather condition.
 */
function buildWeatherConditionMapping(weatherConditionData) {
  const weatherConditionMapping = {};
  for (const weatherCondition of weatherConditionData) {
    const weatherConditionName = weatherCondition[0];
    weatherConditionMapping[weatherConditionName] = {
      // Condition name (e.g. Sunny)
      'condition': weatherConditionName,

      // Temperature (e.g. 50 to 70)
      'temperature': weatherCondition[1],

      // Precipitation (e.g. below 70)
      'precipitation': weatherCondition[2],

      // Wind speed (e.g. above 5)
      'wind': weatherCondition[3]
    };
  }
  Logger.log('Weather condition mapping: %s', weatherConditionMapping);
  return weatherConditionMapping;
}

/**
 * Builds a mapping between a location name (as understood by OpenWeatherMap
 * API) and a list of geo codes as identified by Google Ads scripts.
 *
 * @param {Array} geoTargetData The geo target data from the spreadsheet.
 * @return {!Object.<string, Array.<Object>>} A map, with key as a locaton name,
 *     and value as an array of geo codes that correspond to that location
 *     name.
 */
function buildLocationMapping(geoTargetData) {
  const locationMapping = {};
  for (const geoTarget of geoTargetData) {
    const locationName = geoTarget[0];
    const locationDetails = locationMapping[locationName] || {
      'geoCodes': []      // List of geo codes understood by Google Ads scripts.
    };
    locationDetails.geoCodes.push(geoTarget[1]);
    locationMapping[locationName] = locationDetails;
  }
  Logger.log('Location Mapping: %s', locationMapping);
  return locationMapping;
}

/**
 * Applies rules to a campaign.
 *
 * @param {string} campaignName The name of the campaign.
 * @param {Object} campaignRules The details of the campaign. See
 *     buildCampaignMapping for details.
 * @param {Object} locationMapping Mapping between a location name (as
 *     understood by OpenWeatherMap API) and a list of geo codes as
 *     identified by Google Ads scripts. See buildLocationMapping for details.
 * @param {Object} weatherConditionMapping Mapping between a weather condition
 *     name (e.g. Sunny) and the rules that correspond to that weather
 *     condition. See buildWeatherConditionMapping for details.
 */
function applyRulesForCampaign(campaignName, campaignRules, locationMapping,
                               weatherConditionMapping) {
  for (const rules of campaignRules) {
    let bidModifier = 1;
    const campaignRule = rules;

    // Get the weather for the required location.
    const locationDetails = locationMapping[campaignRule.location];
    const weather = getWeather(campaignRule.location);
    Logger.log('Weather for %s: %s', locationDetails, weather);

    // Get the weather rules to be checked.
    const weatherConditionName = campaignRule.condition;
    const weatherConditionRules = weatherConditionMapping[weatherConditionName];

    // Evaluate the weather rules.
    if (evaluateWeatherRules(weatherConditionRules, weather)) {
      Logger.log('Matching Rule found: Campaign Name = %s, location = %s, ' +
          'weatherName = %s,weatherRules = %s, noticed weather = %s.',
          campaignRule.name, campaignRule.location,
          weatherConditionName, weatherConditionRules, weather);
      bidModifier = campaignRule.bidModifier;

      if (TARGETING == 'LOCATION' || TARGETING == 'ALL') {
        // Get the geo codes that should have their bids adjusted.
        const geoCodes = campaignRule.targetedOnly ?
          locationDetails.geoCodes : null;
        adjustBids(campaignName, geoCodes, bidModifier);
      }

      if (TARGETING == 'PROXIMITY' || TARGETING == 'ALL') {
        const location = campaignRule.targetedOnly ? campaignRule.location : null;
        adjustProximityBids(campaignName, location, bidModifier);
      }
    }
  }
  return;
}

/**
 * Converts a temperature value from kelvin to fahrenheit.
 *
 * @param {number} kelvin The temperature in Kelvin scale.
 * @return {number} The temperature in Fahrenheit scale.
 */
function toFahrenheit(kelvin) {
  return (kelvin - 273.15) * 1.8 + 32;
}

/**
 * Evaluates the weather rules.
 *
 * @param {Object} weatherRules The weather rules to be evaluated.
 * @param {Object.<string, string>} weather The actual weather.
 * @return {boolean} True if the rule matches current weather conditions,
 *     False otherwise.
 */
function evaluateWeatherRules(weatherRules, weather) {
  // See https://openweathermap.org/weather-data
  // for values returned by OpenWeatherMap API.
  let precipitation = 0;
  if (weather.rain && weather.rain['3h']) {
    precipitation = weather.rain['3h'];
  }
  const temperature = toFahrenheit(weather.main.temp);
  const windspeed = weather.wind.speed;

  return evaluateMatchRules(weatherRules.temperature, temperature) &&
      evaluateMatchRules(weatherRules.precipitation, precipitation) &&
      evaluateMatchRules(weatherRules.wind, windspeed);
}

/**
 * Evaluates a condition for a value against a set of known evaluation rules.
 *
 * @param {string} condition The condition to be checked.
 * @param {Object} value The value to be checked.
 * @return {boolean} True if an evaluation rule matches, false otherwise.
 */
function evaluateMatchRules(condition, value) {
  // No condition to evaluate, rule passes.
  if (condition == '') {
    return true;
  }
  const rules = [matchesBelow, matchesAbove, matchesRange];

  for (const rule of rules) {
    if (rule(condition, value)) {
      return true;
    }
  }
  return false;
}

/**
 * Evaluates whether a value is below a threshold value.
 *
 * @param {string} condition The condition to be checked. (e.g. below 50).
 * @param {number} value The value to be checked.
 * @return {boolean} True if the value is less than what is specified in
 * condition, false otherwise.
 */
function matchesBelow(condition, value) {
  conditionParts = condition.split(' ');

  if (conditionParts.length != 2) {
    return false;
  }

  if (conditionParts[0] != 'below') {
    return false;
  }

  if (value < conditionParts[1]) {
    return true;
  }
  return false;
}

/**
 * Evaluates whether a value is above a threshold value.
 *
 * @param {string} condition The condition to be checked. (e.g. above 50).
 * @param {number} value The value to be checked.
 * @return {boolean} True if the value is greater than what is specified in
 *     condition, false otherwise.
 */
function matchesAbove(condition, value) {
  conditionParts = condition.split(' ');

  if (conditionParts.length != 2) {
    return false;
  }

  if (conditionParts[0] != 'above') {
    return false;
  }

  if (value > conditionParts[1]) {
    return true;
  }
  return false;
}

/**
 * Evaluates whether a value is within a range of values.
 *
 * @param {string} condition The condition to be checked (e.g. 5 to 18).
 * @param {number} value The value to be checked.
 * @return {boolean} True if the value is in the desired range, false otherwise.
 */
function matchesRange(condition, value) {
  conditionParts = condition.replace('w+', ' ').split(' ');

  if (conditionParts.length != 3) {
    return false;
  }

  if (conditionParts[1] != 'to') {
    return false;
  }

  if (conditionParts[0] <= value && value <= conditionParts[2]) {
    return true;
  }
  return false;
}

/**
 * Retrieves the weather for a given location, using the OpenWeatherMap 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) {
  if (location in WEATHER_LOOKUP_CACHE) {
    Logger.log('Cache hit...');
    return WEATHER_LOOKUP_CACHE[location];
  }
  const url=`http://api.openweathermap.org/data/2.5/weather?APPID=${OPEN_WEATHER_MAP_API_KEY}&q=${location}`;
  const response = UrlFetchApp.fetch(url);
  if (response.getResponseCode() != 200) {
    throw Utilities.formatString(
        'Error returned by API: %s, Location searched: %s.',
        response.getContentText(), location);
  }
  const result = JSON.parse(response.getContentText());

  // OpenWeatherMap's way of returning errors.
  if (result.cod != 200) {
    throw Utilities.formatString(
        'Error returned by API: %s,  Location searched: %s.',
        response.getContentText(), location);
  }
  WEATHER_LOOKUP_CACHE[location] = result;
  return result;
}

/**
 * Adjusts the bidModifier for a list of geo codes for a campaign.
 *
 * @param {string} campaignName The name of the campaign.
 * @param {Array} geoCodes The list of geo codes for which bids should be
 *     adjusted.  If null, all geo codes on the campaign are adjusted.
 * @param {number} bidModifier The bid modifier to use.
 */
function adjustBids(campaignName, geoCodes, bidModifier) {
  // Get the campaign.
  const campaign = getCampaign(campaignName);
  if (!campaign) return null;

  // Get the targeted locations.
  const locations = campaign.targeting().targetedLocations().get();
  for (const location of locations) {
    const currentBidModifier = location.getBidModifier().toFixed(2);

    // Apply the bid modifier only if the campaign has a custom targeting
    // for this geo location or if all locations are to be modified.
    if (!geoCodes || (geoCodes.indexOf(location.getId()) != -1 &&
      currentBidModifier != bidModifier)) {
        Logger.log('Setting bidModifier = %s for campaign name = %s, ' +
            'geoCode = %s. Old bid modifier is %s.', bidModifier,
            campaignName, location.getId(), currentBidModifier);
        location.setBidModifier(bidModifier);
    }
  }
}

/**
 * Adjusts the bidModifier for campaigns targeting by proximity location
 * for a given weather location.
 *
 * @param {string} campaignName The name of the campaign.
 * @param {string} weatherLocation The weather location for which bids should be
 *     adjusted.  If null, all proximity locations on the campaign are adjusted.
 * @param {number} bidModifier The bid modifier to use.
 */
function adjustProximityBids(campaignName, weatherLocation, bidModifier) {
  // Get the campaign.
  const campaign = getCampaign(campaignName);
  if(campaign === null) return;

  // Get the proximity locations.
  const proximities = campaign.targeting().targetedProximities().get();
  for (const proximity of proximities) {
    const currentBidModifier = proximity.getBidModifier().toFixed(2);

    // Apply the bid modifier only if the campaign has a custom targeting
    // for this geo location or if all locations are to be modified.
    if (!weatherLocation ||
        (weatherNearProximity(proximity, weatherLocation) &&
           currentBidModifier != bidModifier)) {
        Logger.log('Setting bidModifier = %s for campaign name = %s, with ' +
            'weatherLocation = %s in proximity area. Old bid modifier is %s.',
            bidModifier, campaignName, weatherLocation, currentBidModifier);
        proximity.setBidModifier(bidModifier);
    }
  }
}

/**
 * Checks if weather location is within the radius of the proximity location.
 *
 * @param {Object} proximity The targeted proximity of campaign.
 * @param {string} weatherLocation Name of weather location to check within
 * radius.
 * @return {boolean} Returns true if weather location is within radius.
 */
function weatherNearProximity(proximity, weatherLocation) {
  // See https://en.wikipedia.org/wiki/Haversine_formula for details on how
  // to compute spherical distance.
  const earthRadiusInMiles = 3960.0;
  const degreesToRadians = Math.PI / 180.0;
  const radiansToDegrees = 180.0 / Math.PI;
  const kmToMiles = 0.621371;

  const radiusInMiles = proximity.getRadiusUnits() == 'MILES' ?
    proximity.getRadius() : proximity.getRadius() * kmToMiles;

  // Compute the change in latitude degrees for the radius.
  const deltaLat = (radiusInMiles / earthRadiusInMiles) * radiansToDegrees;
  // Find the radius of a circle around the earth at given latitude.
  const r = earthRadiusInMiles * Math.cos(proximity.getLatitude() *
      degreesToRadians);
  // Compute the change in longitude degrees for the radius.
  const deltaLon = (radiusInMiles / r) * radiansToDegrees;

  // Retrieve weather location for lat/lon coordinates.
  const weather = getWeather(weatherLocation);
  // Check if weather condition is within the proximity boundaries.
  return (weather.coord.lat >= proximity.getLatitude() - deltaLat &&
          weather.coord.lat <= proximity.getLatitude() + deltaLat &&
          weather.coord.lon >= proximity.getLongitude() - deltaLon &&
          weather.coord.lon <= proximity.getLongitude() + deltaLon);
}

/**
 * Finds a campaign by name, whether it is a regular, video, or shopping
 * campaign, by trying all in sequence until it finds one.
 *
 * @param {string} campaignName The campaign name to find.
 * @return {Object} The campaign found, or null if none was found.
 */
function getCampaign(campaignName) {
  const selectors = [AdsApp.campaigns(), AdsApp.videoCampaigns(),
      AdsApp.shoppingCampaigns()];
  for (const selector of selectors) {
    const campaignIter = selector.
        withCondition(`CampaignName = "${campaignName}"`).
        get();
    if (campaignIter.hasNext()) {
      return campaignIter.next();
    }
  }
  return null;
}

/**
 * DO NOT EDIT ANYTHING BELOW THIS LINE.
 * Please modify your spreadsheet URL and API key at the top of the file only.
 */

/**
 * Validates the provided spreadsheet URL to make sure that it's set up
 * properly. Throws a descriptive error message if validation fails.
 *
 * @param {string} spreadsheeturl The URL of the spreadsheet to open.
 * @return {Spreadsheet} The spreadsheet object itself, fetched from the URL.
 * @throws {Error} If the spreadsheet URL hasn't been set
 */
function validateAndGetSpreadsheet(spreadsheeturl) {
  if (spreadsheeturl == 'INSERT_SPREADSHEET_URL_HERE') {
    throw new Error('Please specify a valid Spreadsheet URL. You can find' +
        ' a link to a template in the associated guide for this script.');
  }
  const spreadsheet = SpreadsheetApp.openByUrl(spreadsheeturl);
  return spreadsheet;
}

/**
 * Validates the provided API key to make sure that it's not the default. Throws
 * a descriptive error message if validation fails.
 *
 * @throws {Error} If the configured API key hasn't been set.
 */
function validateApiKey() {
  if (OPEN_WEATHER_MAP_API_KEY == 'INSERT_OPEN_WEATHER_MAP_API_KEY_HERE') {
    throw new Error('Please specify a valid API key for OpenWeatherMap. You ' +
        'can acquire one here: http://openweathermap.org/appid');
  }
}