Shopping

Retrieve all shopping campaigns

function getAllShoppingCampaigns() {
  var retval = [];
  var campaignIterator = AdWordsApp.shoppingCampaigns().get();
  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();

    // Optional: Comment out if you don't need to print details.
    Logger.log('Campaign Name: %s', campaign.getName());

    retval.push(campaign);
  }
  return retval;
}

Retrieve a shopping campaign by its name

function getShoppingCampaignByName(campaignName) {
  var campaignIterator = AdWordsApp.shoppingCampaigns()
      .withCondition("CampaignName = '" + campaignName + "'")
      .get();
  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    Logger.log('Campaign Name: %s', campaign.getName());
  }
}

Retrieve a shopping adgroup by its name

function getShoppingAdGroup() {
  var campaignName = 'INSERT_CAMPAIGN_NAME_HERE';
  var adGroupName = 'INSERT_ADGROUP_NAME_HERE';

  var adGroupIterator = AdWordsApp.shoppingAdGroups()
      .withCondition("CampaignName = '" + campaignName +
          "' and AdGroupName = '" + adGroupName + "'")
      .get();
  while (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    Logger.log(
        'AdGroup Name: %s, CPC = %s, Mobile Bid ' + 'Modifier = %s',
        adGroup.getName(),
        adGroup.bidding().getCpc(),
        adGroup.devices().getMobileBidModifier()
    );
  }
}

Create a shopping ad group

function createShoppingAdGroup() {
  var campaignName = 'INSERT_CAMPAIGN_NAME_HERE';

  var shoppingCampaign = AdWordsApp.shoppingCampaigns()
      .withCondition("CampaignName = '" + campaignName + "'")
      .get()
      .next();
  var adGroupOperation = shoppingCampaign.newAdGroupBuilder().build();
  var adGroup = adGroupOperation.getResult();
  Logger.log(adGroup);
}

Create a shopping product group hierarchy

function createTree() {
  var campaignName = 'INSERT_CAMPAIGN_NAME_HERE';
  var adGroupName = 'INSERT_ADGROUP_NAME_HERE';

  var shoppingAdGroup = AdWordsApp.shoppingAdGroups()
      .withCondition("CampaignName = '" + campaignName +
          "' and AdGroupName = '" + adGroupName + "'")
      .get()
      .next();

  var root = shoppingAdGroup.rootProductGroup();

  // The structure created is
  // - root
  //   - cardcow brand
  //     - New
  //     - Refurbished
  //     - Other conditions
  //   - Other brands

  // Add a brand product group for "cardcow" under root product group.
  var brandNode = root.newChild()
      .brandBuilder()
      .withName('cardcow')
      .withBid(1.2)
      .build()
      .getResult();

  // Add new conditions for New and Refurbished cardcow brand items.
  var newItems = brandNode.newChild()
      .conditionBuilder()
      .withCondition('NEW')
      .build()
      .getResult();

  var refurbishedItems = brandNode.newChild()
      .conditionBuilder()
      .withCondition('REFURBISHED')
      .withBid(0.9)
      .build()
      .getResult();
}

Traverses the product group hierarchy

function walkProductPartitionTree() {
  var campaignName = 'INSERT_CAMPAIGN_NAME_HERE';
  var adGroupName = 'INSERT_ADGROUP_NAME_HERE';

  var shoppingAdGroup = AdWordsApp.shoppingAdGroups()
      .withCondition("CampaignName = '" + campaignName +
          "' and AdGroupName = '" + adGroupName + "'")
      .get()
      .next();
  var root = shoppingAdGroup.rootProductGroup();
  walkHierarchy(root, 0);
}

function walkHierarchy(productGroup, level) {
  var description = '';

  if (productGroup.isOtherCase()) {
    description = 'Other';
  } else if (productGroup.getDimension() == 'CATEGORY') {
    // Shows how to process a product group differently based on its type.
    description = productGroup.asCategory().getName();
  } else {
    description = productGroup.getValue();
  }

  var padding = new Array(level + 1).join('-');

  // Note: Child product groups may not have a max cpc if it has been excluded.
  Logger.log(
      '%s %s, %s, %s, %s, %s',
      padding,
      description,
      productGroup.getDimension(),
      productGroup.getMaxCpc(),
      productGroup.isOtherCase(),
      productGroup.getId().toFixed()
  );
  var childProductGroups = productGroup.children().get();
  while (childProductGroups.hasNext()) {
    var childProductGroup = childProductGroups.next();
    walkHierarchy(childProductGroup, level + 1);
  }
}

Gets the 'Everything else' product group

function getEverythingElseProductGroup() {
  var campaignName = 'INSERT_CAMPAIGN_NAME_HERE';
  var adGroupName = 'INSERT_ADGROUP_NAME_HERE';

  var shoppingAdGroup = AdWordsApp.shoppingAdGroups()
      .withCondition("CampaignName = '" + campaignName +
          "' and AdGroupName = '" + adGroupName + "'")
      .get()
      .next();

  var rootProductGroup = shoppingAdGroup.rootProductGroup();
  var childProductGroups = rootProductGroup.children().get();
  while (childProductGroups.hasNext()) {
    var childProductGroup = childProductGroups.next();
    if (childProductGroup.isOtherCase()) {
      // Note: Child product groups may not have a max cpc if it has been
      // excluded.
      Logger.log(
          '"Everything else" product group found. Type of the product ' +
              'group is %s and bid is %s.',
          childProductGroup.getDimension(),
          childProductGroup.getMaxCpc());
      return;
    }
  }
  Logger.log('"Everything else" product group not found under root ' +
      'product group.');
}

Update bids for product groups

function updateProductGroupBid() {
  var productGroups = AdWordsApp.productGroups()
      .withCondition('Clicks > 5')
      .withCondition('Ctr > 0.01')
      .forDateRange('LAST_MONTH')
      .get();
  while (productGroups.hasNext()) {
    var productGroup = productGroups.next();
    productGroup.setMaxCpc(productGroup.getMaxCpc() + 0.01);
  }
}

Retrieve product ads

function getProductAds() {
  var adGroupName = 'INSERT_ADGROUP_NAME_HERE';

  var shoppingAdGroup = AdWordsApp.shoppingAdGroups()
      .withCondition("AdGroupName = '" + adGroupName + "'")
      .get()
      .next();

  var productAds = shoppingAdGroup.ads().get();
  while (productAds.hasNext()) {
    var productAd = productAds.next();
    Logger.log(
        "Ad with ID = %s and promotion line = '%s' was found.",
        productAd.getId().toFixed(0),
        productAd.getPromotionLine());
  }
}

Create product ads

function createProductAd() {
  var adGroupName = 'INSERT_ADGROUP_NAME_HERE';

  var shoppingAdGroup = AdWordsApp.shoppingAdGroups()
      .withCondition("AdGroupName = '" + adGroupName + "'")
      .get()
      .next();

  var adOperation = shoppingAdGroup.newAdBuilder()
      .withPromotionLine('Hurry! Limited stock ' + 'available!')
      .withMobilePreferred(true)
      .build();
  var productAd = adOperation.getResult();
  Logger.log(
      "Ad with ID = %s and promotion line = '%s' was created.",
      productAd.getId().toFixed(0),
      productAd.getPromotionLine());

}

Send feedback about...

AdWords Scripts
AdWords Scripts
Need help? Visit our support page.