I've been uploading telephone numbers to a remarketing list using the api.
But for some reason, the list says populating and the correct last update
shows but it is not increasing in number. I already have an email list that
works just fine. Is there something wrong with the code below? why would it
work for emails and not phones?
Here is the code I use:
require 'AddPhone.php';
// Set parameters
$EMAILS = $phoneNumbers;
$LIST_ID = $listID;
$CLIENT_CUSTOMER_ID = $clientID;
// Add e-mail addresses to the list
AddCrmBasedUserList::authenticateAndRun( $EMAILS, $LIST_ID,
$CLIENT_CUSTOMER_ID );
Here is the addPhone.php
require __DIR__ . '/vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201809\cm\Operator;
use Google\AdsApi\AdWords\v201809\rm\AddressInfo;
use Google\AdsApi\AdWords\v201809\rm\AdwordsUserListService;
use Google\AdsApi\AdWords\v201809\rm\CrmBasedUserList;
use Google\AdsApi\AdWords\v201809\rm\Member;
use Google\AdsApi\AdWords\v201809\rm\MutateMembersOperand;
use Google\AdsApi\AdWords\v201809\rm\MutateMembersOperandDataType;
use Google\AdsApi\AdWords\v201809\rm\MutateMembersOperation;
use Google\AdsApi\AdWords\v201809\rm\UserListOperation;
use Google\AdsApi\Common\OAuth2TokenBuilder;
class AddCrmBasedUserList {
public static function run(AdWordsServices $adWordsServices,
AdWordsSession $session, array $emails, int $listId, bool $quiet =
True) {
$userListService =
$adWordsServices->get($session, AdwordsUserListService::class);
// Create operation to add members to the user list based on email
// addresses.
$mutateMembersOperations = [];
$mutateMembersOperation = new MutateMembersOperation();
$operand = new MutateMembersOperand();
$operand->setUserListId($listId);
$members = [];
// Hash normalized email addresses based on SHA-256 hashing algorithm.
foreach ($emails as $email) {
$memberByEmail = new Member();
$memberByEmail->setHashedPhoneNumber (self::normalizeAndHash($email));
$members[] = $memberByEmail;
}
// Add members to the operand and add the operation to the list.
$operand->setMembersList($members);
$mutateMembersOperation->setOperand($operand);
$mutateMembersOperation->setOperator(Operator::ADD);
$mutateMembersOperations[] = $mutateMembersOperation;
// Add members to the user list based on email addresses.
$result = $userListService->mutateMembers($mutateMembersOperations);
// Print out some information about the added user list.
// Reminder: it may take several hours for the list to be populated with
// members.
foreach ($result->getUserLists() as $userList) {
$session->getSoapLogger()->info(sprintf(
"%d Phone Numbers's were uploaded to user list with name '%s' and
ID"
. " %d and are scheduled for review.\n",
count($emails),
$userList->getName(),
$userList->getId()
));
}
}
private static function normalizeAndHash($value) {
return hash('sha256', strtolower(trim($value)));
}
public static function authenticateAndRun(array $emails, int $listId,
string $clientCustomerId) {
// 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)
->withClientCustomerId($clientCustomerId)
->build();
self::run(new AdWordsServices(), $session, $emails, $listId);
}
}
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"AdWords API and Google Ads API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/1109f7fe-6363-48da-8fa9-0df70e256f65%40googlegroups.com.