How do I synchronize deleted keywords?

88 views
Skip to first unread message

starrychloe S.

unread,
Jan 7, 2016, 8:00:03 PM1/7/16
to AdWords API Forum
How do I synchronize and download keywords from Google which have been deleted? I have a function which specifically requests Status = REMOVED, and yet it is not returning deleted keywords. The keyword in question also doesn't appear in the web interface in the ad group any more. I considered collecting all the ACTIVE keyword ids, and deleting the rest that are still in my database, but that doesn't work for millions of keywords ("where google_id not in (...)"). 



  public function getKeywords($callback = null) {
    $user
= $this->getUser();
    $adGroupCriterionService
= $user->GetService('AdGroupCriterionService', ADWORDS_VERSION);


    $awqlArray
= [
     
"SELECT Id, KeywordText, KeywordMatchType, AdGroupId, Status, CpcBid where CriteriaType = KEYWORD and CampaignId = %s LIMIT %d,%d",
     
"SELECT Id, KeywordText, KeywordMatchType, AdGroupId, Status, CpcBid where CriteriaType = KEYWORD and CampaignId = %s and Status = REMOVED LIMIT %d,%d"
   
];
 
    $adGroupCriterions
= array();
    $campaigns
= Campaign::find()->all(); # must query by campaign because there are > 100,000 ads. Prevent error: SelectorError.START_INDEX_IS_TOO_HIGH
   
foreach ($campaigns as $campaign) {
      echo
"Getting keywords for campaign $campaign->name\n";
     
foreach ($awqlArray as $awql) {  
        $offset
= 0;
       
do {
         
# must use %s because Google ids are long ints and %d/%u would truncate/corrupt the values
          $pageQuery
= sprintf($awql, $campaign->google_id, $offset, AdWordsConstants::RECOMMENDED_PAGE_SIZE*2);
         
// Make the get request.
          $page
= $adGroupCriterionService->query($pageQuery);
         
// Collect results.
         
if (isset($page->entries)) {
           
foreach ($page->entries as $adGroupCriterion) {
             
// printf("Keyword with text '%s', match type '%s', and ID '%s' was found.\n",
               
// $adGroupCriterion->criterion->text,
               
// $adGroupCriterion->criterion->matchType,
               
// $adGroupCriterion->criterion->id);
             
if ($callback) { // don't save the collection or we will run out of memory
                $callback
($adGroupCriterion);
             
} else {
                $adGroupCriterions
[] = $adGroupCriterion;
             
}
           
}
         
}
         
// Advance the paging offset.
          $offset
+= AdWordsConstants::RECOMMENDED_PAGE_SIZE*2;
       
} while ($page->totalNumEntries > $offset);
     
}
   
}
 
   
return $adGroupCriterions;
 
}



Yin Niu

unread,
Jan 8, 2016, 9:39:59 AM1/8/16
to AdWords API Forum
Hello, 

It is only possible to retrieve a deleted criterion data if it had any impressions before it's deleted. If it had zero impressions, once deleted it is completely removed from the system.

Thanks,
Yin, AdWords API Team

starrychloe S.

unread,
Jan 10, 2016, 5:16:20 PM1/10/16
to AdWords API Forum
OK I did this.

    $googleIds = array(); // store the found Google ids to set the rest to removed.  
    $callback = function($adGroupCriterion) use ($adGroupLookup, &$googleIds) {
      ...
      $googleIds[] = $adGroupCriterion->criterion->id; 
    };
    $gaw = new GoogleAdWords();
    $result = $gaw->getKeywords($callback);
    # Google is not returning deleted keywords in PROD! Not even when selecting Status = REMOVED! Must set all remaining keywords to REMOVED
    $deleteGoogleIds = AdGroupKeyword::find()->select('google_id')->column();
    $deleteGoogleIds = array_diff($deleteGoogleIds, $googleIds); // all that's left are the keywords that Google did not return - delete them!
    echo "Setting ".count($deleteGoogleIds)." keywords to status DELETED.\n";
    $deleteGoogleIds = array_chunk($deleteGoogleIds, 100); // chunk it into 100 items so the DB doesn't barf
    foreach ($deleteGoogleIds as $batch) {
      Yii::$app->db->createCommand()
        ->update('ad_group_keyword', ['status' => Constants::DELETED], ['google_id' => $batch])
        ->execute();
    }




Reply all
Reply to author
Forward
0 new messages