0

I am working with Google AdWords API to create ad groups and product groups (Product partitions) using the PHP client library provided by Google team. I have successfully created the ad groups using that library. But I am stuck at the point to create product groups for that ad group.

I am sure to create product groups we need Shopping campaigns. I have tried to create products partitions for that shopping campaign using the script "AddProductPartitionTree.php" provided in the client library. But every time it throws an error

[AdGroupCriterionError.PRODUCT_PARTITION_ALREADY_EXISTS @ operations[8].

There is something wrong while creating the root node of the product partitions

I am sharing the code of product partition script below:

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
        )
    );

}

I wanted to get help to create product groups for ad groups in AdWords.

Help would be appreciated. Thanks

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
U.Malik
  • 111
  • 11

1 Answers1

1

Delete your existing partitions first and create new.


//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.
`
subinpvasu
  • 36
  • 4