التسوّق

استرداد جميع حملات التسوّق

function getAllShoppingCampaigns() {
  // AdsApp.shoppingCampaigns() will return all campaigns that are not removed
  // by default.
  const campaignIterator = AdsApp.shoppingCampaigns().get();
  console.log(`Total shopping campaigns found : ${
      campaignIterator.totalNumEntities()}`);
  return campaignIterator;
}

الحصول على حملة تسوّق حسب اسمها

function getShoppingCampaignByName(shoppingCampaignName) {
  const campaignIterator = AdsApp.shoppingCampaigns()
      .withCondition(`campaign.name = "${shoppingCampaignName}"`)
      .get();

  if (campaignIterator.hasNext()) {
    const campaign = campaignIterator.next();
    console.log(`Campaign Name: ${campaign.getName()}`);
    console.log(`Enabled: ${campaign.isEnabled()}`);
    console.log(`Bidding strategy: ${campaign.getBiddingStrategyType()}`);
    console.log(`Ad rotation: ${campaign.getAdRotationType()}`);
    console.log(`Start date: ${formatDate(campaign.getStartDate())}`);
    console.log(`End date: ${formatDate(campaign.getEndDate())}`);
    return campaign;
  } else {
    throw new Error(
        `No shopping campaign named "${shoppingCampaignName}" found`);
  }
}

function formatDate(date) {
  function zeroPad(number) {
    return Utilities.formatString('%02d', number);
  }
  return (date == null) ?
      'None' :
      zeroPad(date.year) + zeroPad(date.month) + zeroPad(date.day);
}

الحصول على مجموعة إعلانية للتسوّق حسب اسمها

function getShoppingAdGroupByName(shoppingAdGroupName) {
  const adGroupIterator = AdsApp.shoppingAdGroups()
      .withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
      .get();

  if (!adGroupIterator.hasNext()) {
    throw new Error(`No ad group with name "${shoppingAdGroupName}" found`);
  }

  const shoppingAdGroup = adGroupIterator.next();
  if (adGroupIterator.totalNumEntities() > 1) {
    console.warn(`Multiple ad groups named "${shoppingAdGroupName}" found.
Using the one from campaign "${shoppingAdGroup.getCampaign().getName()}"`);
  }

  return shoppingAdGroup;
}

إنشاء مجموعة Shopping إعلانية

function createElectronicsShoppingAdGroup() {
  // This example snippet assumes a user has a shopping campaign named
  // 'Shopping' and creates a new ad group named 'Electronics' in the campaign.
  // Please customize the snippet to suit your use case.
  const shoppingCampaignName = 'Shopping';
  const newAdGroupName = 'Electronics';

  const shoppingCampaign = AdsApp.shoppingCampaigns()
      .withCondition(`campaign.name = "${shoppingCampaignName}"`)
      .get()
      .next();

  const adGroupOperation = shoppingCampaign.newAdGroupBuilder()
      .withName(newAdGroupName)
      .withCpc(0.75)
      .build();

  if (adGroupOperation.isSuccessful()) {
    const adGroup = adGroupOperation.getResult();
    console.log(`Successfully created ad group "${
        adGroup.getName()}" in campaign "${adGroup.getCampaign().getName()}"`);
  } else {
    const errors = adGroupOperation.getErrors();
    console.error(`Creation failed with errors: ${errors}`);
    throw new Error(`Failed to create ad group "${
        newAdGroupName}" in campaign "${shoppingCampaignName}"`);
  }
}

إنشاء عرض هرمي لمجموعة منتجات Shopping

