The code samples below provide examples for managing Shopping campaigns using the AdWords API. Client Library.
Build a product partition tree for an ad group
<?php /** * Copyright 2017 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\AdsApi\Examples\AdWords\v201809\ShoppingCampaigns; require __DIR__ . '/../../../../vendor/autoload.php'; use Google\AdsApi\AdWords\AdWordsServices; use Google\AdsApi\AdWords\AdWordsSession; use Google\AdsApi\AdWords\AdWordsSessionBuilder; use Google\AdsApi\AdWords\Shopping\v201809\ProductPartitions; use Google\AdsApi\AdWords\v201809\cm\AdGroupCriterionService; use Google\AdsApi\AdWords\v201809\cm\ProductBiddingCategory; use Google\AdsApi\AdWords\v201809\cm\ProductBrand; use Google\AdsApi\AdWords\v201809\cm\ProductCanonicalCondition; use Google\AdsApi\AdWords\v201809\cm\ProductCanonicalConditionCondition; use Google\AdsApi\AdWords\v201809\cm\ProductDimensionType; use Google\AdsApi\Common\OAuth2TokenBuilder; /** * This example creates a product partition tree. */ class AddProductPartitionTree { const AD_GROUP_ID = 'INSERT_AD_GROUP_ID_HERE'; public static function runExample( AdWordsServices $adWordsServices, AdWordsSession $session, $adGroupId ) { // The most trivial partition tree has only a unit node as the root: // $productPartitions->createBiddableUnit(null, null, 100000); $operations = []; $root = ProductPartitions::createSubdivision(); $criterion = ProductPartitions::asBiddableAdGroupCriterion($adGroupId, $root); $operation = ProductPartitions::createAddOperation($criterion); $operations[] = $operation; $newCondition = new ProductCanonicalCondition(); $newCondition->setCondition(ProductCanonicalConditionCondition::NEW_VALUE); $newConditionUnit = ProductPartitions::createUnit($root, $newCondition); $criterion = ProductPartitions::asBiddableAdGroupCriterion( $adGroupId, $newConditionUnit, 200000 ); $operation = ProductPartitions::createAddOperation($criterion); $operations[] = $operation; $usedCondition = new ProductCanonicalCondition(); $usedCondition->setCondition(ProductCanonicalConditionCondition::USED); $usedConditionUnit = ProductPartitions::createUnit($root, $usedCondition); $criterion = ProductPartitions::asBiddableAdGroupCriterion( $adGroupId, $usedConditionUnit, 100000 ); $operation = ProductPartitions::createAddOperation($criterion); $operations[] = $operation; $otherCondition = ProductPartitions::createSubdivision( $root, new ProductCanonicalCondition() ); $criterion = ProductPartitions::asBiddableAdGroupCriterion( $adGroupId, $otherCondition ); $operation = ProductPartitions::createAddOperation($criterion); $operations[] = $operation; $coolBrand = new ProductBrand(); $coolBrand->setValue('CoolBrand'); $coolBrandUnit = ProductPartitions::createUnit($otherCondition, $coolBrand); $criterion = ProductPartitions::asBiddableAdGroupCriterion( $adGroupId, $coolBrandUnit, 900000 ); $operation = ProductPartitions::createAddOperation($criterion); $operations[] = $operation; $cheapBrand = new ProductBrand(); $cheapBrand->setValue('CheapBrand'); $cheapBrandUnit = ProductPartitions::createUnit($otherCondition, $cheapBrand); $criterion = ProductPartitions::asBiddableAdGroupCriterion( $adGroupId, $cheapBrandUnit, 10000 ); $operation = ProductPartitions::createAddOperation($criterion); $operations[] = $operation; $otherBrand = ProductPartitions::createSubdivision( $otherCondition, new ProductBrand() ); $criterion = ProductPartitions::asBiddableAdGroupCriterion( $adGroupId, $otherBrand ); $operation = ProductPartitions::createAddOperation($criterion); $operations[] = $operation; // The value for the bidding category is a fixed ID for the 'Luggage & Bags' // category. You can retrieve IDs for categories from the // ConstantDataService. // See the 'GetProductCategoryTaxonomy' example for more details. $productBiddingCategory = new ProductBiddingCategory(); $productBiddingCategory->setType(ProductDimensionType::BIDDING_CATEGORY_L1); $productBiddingCategory->setValue(-5914235892932915235); $productBiddingCategoryUnit = ProductPartitions::createUnit($otherBrand, $productBiddingCategory); $criterion = ProductPartitions::asBiddableAdGroupCriterion( $adGroupId, $productBiddingCategoryUnit, 750000 ); $operation = ProductPartitions::createAddOperation($criterion); $operations[] = $operation; $productBiddingCategory = new ProductBiddingCategory(); $productBiddingCategory->setType(ProductDimensionType::BIDDING_CATEGORY_L1); $productBiddingCategoryUnit = ProductPartitions::createUnit($otherBrand, $productBiddingCategory); $criterion = ProductPartitions::asBiddableAdGroupCriterion( $adGroupId, $productBiddingCategoryUnit, 110000 ); $operation = ProductPartitions::createAddOperation($criterion); $operations[] = $operation; $adGroupCriterionService = $adWordsServices->get($session, AdGroupCriterionService::class); // Creates ad group criteria on the server. $adGroupCriterionService->mutate($operations); // Display the production partition tree. printf( "%s\n", ProductPartitions::showAdGroupTree( $adWordsServices, $session, $adGroupId ) ); } public static function main() { // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Construct an API session configured from a properties file and the // OAuth2 credentials above. $session = (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build(); self::runExample( new AdWordsServices(), $session, intval(self::AD_GROUP_ID) ); } } AddProductPartitionTree::main();
Set a product scope for a campaign
<?php /** * Copyright 2017 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\AdsApi\Examples\AdWords\v201809\ShoppingCampaigns; require __DIR__ . '/../../../../vendor/autoload.php'; use Google\AdsApi\AdWords\AdWordsServices; use Google\AdsApi\AdWords\AdWordsSession; use Google\AdsApi\AdWords\AdWordsSessionBuilder; use Google\AdsApi\AdWords\v201809\cm\CampaignCriterion; use Google\AdsApi\AdWords\v201809\cm\CampaignCriterionOperation; use Google\AdsApi\AdWords\v201809\cm\CampaignCriterionService; use Google\AdsApi\AdWords\v201809\cm\Operator; use Google\AdsApi\AdWords\v201809\cm\ProductBiddingCategory; use Google\AdsApi\AdWords\v201809\cm\ProductBrand; use Google\AdsApi\AdWords\v201809\cm\ProductCanonicalCondition; use Google\AdsApi\AdWords\v201809\cm\ProductCanonicalConditionCondition; use Google\AdsApi\AdWords\v201809\cm\ProductCustomAttribute; use Google\AdsApi\AdWords\v201809\cm\ProductDimensionType; use Google\AdsApi\AdWords\v201809\cm\ProductOfferId; use Google\AdsApi\AdWords\v201809\cm\ProductScope; use Google\AdsApi\AdWords\v201809\cm\ProductType; use Google\AdsApi\Common\OAuth2TokenBuilder; /** * This example restricts the products that will be included in the campaign by * setting a ProductScope. */ class AddProductScope { const CAMPAIGN_ID = 'INSERT_CAMPAIGN_ID_HERE'; public static function runExample( AdWordsServices $adWordsServices, AdWordsSession $session, $campaignId ) { $campaignCriterionService = $adWordsServices->get($session, CampaignCriterionService::class); $productScope = new ProductScope(); // This set of dimensions is for demonstration purposes only. It would be // extremely unlikely that you want to include so many dimensions in your // product scope. $productBrand = new ProductBrand(); $productBrand->setValue('Nexus'); $productCanonicalCondition = new ProductCanonicalCondition(); $productCanonicalCondition->setCondition( ProductCanonicalConditionCondition::NEW_VALUE ); $productCustomAttribute = new ProductCustomAttribute(); $productCustomAttribute->setType(ProductDimensionType::CUSTOM_ATTRIBUTE_0); $productCustomAttribute->setValue('my attribute value'); $productOfferId = new ProductOfferId(); $productOfferId->setValue('book1'); $productType1 = new ProductType(); $productType1->setType(ProductDimensionType::PRODUCT_TYPE_L1); $productType1->setValue('Media'); $productType2 = new ProductType(); $productType2->setType(ProductDimensionType::PRODUCT_TYPE_L2); $productType2->setValue('Books'); // The value for the bidding category is a fixed ID for the 'Luggage & Bags' // category. You can retrieve IDs for categories from the // ConstantDataService. // See the 'GetProductCategoryTaxonomy' example for more details. $productBiddingCategory = new ProductBiddingCategory(); $productBiddingCategory->setType(ProductDimensionType::BIDDING_CATEGORY_L1); $productBiddingCategory->setValue(-5914235892932915235); $productScope->setDimensions( [ $productBrand, $productCanonicalCondition, $productCustomAttribute, $productOfferId, $productType1, $productType2, $productBiddingCategory ] ); $campaignCriterion = new CampaignCriterion(); $campaignCriterion->setCampaignId($campaignId); $campaignCriterion->setCriterion($productScope); // Create a campaign criterion operation and add it to the operations list. $operations = []; $operation = new CampaignCriterionOperation(); $operation->setOperand($campaignCriterion); $operation->setOperator(Operator::ADD); $operations[] = $operation; // Create the campaign criterion on the server and print out some // information. $campaignCriterionService->mutate($operations); printf( "Created a ProductScope criterion with ID %d.\n", $campaignCriterion->getCriterion()->getId() ); } public static function main() { // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Construct an API session configured from a properties file and the // OAuth2 credentials above. $session = (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build(); self::runExample( new AdWordsServices(), $session, intval(self::CAMPAIGN_ID) ); } } AddProductScope::main();
Add a Shopping campaign
<?php /** * Copyright 2017 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\AdsApi\Examples\AdWords\v201809\ShoppingCampaigns; require __DIR__ . '/../../../../vendor/autoload.php'; use Google\AdsApi\AdWords\AdWordsServices; use Google\AdsApi\AdWords\AdWordsSession; use Google\AdsApi\AdWords\AdWordsSessionBuilder; use Google\AdsApi\AdWords\v201809\cm\AdGroup; use Google\AdsApi\AdWords\v201809\cm\AdGroupAd; use Google\AdsApi\AdWords\v201809\cm\AdGroupAdOperation; use Google\AdsApi\AdWords\v201809\cm\AdGroupAdService; use Google\AdsApi\AdWords\v201809\cm\AdGroupCriterionOperation; use Google\AdsApi\AdWords\v201809\cm\AdGroupCriterionService; use Google\AdsApi\AdWords\v201809\cm\AdGroupOperation; use Google\AdsApi\AdWords\v201809\cm\AdGroupService; use Google\AdsApi\AdWords\v201809\cm\AdvertisingChannelType; use Google\AdsApi\AdWords\v201809\cm\BiddableAdGroupCriterion; use Google\AdsApi\AdWords\v201809\cm\BiddingStrategyConfiguration; use Google\AdsApi\AdWords\v201809\cm\BiddingStrategyType; use Google\AdsApi\AdWords\v201809\cm\Budget; use Google\AdsApi\AdWords\v201809\cm\Campaign; use Google\AdsApi\AdWords\v201809\cm\CampaignOperation; use Google\AdsApi\AdWords\v201809\cm\CampaignService; use Google\AdsApi\AdWords\v201809\cm\CampaignStatus; use Google\AdsApi\AdWords\v201809\cm\CpcBid; use Google\AdsApi\AdWords\v201809\cm\Money; use Google\AdsApi\AdWords\v201809\cm\Operator; use Google\AdsApi\AdWords\v201809\cm\ProductAd; use Google\AdsApi\AdWords\v201809\cm\ProductPartition; use Google\AdsApi\AdWords\v201809\cm\ProductPartitionType; use Google\AdsApi\AdWords\v201809\cm\ShoppingSetting; use Google\AdsApi\Common\OAuth2TokenBuilder; /** * This example adds a shopping campaign. */ class AddShoppingCampaign { const BUDGET_ID = 'INSERT_BUDGET_ID_HERE'; const MERCHANT_ID = 'INSERT_MERCHANT_ID_HERE'; // If set to true, a default partition will be created. If running the // AddProductPartitionTree.php example right after this example, make sure // this stays set to false. const SHOULD_CREATE_DEFAULT_PARTITION = false; public static function runExample( AdWordsServices $adWordsServices, AdWordsSession $session, $budgetId, $merchantId, $shouldCreateDefaultPartition ) { $campaignService = $adWordsServices->get($session, CampaignService::class); $adGroupService = $adWordsServices->get($session, AdGroupService::class); $adGroupAdService = $adWordsServices->get($session, AdGroupAdService::class); $adGroupCriterionService = $adWordsServices->get($session, AdGroupCriterionService::class); // Create a campaign with required and optional settings. $campaign = new Campaign(); $campaign->setName('Shopping campaign #' . uniqid()); // The advertisingChannelType is what makes this a Shopping campaign $campaign->setAdvertisingChannelType(AdvertisingChannelType::SHOPPING); // Recommendation: Set the campaign to PAUSED when creating it to stop // the ads from immediately serving. Set to ENABLED once you've added // targeting and the ads are ready to serve. $campaign->setStatus(CampaignStatus::PAUSED); // Set portfolio budget (required). $campaign->setBudget(new Budget()); $campaign->getBudget()->setBudgetId($budgetId); // Set bidding strategy (required). $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); $biddingStrategyConfiguration->setBiddingStrategyType( BiddingStrategyType::MANUAL_CPC ); $campaign->setBiddingStrategyConfiguration($biddingStrategyConfiguration); // All Shopping campaigns need a ShoppingSetting. $shoppingSetting = new ShoppingSetting(); $shoppingSetting->setSalesCountry('US'); $shoppingSetting->setCampaignPriority(0); $shoppingSetting->setMerchantId($merchantId); // Set to "true" to enable Local Inventory Ads in your campaign. $shoppingSetting->setEnableLocal(true); $campaign->setSettings([$shoppingSetting]); // Create a campaign operation and add it to the operations list. $operations = []; $operation = new CampaignOperation(); $operation->setOperand($campaign); $operation->setOperator(Operator::ADD); $operations[] = $operation; // Create the campaign on the server and print out some information. $campaign = $campaignService->mutate($operations)->getValue()[0]; printf( "Campaign with name '%s' and ID %d was added.\n", $campaign->getName(), $campaign->getId() ); // Create ad group. $adGroup = new AdGroup(); $adGroup->setCampaignId($campaign->getId()); $adGroup->setName('Ad Group #' . uniqid()); // Create an ad group operation and add it to the operations list. $operations = []; $operation = new AdGroupOperation(); $operation->setOperand($adGroup); $operation->setOperator(Operator::ADD); $operations[] = $operation; // Create the ad group on the server and print out some information. $adGroup = $adGroupService->mutate($operations)->getValue()[0]; printf( "Ad group with name '%s' and ID %d was added.\n", $adGroup->getName(), $adGroup->getId() ); // Create product ad. $productAd = new ProductAd(); // Create ad group ad. $adGroupAd = new AdGroupAd(); $adGroupAd->setAdGroupId($adGroup->getId()); $adGroupAd->setAd($productAd); // Create an ad group ad operation and add it to the operations list. $operations = []; $operation = new AdGroupAdOperation(); $operation->setOperand($adGroupAd); $operation->setOperator(Operator::ADD); $operations[] = $operation; // Create the ad group ad on the server and print out some information. $adGroupAd = $adGroupAdService->mutate($operations)->getValue()[0]; printf("Product ad with ID %d was added.\n", $adGroupAd->getAd()->getId()); if ($shouldCreateDefaultPartition) { $operation = self::createDefaultPartitionOperation($adGroup->getId(), 500000); $adGroupCriterion = $adGroupCriterionService->mutate([$operation])->getValue()[0]; printf( "Ad group criterion with ID %d in ad group with ID %d was added.\n", $adGroupCriterion->getCriterion()->getId(), $adGroupCriterion->getAdGroupId() ); } } private static function createDefaultPartitionOperation( $adGroupId, $bidAmount ) { // Creates an ad group criterion for 'All products'. $productPartition = new ProductPartition(); $productPartition->setPartitionType(ProductPartitionType::UNIT); // Make sure the caseValue is null and the parentCriterionId is null. $productPartition->setParentCriterionId(null); $productPartition->setCaseValue(null); // Creates a biddable ad group criterion. $criterion = new BiddableAdGroupCriterion(); $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); $cpcBid = new CpcBid(); $money = new Money(); $money->setMicroAmount($bidAmount); $cpcBid->setBid($money); $biddingStrategyConfiguration->setBids([$cpcBid]); $criterion->setBiddingStrategyConfiguration($biddingStrategyConfiguration); $criterion->setAdGroupId($adGroupId); $criterion->setCriterion($productPartition); // Creates an ad group criterion operation. $operation = new AdGroupCriterionOperation(); $operation->setOperand($criterion); $operation->setOperator(Operator::ADD); return $operation; } public static function main() { // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Construct an API session configured from a properties file and the // OAuth2 credentials above. $session = (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build(); self::runExample( new AdWordsServices(), $session, intval(self::BUDGET_ID), intval(self::MERCHANT_ID), boolval(self::SHOULD_CREATE_DEFAULT_PARTITION) ); } } AddShoppingCampaign::main();
Add a Smart Shopping campaign
<?php /** * Copyright 2018 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\AdsApi\Examples\AdWords\v201809\ShoppingCampaigns; require __DIR__ . '/../../../../vendor/autoload.php'; use Google\AdsApi\AdWords\AdWordsServices; use Google\AdsApi\AdWords\AdWordsSession; use Google\AdsApi\AdWords\AdWordsSessionBuilder; use Google\AdsApi\AdWords\v201809\cm\AdGroup; use Google\AdsApi\AdWords\v201809\cm\AdGroupAd; use Google\AdsApi\AdWords\v201809\cm\AdGroupAdOperation; use Google\AdsApi\AdWords\v201809\cm\AdGroupAdService; use Google\AdsApi\AdWords\v201809\cm\AdGroupCriterionOperation; use Google\AdsApi\AdWords\v201809\cm\AdGroupCriterionService; use Google\AdsApi\AdWords\v201809\cm\AdGroupOperation; use Google\AdsApi\AdWords\v201809\cm\AdGroupService; use Google\AdsApi\AdWords\v201809\cm\AdGroupType; use Google\AdsApi\AdWords\v201809\cm\AdvertisingChannelSubType; use Google\AdsApi\AdWords\v201809\cm\AdvertisingChannelType; use Google\AdsApi\AdWords\v201809\cm\BiddableAdGroupCriterion; use Google\AdsApi\AdWords\v201809\cm\BiddingStrategyConfiguration; use Google\AdsApi\AdWords\v201809\cm\BiddingStrategyType; use Google\AdsApi\AdWords\v201809\cm\Budget; use Google\AdsApi\AdWords\v201809\cm\BudgetBudgetDeliveryMethod; use Google\AdsApi\AdWords\v201809\cm\BudgetOperation; use Google\AdsApi\AdWords\v201809\cm\BudgetService; use Google\AdsApi\AdWords\v201809\cm\Campaign; use Google\AdsApi\AdWords\v201809\cm\CampaignOperation; use Google\AdsApi\AdWords\v201809\cm\CampaignService; use Google\AdsApi\AdWords\v201809\cm\CampaignStatus; use Google\AdsApi\AdWords\v201809\cm\GoalOptimizedShoppingAd; use Google\AdsApi\AdWords\v201809\cm\Money; use Google\AdsApi\AdWords\v201809\cm\Operator; use Google\AdsApi\AdWords\v201809\cm\ProductPartition; use Google\AdsApi\AdWords\v201809\cm\ProductPartitionType; use Google\AdsApi\AdWords\v201809\cm\ShoppingSetting; use Google\AdsApi\Common\OAuth2TokenBuilder; /** * This example adds a Smart Shopping campaign with an ad group, and ad group * ad. */ class AddSmartShoppingAd { const MERCHANT_ID = 'INSERT_MERCHANT_ID_HERE'; // If set to true, a default partition will be created. // Set it to false if you plan to run the AddProductPartitionTree.php // example right after this example. const SHOULD_CREATE_DEFAULT_PARTITION = true; public static function runExample( AdWordsServices $adWordsServices, AdWordsSession $session, $merchantId, $shouldCreateDefaultPartition ) { $budgetId = self::createBudget($adWordsServices, $session); $campaignId = self::createSmartCampaign( $adWordsServices, $session, $budgetId, $merchantId ); $adGroupId = self::createSmartShoppingAdGroup( $adWordsServices, $session, $campaignId ); self::createSmartShoppingAd($adWordsServices, $session, $adGroupId); if ($shouldCreateDefaultPartition) { self::createDefaultPartition( $adWordsServices, $session, $adGroupId ); } } /** * Creates a non-shared budget for a Smart Shopping campaign. Smart Shopping * campaigns support only non-shared budgets. * * @param AdWordsServices $adWordsServices * @param AdWordsSession $session * @return int the created budget ID */ private static function createBudget( AdWordsServices $adWordsServices, AdWordsSession $session ) { $budgetService = $adWordsServices->get($session, BudgetService::class); $budget = new Budget(); $budget->setName('Interplanetary Cruise Budget #' . uniqid()); $money = new Money(); // This budget equals 50.00 units of your account's currency, e.g., // 50 USD if your currency is USD. $money->setMicroAmount(50000000); $budget->setAmount($money); $budget->setDeliveryMethod(BudgetBudgetDeliveryMethod::STANDARD); // Non-shared budgets are required for Smart Shopping campaigns. $budget->setIsExplicitlyShared(false); // Create a budget operation. $operation = new BudgetOperation(); $operation->setOperand($budget); $operation->setOperator(Operator::ADD); // Create the budget on the server. $addedBudget = $budgetService->mutate([$operation])->getValue()[0]; printf( "Budget with name '%s' and ID %d was created.%s", $addedBudget->getName(), $addedBudget->getBudgetId(), PHP_EOL ); return $addedBudget->getBudgetId(); } /** * Creates a Smart Shopping campaign. * * @param AdWordsServices $adWordsServices the AdWords services * @param AdWordsSession $session the AdWords session * @param int $budgetId the budget ID * @param int $merchantId the Merchant center ID * @return int the created campaign ID */ private static function createSmartCampaign( AdWordsServices $adWordsServices, AdWordsSession $session, $budgetId, $merchantId ) { $campaignService = $adWordsServices->get($session, CampaignService::class); // Create a campaign with required and optional settings. $campaign = new Campaign(); $campaign->setName('Smart Shopping campaign #' . uniqid()); // The advertisingChannelType is what makes this a Shopping campaign. $campaign->setAdvertisingChannelType(AdvertisingChannelType::SHOPPING); // Sets the advertisingChannelSubType to SHOPPING_GOAL_OPTIMIZED_ADS to // make this a Smart Shopping campaign. $campaign->setAdvertisingChannelSubType( AdvertisingChannelSubType::SHOPPING_GOAL_OPTIMIZED_ADS ); // Recommendation: Set the campaign to PAUSED when creating it to stop // the ads from immediately serving. Set to ENABLED once you've added // targeting and the ads are ready to serve. $campaign->setStatus(CampaignStatus::PAUSED); // Set a budget. $budget = new Budget(); $budget->setBudgetId($budgetId); $campaign->setBudget($budget); // Set a bidding strategy. Only MAXIMIZE_CONVERSION_VALUE is supported. $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); $biddingStrategyConfiguration->setBiddingStrategyType( BiddingStrategyType::MAXIMIZE_CONVERSION_VALUE ); $campaign->setBiddingStrategyConfiguration( $biddingStrategyConfiguration ); // All Shopping campaigns need a ShoppingSetting. $shoppingSetting = new ShoppingSetting(); $shoppingSetting->setSalesCountry('US'); $shoppingSetting->setMerchantId($merchantId); $campaign->setSettings([$shoppingSetting]); // Create a campaign operation and add it to the operations list. $operation = new CampaignOperation(); $operation->setOperand($campaign); $operation->setOperator(Operator::ADD); // Create the campaign on the server and print out some information. $addedCampaign = $campaignService->mutate([$operation])->getValue()[0]; printf( "Smart Shopping campaign with name '%s' and ID %d was added.%s", $addedCampaign->getName(), $addedCampaign->getId(), PHP_EOL ); return $addedCampaign->getId(); } /** * Creates a Smart Shopping ad group by setting the ad group type to * SHOPPING_GOAL_OPTIMIZED_ADS. * * @param AdWordsServices $adWordsServices the AdWords services * @param AdWordsSession $session the AdWords session * @param int $campaignId the campaign ID * @return int the ad group ID */ private static function createSmartShoppingAdGroup( AdWordsServices $adWordsServices, AdWordsSession $session, $campaignId ) { $adGroupService = $adWordsServices->get($session, AdGroupService::class); // Create an ad group. $adGroup = new AdGroup(); $adGroup->setCampaignId($campaignId); $adGroup->setName('Smart Shopping ad group #' . uniqid()); // Set the ad group type to SHOPPING_GOAL_OPTIMIZED_ADS. $adGroup->setAdGroupType(AdGroupType::SHOPPING_GOAL_OPTIMIZED_ADS); // Create operation. $adGroupOperation = new AdGroupOperation(); $adGroupOperation->setOperand($adGroup); $adGroupOperation->setOperator(Operator::ADD); // Create the ad group on the server and print out some information. $addedAdGroup = $adGroupService->mutate([$adGroupOperation]) ->getValue()[0]; printf( "Smart Shopping ad group with name '%s' and ID %d was added.%s", $addedAdGroup->getName(), $addedAdGroup->getId(), PHP_EOL ); return $addedAdGroup->getId(); } /** * Creates a Smart Shopping ad. * * @param AdWordsServices $adWordsServices the AdWords services * @param AdWordsSession $session the AdWords session * @param int $adGroupId the ad group ID */ private static function createSmartShoppingAd( AdWordsServices $adWordsServices, AdWordsSession $session, $adGroupId ) { $adGroupAdService = $adWordsServices->get($session, AdGroupAdService::class); // Create a Smart Shopping ad (Goal-optimized Shopping ad). $smartShoppingAd = new GoalOptimizedShoppingAd(); // Create an ad group ad. $adGroupAd = new AdGroupAd(); $adGroupAd->setAdGroupId($adGroupId); $adGroupAd->setAd($smartShoppingAd); // Create an ad group ad operation and add it to the operations list. $operation = new AdGroupAdOperation(); $operation->setOperand($adGroupAd); $operation->setOperator(Operator::ADD); // Create the ad group ad on the server and print out some information. $addedAdGroupAd = $adGroupAdService->mutate([$operation]) ->getValue()[0]; printf( "Smart Shopping ad with ID %d was added.%s", $addedAdGroupAd->getAd()->getId(), PHP_EOL ); } /** * Creates a default product partition as an ad group criterion. * * @param AdWordsServices $adWordsServices the AdWords services * @param AdWordsSession $session the AdWords session * @param int $adGroupId the ad group ID */ private static function createDefaultPartition( AdWordsServices $adWordsServices, AdWordsSession $session, $adGroupId ) { $adGroupCriterionService = $adWordsServices->get($session, AdGroupCriterionService::class); // Creates an ad group criterion for 'All products'. $productPartition = new ProductPartition(); $productPartition->setPartitionType(ProductPartitionType::UNIT); // Creates a biddable ad group criterion. $criterion = new BiddableAdGroupCriterion(); $criterion->setAdGroupId($adGroupId); $criterion->setCriterion($productPartition); // Creates an ad group criterion operation. $operation = new AdGroupCriterionOperation(); $operation->setOperand($criterion); $operation->setOperator(Operator::ADD); $addedAdGroupCriterion = $adGroupCriterionService->mutate([$operation]) ->getValue()[0]; printf( "Ad group criterion with ID %d in ad group with ID %d" . " was added.%s", $addedAdGroupCriterion->getCriterion()->getId(), $addedAdGroupCriterion->getAdGroupId(), PHP_EOL ); } public static function main() { // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Construct an API session configured from a properties file and the // OAuth2 credentials above. $session = (new AdWordsSessionBuilder())->fromFile() ->withOAuth2Credential($oAuth2Credential) ->build(); self::runExample( new AdWordsServices(), $session, intval(self::MERCHANT_ID), boolval(self::SHOULD_CREATE_DEFAULT_PARTITION) ); } } AddSmartShoppingAd::main();
Get the set of product bidding categories
<?php /** * Copyright 2017 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\AdsApi\Examples\AdWords\v201809\ShoppingCampaigns; require __DIR__ . '/../../../../vendor/autoload.php'; use Google\AdsApi\AdWords\AdWordsServices; use Google\AdsApi\AdWords\AdWordsSession; use Google\AdsApi\AdWords\AdWordsSessionBuilder; use Google\AdsApi\AdWords\v201809\cm\ConstantDataService; use Google\AdsApi\AdWords\v201809\cm\Predicate; use Google\AdsApi\AdWords\v201809\cm\PredicateOperator; use Google\AdsApi\AdWords\v201809\cm\Selector; use Google\AdsApi\Common\OAuth2TokenBuilder; /** * This example fetches the set of valid `ProductBiddingCategory`. */ class GetProductCategoryTaxonomy { public static function runExample( AdWordsServices $adWordsServices, AdWordsSession $session ) { $constantDataService = $adWordsServices->get($session, ConstantDataService::class); // Create a selector to select product bidding categories in US. $selector = new Selector(); $selector->setPredicates( [new Predicate('Country', PredicateOperator::IN, ['US'])] ); // A mapping from ID to bidding category. $biddingCategories = []; // An array of root bidding categories (those that don't have any parents). $rootCategories = []; $productBiddingCategories = $constantDataService->getProductBiddingCategoryData($selector); foreach ($productBiddingCategories as $productBiddingCategory) { $id = $productBiddingCategory->getDimensionValue()->getvalue(); $name = $productBiddingCategory->getDisplayValue()[0]->getvalue(); $parentId = ($productBiddingCategory->getParentDimensionValue() !== null) ? $productBiddingCategory->getParentDimensionValue()->getvalue() : null; if (array_key_exists($id, $biddingCategories) === false) { $biddingCategories[$id] = [ 'children' => [] ]; } $biddingCategories[$id]['id'] = $id; $biddingCategories[$id]['name'] = $name; if ($parentId !== null) { if (array_key_exists($parentId, $biddingCategories) === false) { // 'id' and 'name' of $biddingCategories[$parentId] will get // populated in its iteration of this loop. $biddingCategories[$parentId] = [ 'children' => [] ]; } $biddingCategories[$parentId]['children'][] = $biddingCategories[$id]; } else { $rootCategories[] = $biddingCategories[$id]; } } self::displayCategories($rootCategories, ''); } private static function displayCategories(array $categories, $prefix) { foreach ($categories as $category) { printf("%s%s [%s]\n", $prefix, $category['name'], $category['id']); if (count($category['children']) > 0) { self::displayCategories( $category['children'], sprintf('%s%s > ', $prefix, $category['name']) ); } } } public static function main() { // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Construct an API session configured from a properties file and the // OAuth2 credentials above. $session = (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build(); self::runExample(new AdWordsServices(), $session); } } GetProductCategoryTaxonomy::main();