繁体   English   中英

如何使用谷歌广告词API的PHP客户端库创建产品组(产品分区)?

[英]How to create the Product groups (Product Partition) using php client library of google ad words API?

我正在使用Google AdWords API,使用Google小组提供的PHP客户端库创建广告组和产品组(产品分区)。 我已使用该库成功创建了广告组。 但我仍然坚持为该广告组创建产品组。

我肯定会创建我们需要购物广告系列的产品组。 我尝试使用客户端库中提供的脚本“AddProductPartitionTree.php”为该购物广告系列创建产品分区。 但每次它都会抛出一个错误

[AdGroupCriterionError.PRODUCT_PARTITION_ALREADY_EXISTS @ operations [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中的广告组创建产品组。

帮助将不胜感激。 谢谢

首先删除现有分区并创建新分区。


//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.
`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM