How to exclude placements with PHP APIs ?

260 views
Skip to first unread message

Maferyt Letakol

unread,
Aug 4, 2017, 5:35:02 PM8/4/17
to AdWords API Forum
Hi everyone,

Actually I'm setting some BiddableAdGroupCriterion with userStatus = 'PAUSED', but I want "placements" to be "excluded".
;
My code :
private function createAdGroupCriterionOperation(array $criterion)
{
    $adGroupCriterion            = new \BiddableAdGroupCriterion();
    $adGroupCriterion->adGroupId = $criterion['adGroupId'];
    $adGroupCriterion->criterion = new \Criterion($criterion['id']);
    $operator = 'SET';

    if ('pause' == $criterion['type']) {
            $adGroupCriterion->userStatus = 'PAUSED';

            if ($criterion['criterionType'] == "placement") {
                  $adGroupCriterion = new \NegativeAdGroupCriterion();
                  $adGroupCriterion->adGroupId = $criterion['adGroupId'];
                  $adGroupCriterion->criterion = new \Criterion($criterion['id']);
                  $operator = 'ADD';
            }
    }

    // Create operation.
    $operation           = new \AdGroupCriterionOperation();
    $operation->operand  = $adGroupCriterion;
    $operation->operator = $operator;

    return $operation;
}

But this is not workind, it does anything :/

I've checked docs & examples but can't figure out what to do.

I wish there would be an "EXCLUDED" status...


Thanks !

Garik Tate

unread,
Aug 6, 2017, 12:32:58 AM8/6/17
to AdWords API Forum
You're going to need to add a negative placement criterion. It's basically the same as adding a negative keyword.

Here is basically what we did, hope that helps: 
<?php

require 'vendor/autoload.php';


use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201705\cm\CampaignCriterion;
use Google\AdsApi\AdWords\v201705\cm\CampaignCriterionOperation;
use Google\AdsApi\AdWords\v201705\cm\CampaignCriterionService;
use Google\AdsApi\AdWords\v201705\cm\ConstantOperand;
use Google\AdsApi\AdWords\v201705\cm\ConstantOperandConstantType;
use Google\AdsApi\AdWords\v201705\cm\ConstantOperandUnit;
use Google\AdsApi\AdWords\v201705\cm\FunctionOperator;
use Google\AdsApi\AdWords\v201705\cm\Placement;
use Google\AdsApi\AdWords\v201705\cm\PlacementMatchType;
use Google\AdsApi\AdWords\v201705\cm\Location;
use Google\AdsApi\AdWords\v201705\cm\LocationExtensionOperand;
use Google\AdsApi\AdWords\v201705\cm\LocationGroups;
use Google\AdsApi\AdWords\v201705\cm\Language;
use Google\AdsApi\AdWords\v201705\cm\MatchingFunction;
use Google\AdsApi\AdWords\v201705\cm\NegativeCampaignCriterion;
use Google\AdsApi\AdWords\v201705\cm\Operator;
use Google\AdsApi\Common\OAuth2TokenBuilder;


/**
 * This example adds various types of targeting criteria to a campaign.
 * To get campaigns, run BasicOperations/GetCampaigns.php.
 */

