customerId = getenv('GOOGLE_ADS_CUSTOMER_ID'); // OAuth2 Token Authentication $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Google Ads Client initialization $this->googleAdsClient = (new GoogleAdsClientBuilder()) ->fromFile() ->withOAuth2Credential($oAuth2Credential) ->build(); } /** * This example updates a campaign by setting the status to `PAUSED`. To get campaigns, run * GetCampaigns.php. */ /* @param int $customerId the customer ID * @param $options * @return mixed * @throws ApiException */ public function runAddGroup($options): mixed { $googleAdsClient = $this->googleAdsClient; // Creates a single shared budget to be used by the campaigns added below. $resourceNames = self::addGroup($googleAdsClient, $options['customer_id'], $options['campaign_id']); return $resourceNames; } /** * Runs the addGroup example. * * @param GoogleAdsClient $googleAdsClient the Google Ads API client * @param int $customerId the customer ID * @param int $campaignId the campaign ID to add ad groups to */ public static function addGroup( GoogleAdsClient $googleAdsClient, int $customerId, int $campaignId ) { $campaignResourceName = ResourceNames::forCampaign($customerId, $campaignId); $operations = []; // Constructs an ad group and sets an optional CPC value. $adGroup1 = new AdGroup([ 'name' => 'Earth to Mars Cruises #' . Helper::getPrintableDatetime(), 'campaign' => $campaignResourceName, 'status' => AdGroupStatus::ENABLED, 'type' => AdGroupType::SEARCH_STANDARD, 'cpc_bid_micros' => 10000000 ]); $adGroupOperation1 = new AdGroupOperation(); $adGroupOperation1->setCreate($adGroup1); $operations[] = $adGroupOperation1; // Constructs another ad group. $adGroup2 = new AdGroup([ 'name' => 'Earth to Venus Cruises #' . Helper::getPrintableDatetime(), 'campaign' => $campaignResourceName, 'status' => AdGroupStatus::ENABLED, 'type' => AdGroupType::SEARCH_STANDARD, 'cpc_bid_micros' => 20000000 ]); $adGroupOperation2 = new AdGroupOperation(); $adGroupOperation2->setCreate($adGroup2); $operations[] = $adGroupOperation2; // Issues a mutate request to add the ad groups. $adGroupServiceClient = $googleAdsClient->getAdGroupServiceClient(); $response = $adGroupServiceClient->mutateAdGroups(MutateAdGroupsRequest::build( $customerId, $operations )); $resourceNames = []; printf("Added %d ad groups:%s", $response->getResults()->count(), PHP_EOL); foreach ($response->getResults() as $addedAdGroup) { /** @var AdGroup $addedAdGroup */ print $addedAdGroup->getResourceName() . PHP_EOL; $resourceNames[] = $addedAdGroup->getResourceName(); } return $resourceNames; } }