アドカスタマイザーの日付関数の代替

スクリプトの概要

レスポンシブ検索広告のアドカスタマイザーでは日付関数が利用できないため、Googleスプレッドシートに記載されたアドカスタマイザー日付関数をYahoo!広告スクリプトで取り込むことで自動的に日付を変更することが可能です。
本ページでは、Googleスプレッドシート上で=today()関数を使い、アドカスタマイザーの値を本日の日付(実行日が3月27日であれば「3月27日」)に更新するスクリプトを紹介します。

ご注意

アドカスタマイザーは編集ができないため、毎回削除→登録処理を実施しています。

毎回差し替えのタイミングで値の審査が入ることをご了承ください。審査中は、デフォルトの値が表示されます。

必要な設定

スプレッドシートのA1に日付関数を以下のようにご設定ください。


・表示形式を設定されている場合は、表示形式の内容が優先されてスクリプトで取得されます。
・テキスト関数などで、文字列と連結し「3月27日新着」のような形にすることも可能です。

※Googleスプレッドシートと連携するためには、編集権限があるGoogleアカウントとの連携が必要です。
 連携方法については以下のページをご参照ください。
 https://ads-developers.yahoo.co.jp/ja/ads-script/post/30390362.html

サンプルコード

サンプルコード内定数について
サンプルコード内の下記定数の説明をします。内容にあわせて変更を行ってください。
・const SPREAD_SHEET_ID = 'スプレッドシートID';
・const SHEET_NAME = 'シート1';
「必要な設定」で準備した、A1に日付関数等が設定されているスプレッドシートIDとシート名を指定します。
スプレッドシート ID については、こちらの FAQをご覧ください。

・const ENTITY_TYPE = 'ACCOUNT'; // ACCOUNT or CAMPAIGN
・const CAMPAIGN_IDS = []; // [12345,23456,34567]
ENTITY_TYPEで値のひも付け先を、アカウントにするかキャンペーンにするかを記載します。
キャンペーンの場合は、CAMPAIGN_IDSにキャンペーンIDを記載してください。
例)・const ENTITY_TYPE = 'CAMPAIGN'; // ACCOUNT or CAMPAIGN
  ・const CAMPAIGN_IDS = [12345]; // [12345,23456,34567]
  ※複数キャンペーンの場合、[12345,23456,34567]のように[]内にキャンペーンIDをカンマ区切りで記載してください。

・const CUSTOMIZER_NAME ='カスタマイザー名';
使用するアドカスタマイザーの属性の名称を指定してください。未登録の名称を指定した場合、自動で新規登録されます。

検索広告