class AddCampaignTargetingCrtieria {


 
const CAMPAIGN_ID = '869044770';


 
public static function runExample(AdWordsServices $adWordsServices,
     
AdWordsSession $session, $campaignId) {
    $campaignCriterionService
=
        $adWordsServices
->get($session, CampaignCriterionService::class);


    $campaignCriteria
= [];
    $operations
= [];
   
foreach ($campaignCriteria as $campaignCriterion) {
      $operation
= new CampaignCriterionOperation();
      $operation
->setOperator(Operator::ADD);
      $operation
->setOperand($campaignCriterion);
      $operations
[] = $operation;
   
}


   
// Add a negative campaign criterion.
    $negativePlacement
= new Placement();
    $negativePlacement
->setUrl('lovebscott.com');
   
//$negativePlacement->setCriterionType(PlacementMatchType::BROAD);
    $negativeCriterion
= new NegativeCampaignCriterion();
    $negativeCriterion
->setCampaignId($campaignId);
    $negativeCriterion
->setCriterion($negativePlacement);


    $operation
= new CampaignCriterionOperation();
    $operation
->setOperator(Operator::ADD);
    $operation
->setOperand($negativeCriterion);
    $operations
[] = $operation;


    $result
= $campaignCriterionService->mutate($operations);


   
// Print out some information about added campaign criteria.
   
foreach ($result->getValue() as $campaignCriterion) {
      printf
(
         
"Campaign targeting criterion with ID %d and type '%s' was added.\n",
          $campaignCriterion
->getCriterion()->getId(),
          $campaignCriterion
->getCriterion()->getType());
   
}
 
}


 
public static function main() {
   
// Generate a refreshable OAuth2 credential for authentication.
    $oAuth2Credential
= (new OAuth2TokenBuilder())
       
->fromFile()
       
->build();


   
// Construct an API session configured from a properties file and the OAuth2
   
// credentials above.
    $session
= (new AdWordsSessionBuilder())
       
->fromFile()
       
->withOAuth2Credential($oAuth2Credential)
       
->build();
   
self::runExample(
       
new AdWordsServices(), $session, intval(self::CAMPAIGN_ID));
 
}
}


AddCampaignTargetingCrtieria::main();

Peter Oliquino

unread,
Aug 7, 2017, 1:37:20 AM8/7/17
to AdWords API Forum
Hi Garik,

Thank you for your suggestion as it is also a correct example of how to exclude a placement criterion at the campaign level. 

Hi Maferyt,

As for your example, it appears that you are trying to exclude the placement at the ad group level. This said, you may refer to the AddKeywords example in PHP and just replace the Keyword Criterion with the Placement criterion when you set it as a negativeAdGroupCriterion as seen in the sample below :

// Create negative ad group criterion. 
$negativeAdGroupCriterion = new NegativeAdGroupCriterion(); 
$negativeAdGroupCriterion->setAdGroupId($adGroupId); 
$negativeAdGroupCriterion->setCriterion($placement);

Let me know if this helps.

Best regards,
Peter
AdWords API Team

Maferyt Letakol

unread,
Aug 7, 2017, 4:25:11 AM8/7/17
to AdWords API Forum
Hello guys,

Thanks for your answers, but I still can't make it work :/
I also tried with $operator = 'SET'; but it does nothing too.

I've changed :
$adGroupCriterion = new \NegativeAdGroupCriterion();
$adGroupCriterion->adGroupId = $criterion['adGroupId'];
$adGroupCriterion->criterion = new \Criterion($criterion['id']);
$operator = 'ADD';

to :
$negativePlacement      = new \Placement();
$negativePlacement->url = 'supersite.com';
$adGroupCriterion            = new \NegativeAdGroupCriterion();
$adGroupCriterion->adGroupId = $criterion['adGroupId'];
$adGroupCriterion->criterion = $negativePlacement;
$operator = 'ADD';

I really don't understand, because this code works well :
$adGroupCriterion             = new \BiddableAdGroupCriterion();
$adGroupCriterion->adGroupId  = $criterion['adGroupId'];
$adGroupCriterion->criterion  = new \Criterion($criterion['id']);
$adGroupCriterion->userStatus = 'PAUSED';
$operator = 'SET';



Anothers infos if it helps :
- using v201702
- application logs says no error
- AdWords interface "ChangeHistory" has nothing when trying to add Negative, but has logs when updating status to "PAUSED"



Thx

Peter Oliquino

unread,
Aug 7, 2017, 7:24:41 AM8/7/17
to AdWords API Forum
Hi Maferyt,

Could you provide me your PHP code and the SOAP request and response and reply via Reply privately to author so I can further investigate? Also, if you haven't enabled SOAP logging at your end, you may refer to this guide as to how.
Reply all
Reply to author
Forward
0 new messages