Price Extension Script Help

446 views
Skip to first unread message

Sofia

unread,
Aug 20, 2019, 7:08:08 AM8/20/19
to Google Ads Scripts Forum
Hi everyone, 

I have seen that we are now able to create scripts to easily update price extensions and I have made the attempt to create one for the very first time and I come across an error that I'm not sure how to resolve. 
Would much appreciate your help. Please bear in mind, I have zero scripts knowledge :D

The script is below: 

 function main() {
 
 // Fetch the data in the sheet  
 var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
 var sheet = spreadsheet.getSheetByName("Davidsons");

 // Load named arrays
 var currencyCode = spreadsheet.getRange("B4").getValue();
 var language = spreadsheet.getRange("B5").getValue();
 var howMany = spreadsheet.getRange("B6").getValue();
 var priceType = spreadsheet.getRange("B7").getValue();
 var priceQualifier = spreadsheet.getRange("B8").getValue();

 // Load all needed variables
 var ext1Header1 = sheet.getRange("D2").getValue();
 var ext1Desc1 = sheet.getRange("D3").getValue();
 var ext1Amount1 = sheet.getRange("D4").getValue();
 var ext1FinalUrl1 = sheet.getRange("D5").getValue();
 var ext1Header2 = sheet.getRange("D6").getValue();
 var ext1Desc2 = sheet.getRange("D7").getValue();
 var ext1Amount2 = sheet.getRange("D8").getValue();
 var ext1FinalUrl2 = sheet.getRange("D9").getValue();
 var ext1Header3 = sheet.getRange("D10").getValue();
 var ext1Desc3 = sheet.getRange("D11").getValue();
 var ext1Amount3 = sheet.getRange("D12").getValue();
 var ext1FinalUrl3 = sheet.getRange("D13").getValue();

 var ext2Header1 = sheet.getRange("G2").getValue();
 var ext2Desc1 = sheet.getRange("G3").getValue();
 var ext2Amount1 = sheet.getRange("G4").getValue();
 var ext2FinalUrl1 = sheet.getRange("G5").getValue();
 var ext2Header2 = sheet.getRange("G6").getValue();
 var ext2Desc2 = sheet.getRange("G7").getValue();
 var ext2Amount2 = sheet.getRange("G8").getValue();
 var ext2FinalUrl2 = sheet.getRange("G9").getValue();
 var ext2Header3 = sheet.getRange("G10").getValue();
 var ext2Desc3 = sheet.getRange("G11").getValue();
 var ext2Amount3 = sheet.getRange("G12").getValue();
 var ext2FinalUrl3 = sheet.getRange("G13").getValue();

 Logger.log(ext2Header1);
 Logger.log(ext2FinalUrl3);

  createPrice1 ();
  createPrice2 ();
  createPrice3 ();
  
 // Build price extension 1
 function createPrice1() {
  var priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem1 = priceItemBuilder
      .withHeader(ext1Header1)
      .withDescription(ext1Desc1)
      .withAmount(ext1Amount1)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext1FinalUrl1)
      .withUnitType('NONE')
      .build()

        if (priceItemOperation.isSuccessful()) {
         // Get the result.
         var priceItem = priceItemOperation.getResult();
       } else {
         // Handle the errors.
         var errors = priceItemOperation.getErrors();
       }

  priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem2 = priceItemBuilder
      .withHeader(ext1Header2)
      .withDescription(ext1Desc2)
      .withAmount(ext1Amount2)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext1FinalUrl2)
      .withUnitType('NONE')
      .build()
      .getResult();

  priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem3 = priceItemBuilder
      .withHeader(ext1Header3)
      .withDescription(ext1Desc3)
      .withAmount(ext1Amount3)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext1FinalUrl3)
      .withUnitType('NONE')
      .build()
      .getResult();

  var priceBuilder = AdsApp.extensions().newPriceBuilder();
  var price1 = priceBuilder
      .withPriceType(priceType)
      .withPriceQualifier(priceQualifier)
      .withLanguage(language)
      .addPriceItem(priceItem1)
      .addPriceItem(priceItem2)
      .addPriceItem(priceItem3)
      .build()
      .getResult();

