Problem - While Creating the Customer Match List i am finding that Customer 
List Is getting created but uploads is showing 0 and its not about time 
because i've waited 2 - 3 days.*(Attaching Image)*

*[image: Screenshot from 2024-05-13 04-26-48.png]*

*CSV*
Email,Phone,First Name,Last Name,Country,Zip\n
x...@gmail.com,81-12-3456-7891,john,smith,us,94016\n
a...@gmail.com,81-12-3456-7891,joanna,smith,cn,101300\n

*RESPONSE, REQUEST*
Number of records read from CSV: 2
Processing record: x...@gmail.com
Processing record: a...@gmail.com
2024-05-13 04:23:05.570  INFO 8206 --- [ault-executor-0] 
c.g.ads.googleads.lib.request.summary    : SUCCESS REQUEST SUMMARY. Method: 
google.ads.googleads.v16.services.UserListService/MutateUserLists, 
Endpoint: googleads.googleapis.com:443, CustomerID: 5084430664, RequestID: 
Jbr3MMpM8Zu_K5NKBQnOow, ResponseCode: OK, Fault: null.
Created Customer Match user list with resource name: 
customers/5084430664/userLists/8658557609.

2024-05-13 04:23:22.230  INFO 8206 --- [ault-executor-2] 
c.g.ads.googleads.lib.request.summary    : SUCCESS REQUEST SUMMARY. Method: 
google.ads.googleads.v16.services.GoogleAdsService/Search, Endpoint: 
googleads.googleapis.com:443, CustomerID: 5084430664, RequestID: 
QUQub7mmT6J2HEMrIQ5BwQ, ResponseCode: OK, Fault: null.
User list 'customers/5084430664/userLists/8658557609' has an estimated 0 
users for Display and 0 users for Search.


*CODE(JAVA)*

public void addCustomerDataFromCSV(GoogleAdsClient googleAdsClient, long 
customerId, 
String filePath) {
try {
List<CustomerRecord> customerRecords = CsvParser.parseCsv(filePath);

System.out.println("Number of records read from CSV: " + 
customerRecords.size());

// Accumulate user data for all records
List<UserData> userDataList = new ArrayList<>();

for (CustomerRecord record : customerRecords) {
System.out.println("Processing record: " + record.getEmail());
// Create user data for each record
UserData userData = createUserDataForRecord(record);
userDataList.add(userData);
}

// Create a single user list containing data from all records
createCustomerMatchUserList(googleAdsClient, customerId, userDataList);
} catch (IOException e) {
e.printStackTrace();
}
}

private UserData createUserDataForRecord(CustomerRecord record) {
// Inside your class or method
MessageDigest sha256Digest;
UserData.Builder userDataBuilder = UserData.newBuilder();

try {
sha256Digest = MessageDigest.getInstance("SHA-256");

// Add email as a UserIdentifier
UserIdentifier hashedEmailIdentifier = UserIdentifier.newBuilder()
.setHashedEmail(normalizeAndHashEmailAddress(sha256Digest, 
record.getEmail()))
.build();
userDataBuilder.addUserIdentifiers(hashedEmailIdentifier);

// Add phone number as a UserIdentifier
if (record.getPhone() != null && !record.getPhone().isEmpty()) {
UserIdentifier hashedPhoneNumberIdentifier = UserIdentifier.newBuilder()
.setHashedPhoneNumber(normalizeAndHash(sha256Digest, record.getPhone(), true
))
.build();
userDataBuilder.addUserIdentifiers(hashedPhoneNumberIdentifier);
}

// Add other fields such as First Name, Last Name, Country, and Zip
OfflineUserAddressInfo addressInfo = OfflineUserAddressInfo.newBuilder()
.setHashedFirstName(normalizeAndHash(sha256Digest, record.getFirstName(), 
true))
.setHashedLastName(normalizeAndHash(sha256Digest, record.getLastName(), true
))
.setCountryCode(record.getCountryCode())
.setPostalCode(record.getPostalCode())
.build();
UserIdentifier addressIdentifier = UserIdentifier.newBuilder()
.setAddressInfo(addressInfo)
.build();
userDataBuilder.addUserIdentifiers(addressIdentifier);


} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
// Handle the exception
e.printStackTrace();
}

return userDataBuilder.build();
}