/*
■スクリプト内容
指定のスプレッドシート内のA1の内容を、指定のアドカスタマイザーの値と差し替える。
値のひも付けは、アカウントかキャンペーンを指定できる。
■利用方法
1.当スクリプトを、検索広告のスクリプトとしてください。
2.定数を以下のように設定してください。
■定数
・SPREAD_SHEET_ID //スプレットシートIDを指定
・SHEET_NAME //スプレッドシートのシート名
・ENTITY_TYPE //アドカスタマイザーのひも付け先 アカウントにひも付ける場合はACCOUNT、キャンペーンにひも付けるならCAMPAIGN
・CAMPAIGN_IDS // ひも付け先のキャンペーンID。カンマ区切りで入力 
・CUSTOMIZER_NAME // アドカスタマイザーの属性名称 登録済みの場合は既存の属性を使い、未登録の場合は新規登録を行います。
*/
const SPREAD_SHEET_ID = 'スプレッドシートID';
const SHEET_NAME = 'シート1';
const ENTITY_TYPE = 'ACCOUNT'; // ACCOUNT or CAMPAIGN
const CAMPAIGN_IDS = []; // [12345,23456,34567]
const CUSTOMIZER_NAME ='カスタマイザー名';
const accountId = AdsUtilities.getCurrentAccountId();
function main() {
  try{
    switchCustomizer();
  } catch(e){
    throw new Error(e); 
  } 
}
function switchCustomizer(){
  const ss = SpreadsheetApp.openById(SPREAD_SHEET_ID);
  let sh = ss.getSheetByName(SHEET_NAME);
  sh.appendRow(['']);
  const dateData = sh.getDataRange().getValues();
  const customizerValue = dateData[0][0];
  const customizerAttributeId = getCustomizerId();
  Logger.log('対象の値' + customizerValue);
  if( 'ACCOUNT' === ENTITY_TYPE ){
    Logger.log('アカウントへの関連付けを行います');
    switchAccountCustomizer(customizerAttributeId, customizerValue);
  } else if( 'CAMPAIGN' === ENTITY_TYPE ){
    Logger.log('キャンペーンへの関連付けをおこないます');
    switchCampaignCustomizer(customizerAttributeId, customizerValue);
  } else {
    throw new Error('ENTITY_TYPEの設定に誤りがあります'); 
  }
}
function getCustomizerId(){
  let customizerData = Search.CustomizerAttributeService.get({
    accountId: accountId,
  }).rval;
  if( customizerData.totalNumEntries > 0 ){
    for (let i = 0; i < customizerData.values.length; i++){
      const customizerAttribute =  customizerData.values[i].customizerAttribute;
      if( customizerAttribute.name === CUSTOMIZER_NAME ){
        Logger.log('対象のアドカスタマイザーを取得しました。アドカスタマイザーID:' + customizerAttribute.customizerAttributeId);
        return customizerAttribute.customizerAttributeId;
      }
    }
  }
  customizerData = Search.CustomizerAttributeService.add({
    accountId: accountId,
    operand: [{
      name: CUSTOMIZER_NAME,
      type: "TEXT"
    }]
  }).rval;
  if( customizerData.values[0].operationSucceeded ){
    Logger.log('対象のアドカスタマイザーを作成しました。アドカスタマイザーID:' + customizerData.values[0].customizerAttribute.customizerAttributeId);
    return customizerData.values[0].customizerAttribute.customizerAttributeId;
  } else {
    throw new Error('アドカスタマイザーの作成に失敗しました。'); 
  }
}
function switchAccountCustomizer(customizerAttributeId, customizerValue){
  const customizerOperand = [{
    customizerAttributeId: customizerAttributeId,
    value: customizerValue
  }];
  const removeData = Search.AccountCustomizerService.remove({
    accountId: accountId,
    operand: customizerOperand
  }).rval;
  if( !removeData.values[0].operationSucceeded ){
    Logger.log('アドカスタマイザーのアカウント関連付け削除に失敗しました。※初回は必ず失敗します'); 
  }
  Logger.log(JSON.stringify(removeData));
  const addData = Search.AccountCustomizerService.add({
    accountId: accountId,
    operand: customizerOperand
  }).rval;
  if( addData.values[0].operationSucceeded ){
    Logger.log('アカウントへの関連付けに成功しました。値を「' + customizerValue + '」に変更しました。');
  } else {
    throw new Error('アドカスタマイザーのアカウント関連付けに失敗しました。'); 
  }
}
function switchCampaignCustomizer(customizerAttributeId, customizerValue){
  const customizerOperand = [];
  for(let i = 0; i < CAMPAIGN_IDS.length; i++ ){
    customizerOperand.push({
      campaignId: CAMPAIGN_IDS[i],
      customizerAttributeId: customizerAttributeId,
      value: customizerValue,
    });
  }
  const removeData = Search.CampaignCustomizerService.remove({
    accountId: accountId,
    operand: customizerOperand
  }).rval;
  let errFlg = false;
  for(let i = 0; i < removeData.values.length; i++ ){
    if( !removeData.values[i].operationSucceeded ){
      Logger.log('アドカスタマイザーのキャンペーン関連付け削除に失敗しました。※初回は必ず失敗します');
    }
  }
  const addData = Search.CampaignCustomizerService.add({
    accountId: accountId,
    operand: customizerOperand
  }).rval;
  for(let i = 0; i < addData.values.length; i++ ){
    if( addData.values[i].operationSucceeded ){
      Logger.log('キャンペーンID' + addData.values[i].campaignCustomizer.campaignId 
        +'への関連付けに成功しました。値を「' + customizerValue + '」に変更しました。');
    } else {
      errFlg = true;
      Logger.log('アドカスタマイザーのキャンペーン関連付けに失敗しました。キャンペーンID:' + addData.values[i].campaignCustomizer.campaignId);
    }
  }
  if( errFlg ){
    throw new Error('アドカスタマイザーのキャンペーン関連付けに失敗しました。'); 
  }
}