Thursday, September 26, 2013

How to update text ad status using Google Adwords Api and PHP client library?

There are three status of text Ad:
1. ENABLED
2. DISABLED
3. PAUSED

If you delete AdGroupAd using 'REMOVE' operator then you can't change it anymore. If you try to update it back to 'ENABLED/PAUSED' you will get AdGroupAdError -> CANNOT_OPERATE_ON_DELETED_ADGROUPAD (Deleted ads may not be modified).

If you want to delete the AdGroupAd use following function and it uses 'REMOVE' Operator. This means that such Ad can't be changed anymore. It will never be fully deleted, but they will always exist in your account with the status 'DISABLED'.

For example
function DeleteAd($user, $adGroupId, $adId) {
  // Get the service, which loads the required classes.
  $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION);

  // Create base class ad to avoid setting type specific fields.
  $ad = new Ad();
  $ad->id = $adId;

  // Create ad group ad.
  $adGroupAd = new AdGroupAd();
  $adGroupAd->adGroupId = $adGroupId;
  $adGroupAd->ad = $ad;

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

  $operations = array($operation);

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

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

If you want to modify a text ad that was created, then you must use 'SET' Operator. You can update the status of text ad using following function together with 'SET' Operator.

Best Practice will be: Instead of setting status to 'DISABLED', you could set it to 'PAUSED'. And if adding of new AdGroupAd doesn't succeed, you could always revert old AdGroupAd from 'PAUSED' to 'ACTIVE'.

For example
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $adGroupId the id of the ad group containing the ad
 * @param string $adId the ID of the ad
 * @param string $status the status(ACTIVE, DELETED, PAUSED) of the ad
 */
function UpdateAd($user, $adGroupId, $adId, $status) {
  // Get the service, which loads the required classes.
  $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION);

  // Create ad using an existing ID. Use the base class Ad instead of TextAd to
  // avoid having to set ad-specific fields.
  $ad = new Ad();
  $ad->id = $adId;

  // Create ad group ad.
  $adGroupAd = new AdGroupAd();
  $adGroupAd->adGroupId = $adGroupId;
  $adGroupAd->ad = $ad;

 if($status == "DELETED")
  {
  $adGroupAd->status = 'DISABLED'; 
  }
  
  if($status == "ACTIVE")
  {
  $adGroupAd->status = 'ENABLED'; 
  }

  if($status == "PAUSED")
  {
    $adGroupAd->status = 'PAUSED';
  }
  // Create operation.
  $operation = new AdGroupAdOperation();
  $operation->operand = $adGroupAd;   
  $operation->operator = 'SET';   
  $operations = array($operation);

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

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

  printf("Ad of type '%s' with ID '%s' has updated status '%s'.\n",
      $adGroupAd->ad->AdType, $adGroupAd->ad->id, $adGroupAd->status);
}

Source: google-api-adwords-php library v201306

1 comment: