我正在与Google AdWords API一起使用Google团队提供的PHP客户端库来创建广告组和产品组(产品分区)。 我已经使用该库成功创建了广告组。但是我很想为该广告组创建产品组。
我确定会创建我们需要购物广告系列的产品组。我尝试使用客户端库中提供的脚本“ AddProductPartitionTree.php”为该购物广告系列创建产品分区。但是每次抛出错误
[AdGroupCriterionError.PRODUCT_PARTITION_ALREADY_EXISTS @操作[8]。
在创建产品分区的根节点时出现了问题
我在下面共享产品分区脚本的代码:
private function createProductPartition(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();
// print_r($root);
$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('aerotech');
$coolBrandUnit = ProductPartitions::createUnit($otherCondition, $coolBrand);
$criterion = ProductPartitions::asBiddableAdGroupCriterion(
$adGroupId,
$coolBrandUnit,
900000
);
$operation = ProductPartitions::createAddOperation($criterion);
$operations[] = $operation;
$cheapBrand = new ProductBrand();
$cheapBrand->setValue('taylormade');
$cheapBrandUnit = ProductPartitions::createUnit($otherCondition, $cheapBrand);
$criterion = ProductPartitions::asBiddableAdGroupCriterion(
$adGroupId,
$cheapBrandUnit,
10000
);
$operation = ProductPartitions::createAddOperation($criterion);
$operations[] = $operation;
$otherBrand = ProductPartitions::createSubdivision(
$otherCondition,
new ProductBrand()
);
// print_r($otherBrand);
$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::UNKNOWN);
$productBiddingCategory->setType(ProductDimensionType::BIDDING_CATEGORY_L1);
// $productBiddingCategory->setValue(-5914235892932915235);
// $productBiddingCategory->setValue(6085370270382700000);
$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);
print_r($operations);
exit;
// Creates ad group criteria on the server.
$adGroupCriterionService->mutate($operations);
// Display the production partition tree.
printf(
"%s\n",
ProductPartitions::showAdGroupTree(
$adWordsServices,
$session,
$adGroupId
)
);
}
我想获得帮助,为AdWords中的广告组创建产品组。
我们将不胜感激。 谢谢
答案 0 :(得分:0)
首先删除现有分区,然后创建新分区。
//this might work
$selector = new Selector();
$selector->setFields(array('Id', 'CpcBid', 'CaseValue', 'ParentCriterionId', 'PartitionType'));
$selector->setPredicates(array(new Predicate('AdGroupId', 'IN', [$adGroupId]),
new Predicate('Status', 'NOT_IN', [\Google\AdsApi\AdWords\v201809\cm\UserStatus::REMOVED])
));
$selector->setPaging(new Paging(0, self::PAGE_LIMIT));
$totalNumberOfEntries = 0;
try {
do {
echo '<pre>';
$page = $adGroupCriterionService->get($selector);
if ($page->getEntries() !== null) {
$totalNumberOfEntries = $page->getTotalNumEntries();
foreach ($page->getEntries() as $entry) {
$subId = strval($entry->getCriterion()->getId());
$parId = strval($entry->getCriterion()->getParentCriterionId());
}
}
$selector->getPaging()->setStartIndex($selector->getPaging()->getStartIndex() + self::PAGE_LIMIT);
} while ($selector->getPaging()->getStartIndex() < $totalNumberOfEntries);
} catch (Exception $ex) {
}
$criterion = new Criterion();
$criterion->setCriterionType(\Google\AdsApi\AdWords\v201809\cm\CriterionType::PRODUCT_PARTITION);
$criterion->setId($parId);
// Create an ad group criterion.
$adGroupCriterion = new AdGroupCriterion();
$adGroupCriterion->setAdGroupId($adGroupId);
$adGroupCriterion->setCriterion($criterion);
// Create an ad group criterion operation and add it the operations list.
$operation = new AdGroupCriterionOperation();
$operation->setOperand($adGroupCriterion);
$operation->setOperator(Operator::REMOVE);
$operations = [$operation];
$adGroupCriterionService->mutate($operations);
#in case of TOO LOW error, create a subdivision.
`