Snippets

Frederick Vallaeys FInd Duplicate Shopping Targets

Created by Frederick Vallaeys
function main() {
  
  var productList = new Array();
  var duplicateProductList = new Array();
  
  var report = AdWordsApp.report(
    'SELECT Id, ProductGroup, AdGroupName ' +
    'FROM   PRODUCT_PARTITION_REPORT ' +
    'WHERE  Impressions > 0 ' +
    'DURING LAST_30_DAYS');
  var rows = report.rows();
  
  while(rows.hasNext()) {
    var row = rows.next();
    var productId = row['Id'];
    var productGroup = row['ProductGroup'];
    var adGroupName = row['AdGroupName'];
    
    
    if(productList[productId]) {
      //Logger.log("Product ID: " + productId + " is targeted by multiple product groups: " + productGroup + " ag: " + adGroupName);
      duplicateProductList.push(productId);
    } else {
      productList[productId] = new Object();
      productList[productId].productGroups = new Array();
      //Logger.log(productId + " targeted by " + productGroup);
    }
    
    var duplicate = new Object();
    duplicate.productGroup = productGroup;
    duplicate.adGroupName = adGroupName;
    productList[productId].productGroups.push(duplicate);
    
    
  }
  
  var duplicateProductList = duplicateProductList.getUnique();
  if(duplicateProductList.length) {
    Logger.log("Duplicate Targets:");
    for(var counter = 0; counter < duplicateProductList.length; counter++) {
      var productId = duplicateProductList[counter];
      Logger.log("Product ID: " + productId);
      var productGroups = productList[productId].productGroups;
      for(var i = 0 ; i < productGroups.length; i++) {
        var productGroup = productGroups[i].productGroup;
        var adGroupName = productGroups[i].adGroupName;
        Logger.log("  ad group: " + adGroupName + " product group: " + productGroup);
      }
    }
  }
}

Array.prototype.getUnique = function() {
 var o = {}, a = [], i, e;
 for (i = 0; e = this[i]; i++) {o[e] = 1};
 for (e in o) {a.push (e)};
 return a;
}
  
    

Comments (1)

  1. Abi_Cynthee

    Hi Frederick/Anyone else,

    Are you able to amend the script to only look at enabled campaigns?

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.