private void createCustomerMatchUserList(GoogleAdsClient googleAdsClient, long 
customerId, List<UserData> userDataList) {
try (UserListServiceClient userListServiceClient =
googleAdsClient.getLatestVersion().createUserListServiceClient()) {
// Create the user list
UserList userList = UserList.newBuilder()
.setName("Customer Match list") // Provide a name for the list
.setDescription("A list of customers that originated from email addresses")
.setMembershipLifeSpan(30) // Set the membership lifespan as required
.setCrmBasedUserList(
CrmBasedUserListInfo.newBuilder()
.setUploadKeyType(CustomerMatchUploadKeyTypeEnum.CustomerMatchUploadKeyType.
CONTACT_INFO))

.build();

// Create the operation to add users
List<OfflineUserDataJobOperation> operations = new ArrayList<>();
for (UserData userData : userDataList) {
operations.add(OfflineUserDataJobOperation.newBuilder
().setCreate(userData).build());
}

// Add the user list with all users
MutateUserListsResponse response = userListServiceClient.mutateUserLists(
Long.toString(customerId), ImmutableList.of(UserListOperation.newBuilder
().setCreate(userList).build()));

System.out.printf("Created Customer Match user list with resource name: 
%s.%n",
response.getResults(0).getResourceName());

// Fetch the user list details
String userListResourceName = response.getResults(0).getResourceName();
fetchAndPrintUserListDetails(googleAdsClient, customerId, 
userListResourceName);

} catch (Exception e) {
e.printStackTrace();
}
}

private void fetchAndPrintUserListDetails(GoogleAdsClient googleAdsClient, long 
customerId, String userListResourceName) {
try (GoogleAdsServiceClient googleAdsServiceClient =
googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
// Create a query to retrieve the user list details
String query = String.format(
"SELECT user_list.size_for_display, user_list.size_for_search "
+ "FROM user_list "
+ "WHERE user_list.resource_name = '%s'",
userListResourceName);

// Issue the search request to fetch user list details
SearchGoogleAdsRequest request = SearchGoogleAdsRequest.newBuilder()
.setCustomerId(Long.toString(customerId))
.setQuery(query)
.build();

// Execute the search request
GoogleAdsServiceClient.SearchPagedResponse response = 
googleAdsServiceClient.search(request);

// Print the user list details
for (GoogleAdsRow googleAdsRow : response.iterateAll()) {
UserList userList = googleAdsRow.getUserList();
System.out.printf("User list '%s' has an estimated %d users for Display and 
%d users for Search.%n",
userList.getResourceName(), userList.getSizeForDisplay(), 
userList.getSizeForSearch());
}

} catch (Exception e) {
e.printStackTrace();
}
}


private String normalizeAndHash(MessageDigest digest, String s, boolean 
trimIntermediateSpaces)
throws UnsupportedEncodingException {
String normalized = s.toLowerCase();
if (trimIntermediateSpaces) {
normalized = normalized.replaceAll("\\s+", "");
} else {
normalized = normalized.trim();
}
byte[] hash = digest.digest(normalized.getBytes("UTF-8"));
StringBuilder result = new StringBuilder();
for (byte b : hash) {
result.append(String.format("%02x", b));
}
return result.toString();
}

private String normalizeAndHashEmailAddress(MessageDigest digest, String 
emailAddress)
throws UnsupportedEncodingException {
String normalizedEmail = emailAddress.toLowerCase();
String[] emailParts = normalizedEmail.split("@");
if (emailParts.length > 1 && emailParts[1].matches("^(gmail|googlemail)\\
.com\\s*")) {
// Removes any '.' characters from the portion of the email address before 
the domain if the
// domain is gmail.com or googlemail.com.
emailParts[0] = emailParts[0].replaceAll("\\.", "");
normalizedEmail = String.format("%s@%s", emailParts[0], emailParts[1]);
}
return normalizeAndHash(digest, normalizedEmail,true);
}

CustomerRecord.java

public class CustomerRecord {
private String email;
private String phone;
private String firstName;
private String lastName;
private String countryCode;
private String postalCode;

public CustomerRecord(String email, String phone, String firstName, String 
lastName, String countryCode, String postalCode) {
this.email = email;
this.phone = phone;
this.firstName = firstName;
this.lastName = lastName;
this.countryCode = countryCode;
this.postalCode = postalCode;
}

public String getEmail() {
return email;
}

public String getPhone() {
return phone;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getCountryCode() {
return countryCode;
}

public String getPostalCode() {
return postalCode;
}

}

CsvParser.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CsvParser {

public static List<CustomerRecord> parseCsv(String filePath) throws IOException 
{
List<CustomerRecord> customerRecords = new ArrayList<>(); // Update the 
type here
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
// Skip the header if present
br.readLine();
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
if (data.length >= 2) {
String email = data[0].trim();
String phone = data[1].trim();
String firstName = (data.length >= 3) ? data[2].trim() : "";
String lastName = (data.length >= 4) ? data[3].trim() : "";
String countryCode = (data.length >= 5) ? data[4].trim() : "";
String postalCode = (data.length >= 6) ? data[5].trim() : "";
customerRecords.add(new CustomerRecord(email, phone, firstName, lastName, 
countryCode, postalCode));
}
}
}
return customerRecords;
}
}

-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
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 adwords-api@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscr...@googlegroups.com
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 
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to adwords-api+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/adwords-api/0d0e7f61-dc0b-42d3-b1a8-f53dccfc3cc9n%40googlegroups.com.
  • Fa... Abhishek Singh
    • ... 'Google Ads API Forum Advisor' via Google Ads API and AdWords API Forum

Reply via email to