如何使用Google Adwords API在多个广告系列中创建一个广告组?

时间:2019-01-23 08:47:26

标签: google-adwords adwords-api-v201802

我正在使用Google Adwords API。我正在使用PHP客户端库。我打算为多个广告系列创建一个广告组。 我正在循环执行此操作,并且成功为第一次迭代创建了广告组,但随后在enter image description here

下抛出错误

我们将不胜感激。 如果有人想要代码,我也会提供。 谢谢

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。实际上,对于每个错误的迭代,我都使用相同的服务实例。

  

$ adGroupService = $ adWordsServices-> get($ session,   AdGroupService :: class);

$fields =['Id','Name','Status','ServingStatus','AdvertisingChannelType','AdvertisingChannelSubType'];
            $campaignService = $adWordsServices->get($session, CampaignService::class);
            $adGroupId = 0;
            for ($i=0; $i <count($campaignNameArr) ; $i++) { 
                $operations =[];
                $adGroupService =  $adWordsServices->get($session, AdGroupService::class); // HERE IS THE LINE OF CODE.
                $_collection = self::fetchCampaigns(
                    $request,
                   $campaignService,
                    $fields,
                    $entriesPerPage,
                    $pageNo,
                    $campaignNameArr[$i]
                );

                if(count($_collection) >0)
                {
                    $flag= true;
                    foreach ($_collection as $key => $campaign) {

                        $campaignId = $campaign->getID();
                    }
                }
                else
                {
                    echo "No Campaign with name of :". $campaignNameArr[$i];
                    echo "<br/>";
                    continue;
                }              

                $msg = 'Created';
                echo $campaignId."<br/>";


                // Create an ad group with required and optional settings.
                $adGroup = new AdGroup();
                $adGroup->setCampaignId($campaignId);
                // $adGroup->setName($productTitle.' #' . uniqid());
                $adGroup->setName($productTitle);
                // Set bids (required).
                $bid = new CpcBid();
                $money = new Money();
                $money->setMicroAmount(10000);
                $bid->setBid($money);
                $biddingStrategyConfiguration = new BiddingStrategyConfiguration();
                $biddingStrategyConfiguration->setBids([$bid]);
                $adGroup->setBiddingStrategyConfiguration($biddingStrategyConfiguration);

                // Set additional settings (optional).
                $adGroup->setStatus(AdGroupStatus::ENABLED);

                // Targeting restriction settings. Depending on the criterionTypeGroup
                // value, most TargetingSettingDetail only affect Display campaigns.
                // However, the USER_INTEREST_AND_LIST value works for RLSA campaigns -
                // Search campaigns targeting using a remarketing list.
                $targetingSetting = new TargetingSetting();
                $details = [];
                // Restricting to serve ads that match your ad group placements.
                // This is equivalent to choosing "Target and bid" in the UI.
                $details[] = new TargetingSettingDetail(CriterionTypeGroup::PLACEMENT, false);
                // Using your ad group verticals only for bidding. This is equivalent
                // to choosing "Bid only" in the UI.
                $details[] = new TargetingSettingDetail(CriterionTypeGroup::VERTICAL, true);
                $targetingSetting->setDetails($details);
                $adGroup->setSettings([$targetingSetting]);

                // Set the rotation mode.
                $rotationMode = new AdGroupAdRotationMode(AdRotationMode::OPTIMIZE);
                $adGroup->setAdGroupAdRotationMode($rotationMode);

                // Create an ad group operation and add it to the operations list.
                $operation = new AdGroupOperation();
                $operation->setOperand($adGroup);
                $operation->setOperator(Operator::ADD);
                $operations[] = $operation;

                // Create the ad groups on the server and print out some information for
                // each created ad group.
                $result = $adGroupService->mutate($operations);
                $adGroupId = $adGroup->getId();
                foreach ($result->getValue() as $adGroup) {
                    printf(
                        "Ad group with name '%s' and ID %d was added.\n",
                        $adGroup->getName(),
                        $adGroup->getId()
                    );
                }            

            }