PLA Script: 自动创建SPG系列

// Copyright 2020. Increase BV. All Rights Reserved.
//
// Created By: Tibbe van Asten
// for Increase B.V.
//
// Created 15-04-2019
// Last update: 14-01-2020
//
// ABOUT THE SCRIPT
// All products in the productfeed are checked for existance in the
// Google Shopping campaigns. If new, they're added to the right campaign
//
////////////////////////////////////////////////////////////////////
​
var config = {
​
  LOG : true,
​
  // Connect Merchant Center. Add user to MC that runs this script.
  // Also enable Advanced API 'Shopping content'.
  MERCHANT_ID : "123456789",
​
  // Set a default bid. This will be used for adgroups and products
  DEFAULT_BID : 0.75,
​
  // Set the target Campaigns.
​
  TARGET_CAMPAIGN : "_SPG"
}
​
////////////////////////////////////////////////////////////////////
​
function main(){
​
  // First off, we collect all productdata from Merchant Center.
  // We also check all active adgroups
  var products = connectMerchant();
  var activeProducts = activeAdGroups();
​
  var addProducts = {};
​
  // All products of the feed will be checked
  for (var i = 0; i < products.length; i++) {
​
    // If the products-Id is already the name of an existing adgroup, we don't add it.
    // If the product is out of stock, we don't add it.
​
    if(activeProducts.indexOf(products[i]["customLabel0"]) < 0){
      var productId = products[i]["customLabel0"];
​
      if (addProducts[productId] == null) {
        addProducts[productId] = [];
        
      }
      addProducts[productId].push([productId]);
    }
​
  } // for statement
​
    if(config.LOG === true){
      Logger.log("Found " + Object.keys(addProducts).length + " to be added");
      Logger.log(" ");
    }
​
  // Adding all products not already in the campaigns
  for (var key in addProducts) {
​
    var campaignSelector = AdsApp
      .shoppingCampaigns()
      .withCondition("Status = ENABLED");
    
    if(config.TARGET_CAMPAIGN != ""){
      campaignSelector = campaignSelector.withCondition("Name CONTAINS '" + config.TARGET_CAMPAIGN + "'");
    }
    
    var campaignIterator = campaignSelector.get();
​
    while(campaignIterator.hasNext()){
      var campaign = campaignIterator.next();
​
      // Adding adgroup to Shopping campaign
      var shoppingAdGroup = campaign
        .newAdGroupBuilder()
        .withName(addProducts[key][0][0])
        .withCpc(config.DEFAULT_BID)
        .withStatus("ENABLED")
        .build()
        .getResult();
​
        if(config.LOG === true){
          Logger.log(shoppingAdGroup.getName() + " added to " + campaign.getName());
        }
​
      // Adding an ad to the new adgroup
      var ad = shoppingAdGroup
        .newAdBuilder()
        .build()
        .getResult();
​
      // Creating the root productgroup 'All Products' and select it
      shoppingAdGroup.createRootProductGroup();
      var root = shoppingAdGroup.rootProductGroup();
​
      // Creating a productgroup for this product
      root.newChild().customLabelBuilder()
        .withBid(config.DEFAULT_BID)
        .withType('CUSTOM_LABEL_0')
        .withValue(addProducts[key][0][0])
        .build();
​
      // Making sure all other products in this adgroup are excluded
      var children = root.children().get();
      while (children.hasNext()) {
        var child = children.next();
        if (child.isOtherCase()) {
          child.exclude();
        }
      } // childrenIterator
​
    } // campaignIterator
​
  } // addProductsIterator
​
} // function main()
​
////////////////////////////////////////////////////////////////////
​
function activeAdGroups(){
​
  var activeProducts = [];
​
  // We will check if an adGroup with this ID as name already exists
  var adGroupIterator = AdsApp
    .shoppingAdGroups()
    .withCondition("CampaignStatus = ENABLED")
    .withCondition("Status != REMOVED")
    .get();
​
  while(adGroupIterator.hasNext()){
    var adGroup = adGroupIterator.next();
    activeProducts.push(adGroup.getName().split(" - ")[0]);
  } // adGroupIterator
​
  return activeProducts;
​
} // activeAdGroups
​
////////////////////////////////////////////////////////////////////
​
function connectMerchant(){
​
  if(config.MERCHANT == "123456789"){
    throw Error("Change the Merchant ID in the settings");
  }
​
  var pageToken;
  var pageNum = 1;
  var maxResults = 250;
  var products = [];
​
  do {
    var productList = ShoppingContent.Products.list(config.MERCHANT_ID, {
      pageToken: pageToken,
      maxResults: maxResults
    });
​
    if (productList.resources) {
      for (var i = 0; i < productList.resources.length; i++) {
​
        // We only add products from Merchant Center when in stock
        if(productList.resources[i]["availability"] == "in stock"){
          products.push(productList.resources[i]);
        }
        
      }
    }
​
    pageToken = productList.nextPageToken;
    pageNum++;
  } while (pageToken);
​
  return products;
​
} // function connectMerchant

发表评论

电子邮件地址不会被公开。 必填项已用*标注