function createElectronicsProductGroups() {
  // This example snippet assumes a user has a shopping campaign named
  // 'Shopping' that includes an ad group named 'Electronics'. Please customize
  // the product group hierarchy to suit your use case.
  const shoppingCampaignName = 'Shopping';
  const shoppingAdGroupName = 'Electronics';

  const shoppingAdGroup = AdsApp.shoppingAdGroups()
      .withCondition(`campaign.name = "${shoppingCampaignName}"`)
      .withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
      .get()
      .next();

  const rootProductGroup = shoppingAdGroup.rootProductGroup();

  // The created product group hierarchy will be
  //  - root
  //    - 'Cardcow' brand
  //      - New condition
  //      - Refurbished condition
  //      - Other conditions
  //    - Other brands

  // Add a brand product group for 'Cardcow' under the root product group.
  const brandNode = rootProductGroup.newChild()
      .brandBuilder()
      .withName('Cardcow')
      .withBid(1.2)
      .build()
      .getResult();

  // Add groups for new and refurbished Cardcow brand items.
  const newItems = brandNode.newChild()
      .conditionBuilder()
      .withCondition('NEW')
      .build()
      .getResult();

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

اتخاذ إجراء بشأن كل مجموعة منتجات في التسلسل الهرمي

function actOnAllElectronicsProductGroups() {
  // This example snippet assumes a user has a hierarchy of product groups under
  // an ad group named 'Electronics' in a shopping campaign named 'Shopping'. It
  // applies the function 'actOnProductGroupAndChildren' to each product group
  // in the hierarchy. Please customize the 'actOnProductGroupAndChildren'
  // function to suit your specific use case.
  const shoppingCampaignName = 'Shopping';
  const shoppingAdGroupName = 'Electronics';

  const shoppingAdGroup = AdsApp.shoppingAdGroups()
      .withCondition(`campaign.name = "${shoppingCampaignName}"`)
      .withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
      .get()
      .next();

  const rootProductGroup = shoppingAdGroup.rootProductGroup();
  actOnProductGroupAndChildren(rootProductGroup, 0);
}

function actOnProductGroupAndChildren(productGroup, level) {
  // This example function logs descriptive information about the given
  // productGroup and all children of the given productGroup. Please customize
  // the function to suit your particular use case.
  let description = '';
  if (productGroup.isOtherCase()) {
    description = 'Other';
  } else if (productGroup.getDimension() == 'CATEGORY') {
    description = productGroup.asCategory().getName();
  } else {
    description = productGroup.getValue();
  }

  // Note: Child product groups may not have a max cpc if it has been excluded.
  const padding = new Array(level + 1).join('-');
  console.log(
      '%s %s, %s, %s, %s, %s', padding, description,
      productGroup.getDimension(), productGroup.getMaxCpc(),
      productGroup.isOtherCase(), productGroup.getId().toFixed());

  for (const childProductGroup of productGroup.children()) {
    actOnProductGroupAndChildren(childProductGroup, level + 1);
  }
}

الحصول على "كل شيء آخر" مجموعة منتجات

function getEverythingElseProductGroupForAdGroup(shoppingAdGroupName) {
  const adGroupIterator = AdsApp.shoppingAdGroups()
      .withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
      .get();

  if (!adGroupIterator.hasNext()) {
    throw new Error(`No ad group with name "${shoppingAdGroupName}" found`);
  }

  const shoppingAdGroup = adGroupIterator.next();
  if (adGroupIterator.totalNumEntities() > 1) {
    console.warn(`Multiple ad groups named "${shoppingAdGroupName}" found.
Using the one from campaign "${shoppingAdGroup.getCampaign().getName()}"`);
  }

  const rootProductGroup = shoppingAdGroup.rootProductGroup();
  for (const childProductGroup of rootProductGroup.children()) {
    if (childProductGroup.isOtherCase()) {
      // Note: Child product groups may not have a max cpc if it has been
      // excluded.
      console.log(
          `"Everything else" product group found. Type of the product group is ${
              childProductGroup.getDimension()} and bid is ${
              childProductGroup.getMaxCpc()}`);
      return childProductGroup;
    }
  }
  console.warn(
      '"Everything else" product group not found under root product group.');
  return null;
}

تحديث عروض الأسعار لمجموعات المنتجات

function updateVariousProductGroupBids() {
  // This example snippet modifies the bids of some product groups based on
  // criteria. Please modify the snippet to suit your use case.
  const productGroups = AdsApp.productGroups()
      .withCondition('Clicks > 5')
      .withCondition('Ctr > 0.01')
      .forDateRange('LAST_MONTH')
      .get();

  for (const productGroup of productGroups) {
    productGroup.setMaxCpc(productGroup.getMaxCpc() + 0.01);
  }
}

الحصول على إعلانات المنتجات

function getProductAdsInShoppingAdGroup(shoppingAdGroupName) {
  const adGroupIterator = AdsApp.shoppingAdGroups()
      .withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
      .get();

  if (!adGroupIterator.hasNext()) {
    throw new Error(`No ad group with name "${shoppingAdGroupName}" found`);
  }

  const shoppingAdGroup = adGroupIterator.next();
  if (adGroupIterator.totalNumEntities() > 1) {
    console.warn(`Multiple ad groups named "${shoppingAdGroupName}" found.
Using the one from campaign "${shoppingAdGroup.getCampaign().getName()}"`);
  }

  const productAdIterator = shoppingAdGroup.ads().get();
  console.log(`Ad Group "${shoppingAdGroup.getName()}" has ${
      productAdIterator.totalNumEntities()} ads`);
  return productAdIterator;
}

إنشاء إعلانات المنتج

function createElectronicsProductAd() {
  // This example snippet assumes a user has a shopping campaign named
  // 'Shopping' that includes an ad group named 'Electronics'. Please customize
  // the snippet to suit your use case.
  const shoppingCampaignName = 'Shopping';
  const shoppingAdGroupName = 'Electronics';

  const shoppingAdGroup = AdsApp.shoppingAdGroups()
      .withCondition(`campaign.name = "${shoppingCampaignName}"`)
      .withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
      .get()
      .next();

  const adOperation =
      shoppingAdGroup.newAdBuilder().withMobilePreferred(true).build();

  if (adOperation.isSuccessful()) {
    const productAd = adOperation.getResult();
    console.log(`Successfully created product ad in ad group "${
        productAd.getAdGroup().getName()}"`);
  } else {
    const errors = adOperation.getErrors();
    console.error(`Creation failed with errors: ${errors}`);
    throw new Error(
        `Failed to create product ad in ad group "${shoppingAdGroupName}"`);
  }
}