Brainlabs-Digital/Google-Ads-Scripts

satisfiesIdExcludeFilters function is not working as expected

Closed this issue · 2 comments

function satisfiesIdExcludeFilters(productIdToExclude, product) {

I think the satisfiesIdExcludeFilters() is not working as expected.

Let's say that I have six products. If I put in two or more IDs to exclude within the productIdToExclude array, the code should ultimately tell me that there are only four products total because it just excluded two of them.

Instead, your code seems to immediately return true; because it doesn't find the ID in the current product we are looking at. This happens because we have more than one excluded ID in the productIdToExclude array.

I made a slight change to that function which seems to fix the logic of that filter check:

function satisfiesIdExcludeFilters(productIdToExclude, product) {
  if (productIdToExclude.length) {
    Logger.log(`About to check this product: ${product['productId']}`);
    for (let index = 0; index < productIdToExclude.length; ++index) {
      if (product['productId'].indexOf(productIdToExclude[index]) !== -1) {
        Logger.log(`We found this productIdToExclude (${productIdToExclude[index]}) in this productId [${product['productId']}] - going to return false`);
        return false;
      } else {
        Logger.log(`We didn't find: [${productIdToExclude[index]}] within this: [${product['productId']}]`);
      }
    }
    Logger.log(`We don't have to exclude this productId: ${product['productId']} - going to return true`);
    return true;
  }
  else {
    Logger.log(`The array was empty - going to return true`);
    return true;
  }
}

I hope that helps.

Hi @nakitadog - thanks for spotting this issue and describing it so well! Your code snippet was really helpful for fixing the issue, so thank you for that as well!

The fix has now been added into the code:

function satisfiesIdExcludeFilters(productIdToExclude, product) {

Thanks again!

You are welcome and thank you for the code.