購物
透過集合功能整理內容 你可以依據偏好儲存及分類內容。
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; }
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}"`); } }
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}"`); } }
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\u003cp\u003eThe provided code snippets demonstrate how to manage Shopping campaigns and their components (ad groups, product groups, and ads) within Google Ads using scripts.\u003c/p\u003e\n"],["\u003cp\u003eYou can retrieve all Shopping campaigns, access specific campaigns and ad groups by name, and create new ad groups within existing campaigns.\u003c/p\u003e\n"],["\u003cp\u003eIt is possible to define a product group hierarchy within a Shopping ad group and perform actions on each individual product group or update their bids in bulk.\u003c/p\u003e\n"],["\u003cp\u003eThe snippets showcase ways to fetch existing Product Ads within a specified Shopping ad group, as well as create new Product Ads with specific settings, like mobile preference.\u003c/p\u003e\n"],["\u003cp\u003eThe code leverages the Google Ads Scripts API to interact with various entities and elements within the platform, enabling automated management of Shopping campaign structures.\u003c/p\u003e\n"]]],[],null,[]]