// add to account
 AdsApp.currentAccount().addPrice(price1);

 }

 // Build price extension 2 
 function createPrice2() {
  var priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem1 = priceItemBuilder
      .withHeader(ext2Header1)
      .withDescription(ext2Desc1)
      .withAmount(ext2Amount1)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext2FinalUrl1)
      .withUnitType('NONE')
      .build()
      .getResult();

  priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem2 = priceItemBuilder
      .withHeader(ext2Header2)
      .withDescription(ext2Desc2)
      .withAmount(ext2Amount2)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext2FinalUrl2)
      .withUnitType('NONE')
      .build()
      .getResult();

  priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem3 = priceItemBuilder
      .withHeader(ext2Header3)
      .withDescription(ext2Desc3)
      .withAmount(ext2Amount3)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext2FinalUrl3)
      .withUnitType('NONE')
      .build()
      .getResult();

  var priceBuilder = AdsApp.extensions().newPriceBuilder();
  var price2 = priceBuilder
      .withPriceType(priceType)
      .withPriceQualifier(priceQualifier)
      .withLanguage(language)
      .addPriceItem(priceItem1)
      .addPriceItem(priceItem2)
      .addPriceItem(priceItem3)
      .build()
      .getResult();

 AdsApp.currentAccount().addPrice(price2);

 }

 // Build price extension 3 
 function createPrice3() {
  var priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem1 = priceItemBuilder
      .withHeader(ext3Header1)
      .withDescription(ext3Desc1)
      .withAmount(ext3Amount1)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext3FinalUrl1)
      .withUnitType('NONE')
      .build()
      .getResult();

  priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem2 = priceItemBuilder
      .withHeader(ext3Header2)
      .withDescription(ext3Desc2)
      .withAmount(ext3Amount2)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext3FinalUrl2)
      .withUnitType('NONE')
      .build()
      .getResult();

  priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem3 = priceItemBuilder
      .withHeader(ext3Header3)
      .withDescription(ext3Desc3)
      .withAmount(ext3Amount3)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext3FinalUrl3)
      .withUnitType('NONE')
      .build()
      .getResult();

  var priceBuilder = AdsApp.extensions().newPriceBuilder();
  var price3 = priceBuilder
      .withPriceType(priceType)
      .withPriceQualifier(priceQualifier)
      .withLanguage(language)
      .addPriceItem(priceItem1)
      .addPriceItem(priceItem2)
      .addPriceItem(priceItem3)
      .build()
      .getResult();

 AdsApp.currentAccount().addPrice(price3); 
  }
}


And that's the error I receive: 

8/20/2019 12:05:43 PMReferenceError: "priceItemOperation" is not defined. (file Code.gs, line 61)

Thank you in advance!

Google Ads Scripts Forum Advisor Prod

unread,
Aug 20, 2019, 11:50:59 AM8/20/19
to adwords-scripts+apn2wqc5dvq2cjek...@googlegroups.com, adwords-scripts+apn2wqc5dvq2cjek...@googlegroups.co, adwords...@googlegroups.com
Hi Sofia,

You'll need to change the lines that contain priceItemOperation to priceItem1 since you never define the variable priceItemOperation in your script. You will also need to modify your spreadsheet so that the values passed are valid. For example, you will need to use "en" rather than "English" and "PRODUCT_TIERS" instead of "Product tiers".

Please take a look at the PriceBuilder to see the format of the values that should be passed.

Regards,
Anthony
Google Ads Scripts Team

ref:_00D1U1174p._5001UHE4Ll:ref

Josh Blumberg

unread,
Aug 20, 2019, 12:38:41 PM8/20/19
to Google Ads Scripts Forum
Hi Both,

I wrote the script that Sofia is using above, and was having a similar issue, but you solved it! So thanks for your help. 

The problem was I had "English", not "EN". It would be my recommendation that Google updates the documentation to specify what options are available for the .withLanguage parameter, like it does for the .withPriceQualifier one, where it says "this field is required and must be one of ['BRANDS', 'EVENTS', etc..

The documentation page I'm referring to is this one:


Are you able to update that for future scripters?

Thanks,
Josh

Google Ads Scripts Forum Advisor Prod

unread,
Aug 20, 2019, 4:22:20 PM8/20/19
to adwords-scripts+apn2wqeylfj3bar8...@googlegroups.com, adwords...@googlegroups.com
Hey Josh,

Thanks for the feedback. I'll pass this idea off to the team and see if we can get the documentations updated.

Cheers,

Sofia

unread,
Aug 22, 2019, 11:43:12 AM8/22/19
to Google Ads Scripts Forum
Hello both, 

Thank you very much! 
The script worked :) 

Can I ask you though. Is there a script to automatically update the current price extensions within campaigns instead of creating new price extensions? 

Many thanks, 

Sofia

