How to Update Keyword Status using Google Adwords API ?

You can pause any active keyword or vice versa using Google Adwords API PHP Client Library. Using following method, you can Enable a 'PAUSED' keyword and Paused a 'ACTIVE' keyword. You can delete the keyword as well.

Three states of keywords are:
ACTIVE : Default state of a criterion (e.g. not paused).
DELETED : Criterion is deleted.
PAUSED : Criterion is paused. Also used to pause a criterion.

Following function can be used to update the keyword status from 'PAUSED' to 'ACTIVE' and 'ACTIVE' to 'PAUSED'.

The key thing is to update the userStatus field.

NOTE: During an ADD and SET operation: It may not be set to DELETED.
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $adGroupId the id of the ad group that contains the keyword
 * @param string $criterionId the id of the keyword
 * @param string $status the status of the keyword
 */
function UpdateKeyword($user, $adGroupId, $criterionId, $status){
 
 // Get the service, which loads the required classes.
  $adGroupCriterionService =
      $user->GetService('AdGroupCriterionService', ADWORDS_VERSION); 
 
   // Create criterion using an existing ID. Use the base class Criterion
  // instead of Keyword to avoid having to set keyword-specific fields.
  $criterion = new Criterion();
  $criterion->id = $criterionId;

  // Create ad group criterion.
  $adGroupCriterion = new BiddableAdGroupCriterion();
  $adGroupCriterion->adGroupId = $adGroupId;
  $adGroupCriterion->criterion = new Criterion($criterionId);
  
   $adGroupCriterion->userStatus = $status;  
 
   // Create operation.
  $operation = new AdGroupCriterionOperation();
  $operation->operand = $adGroupCriterion;
  $operation->operator = 'SET';

  $operations = array($operation);

  // Make the mutate request.
  $result = $adGroupCriterionService->mutate($operations);

  // Display result.
  $adGroupCriterion = $result->value[0];
  return $adGroupCriterion;

}


If you want to delete the keyword use following function. You must use 'REMOVE' operator to delete keyword.
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $adGroupId the id of the ad group that the keyword is in
 * @param string $criterionId the id of the keyword to delete
 */
function DeleteKeyword($user, $adGroupId, $criterionId) {
  // Get the service, which loads the required classes.
  $adGroupCriterionService =
      $user->GetService('AdGroupCriterionService', ADWORDS_VERSION);

  // Create criterion using an existing ID. Use the base class Criterion
  // instead of Keyword to avoid having to set keyword-specific fields.
  $criterion = new Criterion();
  $criterion->id = $criterionId;

  // Create ad group criterion.
  $adGroupCriterion = new AdGroupCriterion();
  $adGroupCriterion->adGroupId = $adGroupId;
  $adGroupCriterion->criterion = new Criterion($criterionId);

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

  $operations = array($operation);

  // Make the mutate request.
  $result = $adGroupCriterionService->mutate($operations);

  // Display result.
  $adGroupCriterion = $result->value[0];
  printf("Keyword with ID '%s' was deleted.\n",
      $adGroupCriterion->criterion->id);
}

Reference: Google Adwords API PHP

Comments

Popular Posts