Script for Disapproved product not working

389 views
Skip to first unread message

Jacopo Mariani

unread,
Aug 4, 2022, 9:33:42 AM8/4/22
to Google Ads Scripts Forum
Hi,

I am having issue with the script for disapproved product. when i try to run the script this is the error that is giving me: ReferenceError: ShoppingContent is not defined at main (Code64:27).

The script is the following:
// ID: 1ffca02c22a55e164350b80c28aa2d77
/**
 *
 * GMC Disapproval Checker
 *
 * This script checks your Google Merchant Centre for disapproved products. It will send you emails if the percentage of
 * disapproved products exceeds a specified threshold. You need to select the Shopping Content api in the Advanced Apis
 * section to run this script.
 *
 * Google AdWords Script maintained on brainlabsdigital.com
 *
 */


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Options

var merchantId = 505834217;
// Replace this with your Merchant Center ID.

var threshold = 100; // probabilmente con 100% appena c'e' un prodotto disapprovato.
 // Percentage of disapproved products you would like to be alerted by.
// Do NOT include the percentage sign in this variable.

var email = [""]; // o un gruppo contenente un certo elenco di persone
// Email addresses to send the disapproval alert to.
// If there is more than one email they should be comma separated

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Filters
// These two variables store the product ID's that we want to filter by.
// The ID's need to be in string format e.g. productIdToInclude = ["123"];
// These are actually substrings of the productId so be careful not to use
// strings which are not reasonably specific.

var productIdToInclude = []; // se non metti ID lui controlla tutto
var productIdToExclude = [""];

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Functions

function main() {

  var pageToken;
  var maxResults = 250;
  var disapprovati;
  // These variables are used to fetch all the products (maximum of 250) on a page
  // in the merchant centre. We can then iterate over the pages by using the next
  // page token which is given as part of the response to our request.

  var totalProducts = 0;
  var numberDisapprovedProducts = 0;
 

  do {
    // This is a quick check to see if the filters supplied are valid
    checkFiltersDontContradict(productIdToInclude, productIdToExclude);

    var productStatuses = ShoppingContent.Productstatuses.list(merchantId, {
      pageToken: pageToken,
      maxResults: maxResults
    });

    // If the 'approvalStatus' of our product is not 'approved' then we say it is disapproved.
    if (productStatuses.resources) {
      for (var i = 0; i < productStatuses.resources.length; i++) {
        product = productStatuses.resources[i];
        if (satisfiesAllFilters(product)) {
          totalProducts += 1;
          if (product['destinationStatuses'][0]['approvalStatus'] == 'disapproved') {
            numberDisapprovedProducts++;
            disapprovati = disapprovati + ";" + product['productId'];
          }
        }
      }
    } else {
      Logger.log('No more products in account ' + merchantId);
    }
    // We then pull our next PageToken from our response and use it to make a new request.
    // If there is no next PageToken then we exit our iteration.
    pageToken = productStatuses.nextPageToken;
  } while (pageToken);

  disapprovalPercentage = (numberDisapprovedProducts * 100) / totalProducts;
  Logger.log(numberDisapprovedProducts + ' of ' + totalProducts
    + ' products in your account were disapproved - '
    + disapprovalPercentage.toFixed(2) + '%')

  // If our threshold is exceeded then we assemble an email with details of the alert and send it to our contact emails.
  if (disapprovalPercentage >= threshold) {

    var subject = merchantId + ' GMC Disapproval Alert Email';
    var message = numberDisapprovedProducts + ' of ' + totalProducts
      + ' products in your GMC (' + merchantId + ') were disapproved - '
      + ' prodotto disapprovato ' + disapprovati + ')'
      + disapprovalPercentage.toFixed(2) + '%' + '\n';

    MailApp.sendEmail(email.join(','), subject, message);
    Logger.log('Message to ' + email.join(',') + ' sent.');
  }
}

function checkFiltersDontContradict(productIdToInclude, productIdToExclude) {
  if (productIdToInclude.length && productIdToExclude.length) {
    for (var i in productIdToInclude) {
      if (productIdToExclude.indexOf(productIdToInclude[i]) > -1) {
        throw "Filters have shared values - can not include and exclude simultaneously";
      }
    }
  } else {
    return true;
  }
}

function satisfiesAllFilters(product) {
  return (satisfiesIdIncludeFilters(productIdToInclude, product)
    && satisfiesIdExcludeFilters(productIdToExclude, product));
}

function satisfiesIdIncludeFilters(productIdToInclude, product) {
  if (productIdToInclude.length) {
    for (index = 0; index < productIdToInclude.length; ++index) {
      if (product['productId'].indexOf(productIdToInclude[index]) !== -1) {
        return true;
      }
    }
    return false;
  }
  else {
    return true;
  }
}

function satisfiesIdExcludeFilters(productIdToExclude, product) {
  if (productIdToExclude.length) {
    for (index = 0; index < productIdToExclude.length; ++index) {
      if (product['productId'].indexOf(productIdToExclude[index]) == -1) {
        return true;
      }
    }
    return false;
  }
  else {
    return true;
  }
}


What should i do to make this script working?

Best,
Jacopo

Google Ads Scripts Forum Advisor

unread,
Aug 5, 2022, 2:19:40 AM8/5/22
to adwords...@googlegroups.com

Hi Jacopo,

 

This is Yasmin from the Google Ads Scripts team.

 

I can see that you've created a new thread with the same concern mentioned on another thread you've raised with the subject, "Script Disapproved product Not working". Please refrain from opening multiple threads with the same content for better tracking of the issue on our end. That being said, we would highly appreciate it if the conversation is continued within the aforementioned thread you had opened.

 

Thanks,

 

Google Logo
Yasmin Gabrielle
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2d4pNw:ref

Nils Rooijmans

unread,
Aug 8, 2022, 3:30:02 AM8/8/22
to Google Ads Scripts Forum

you probably forgot to enable the Content API for Shopping.

To use the API :

1. Open your script code in the script editor
2. Go to the top right corner of the script, there is a button ‘Advanced API’s’. Click on that button and enable the Shopping Content API.


Hope this helps,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Google Ads Scripts Forum Advisor

unread,
Aug 8, 2022, 4:31:00 AM8/8/22
to adwords...@googlegroups.com

Hi Nils,

This is Ciara from the Google Ads Scripts team as well.

Thank you for sharing additional information and your insights regarding this issue.

Regards,

Google Logo
Ciara
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2d4pNw:ref
Reply all
Reply to author
Forward
0 new messages