Google Ads Scripts Forum Advisor Prod

unread,
Aug 22, 2019, 12:47:28 PM8/22/19
to adwords-scripts+apn2wqc5dvq2cjek...@googlegroups.com, adwords-scripts+apn2wqc5dvq2cjek...@googlegroups.co, adwords...@googlegroups.com
Hi Sofia,

Great to hear the script worked for you! :)

You can use a PriceSelector to get your current price extensions then use any of the set functions there to modify them.

Josh Blumberg

unread,
Aug 22, 2019, 12:59:38 PM8/22/19
to Google Ads Scripts Forum
From what I understood, you can only use the functions below to edit existing price extensions:

setEndDate
setLanguage
setMobilePreferred
setPriceQualifier
setPriceType
setSchedules
setStartDate
setTrackingTemplate

Because normally we would want to change the text, or the price, as opposed to the options above, that's why I wrote the script to just remove the existing associations and make new extensions and associate them. 

@Anthony, is it possible to actually remove price extensions via scripts? or just remove the association of them to whatever level (account, campaign, or ad group)?

Thanks,
Josh

Google Ads Scripts Forum Advisor Prod

unread,
Aug 22, 2019, 4:00:43 PM8/22/19
to adwords-scripts+apn2wqeylfj3bar8...@googlegroups.com, adwords...@googlegroups.com

Hey Josh,
 

Yes, you can. For example, to remove them from the campaign level, you will need to select your campaign, then use the function removePrice,

Regards,


Anthony
Google Ads Scripts Team



ref:_00D1U1174p._5001UHE4Ll:ref

Josh Blumberg

unread,
Aug 23, 2019, 3:28:03 AM8/23/19
to Google Ads Scripts Forum
Hi Anthony, 

Thanks. I've got something similar in my script (see below), that just removes the association, but the extension still exists in the account (just not associated at any level). 

Is there any way to completely remove the extension?

  // Check if there are existing price extensions, and remove them if so. If not, carry on to builder. 
  var priceExtCount = AdsApp.currentAccount().extensions().prices().get().totalNumEntities();
  Logger.log("There are currently " + priceExtCount + " price extensions associated at account level.");

  if (priceExtCount > 0) {
    Logger.log("Removing the existing account level price extensions");
    var accountExtIterator = AdsApp.currentAccount().extensions().prices().get()
      while (accountExtIterator.hasNext()) {
      var accountExt = accountExtIterator.next();
      AdsApp.currentAccount().removePrice(accountExt);
      } // end while
    } // end if

Thanks,
Josh

Google Ads Scripts Forum Advisor Prod

unread,
Aug 23, 2019, 2:27:51 PM8/23/19
to adwords-scripts+apn2wqeylfj3bar8...@googlegroups.com, adwords...@googlegroups.com
Hi Josh,

There is currently no way to remove the price extensions completely from the account via Google Ads Scripts.

Sofia

unread,
Aug 27, 2019, 11:35:38 AM8/27/19
to Google Ads Scripts Forum

Hi both, 

I am very sorry for my ignorance, but could you tell me whats the exact condition I need to use in order to ignore empty cells? 

var ext1Header3 = sheet.getRange("G10").getValue();
 var ext1Desc3 = sheet.getRange("G11").getValue();
 var ext1Amount3 = sheet.getRange("G12").getValue();
 var ext1FinalUrl3 = sheet.getRange("G13").getValue();

There might be a chance that these cells are empty, and I need the script to simply ignore these. 

Not sure if the condition should be added after this bit though:
  priceItemBuilder = AdsApp.extensions().newPriceItemBuilder();
  var priceItem3 = priceItemBuilder
      .withHeader(ext1Header3)
      .withDescription(ext1Desc3)
      .withAmount(ext1Amount3)
      .withCurrencyCode(currencyCode)
      .withFinalUrl(ext1FinalUrl3)
      .withUnitType('NONE')
      .build()
      .getResult();


I thought a simple code like this would do the magic
if (ext1Header3 == null) {
ext1Header3 = " ";
}


Many thanks, 

Sofia


Google Ads Scripts Forum Advisor Prod

unread,
Aug 27, 2019, 3:57:50 PM8/27/19
to adwords-scripts+apn2wqc5dvq2cjek...@googlegroups.com, adwords-scripts+apn2wqc5dvq2cjek...@googlegroups.co, adwords...@googlegroups.com
Hi Sofia,

The header for a price item is required when you are creating them.
Reply all
Reply to author
Forward
0 new messages