use of com.google.api.ads.adwords.axis.v201809.rm.OfflineDataUploadServiceInterface in project googleads-java-lib by googleads.
the class UploadOfflineData method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param conversionName the name of the conversion tracker.
* @param externalUploadId the external upload ID.
* @param emailAddresses the list of email addresses. Must contain exactly two entries for this
* example.
* @param offlineDataUploadType the type of store sales upload metadata to upload.
* @param advertiserUploadTime the date and time of the advertiser upload.
* @param bridgeMapVersionId the bridge map version ID.
* @param partnerId the partner ID.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
* @throws UnsupportedEncodingException if encoding the offline data values failed.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, String conversionName, long externalUploadId, List<String> emailAddresses, OfflineDataUploadType offlineDataUploadType, String advertiserUploadTime, String bridgeMapVersionId, Integer partnerId) throws RemoteException, UnsupportedEncodingException {
// This example requires exactly 2 email addresses.
if (emailAddresses.size() != 2) {
throw new IllegalArgumentException(String.format("%d email addresses specified. Please specify exactly 2 email addresses.", emailAddresses.size()));
}
// Get the OfflineDataUploadService.
OfflineDataUploadServiceInterface offlineDataUploadService = adWordsServices.get(session, OfflineDataUploadServiceInterface.class);
List<OfflineData> offlineDataList = new ArrayList<>();
// Create the first offline data for upload.
// This transaction occurred 7 days ago with amount of 200 USD.
DateTime transactionTime1 = DateTime.now().minusDays(7);
long transactionAmount1 = 200_000_000L;
List<UserIdentifier> userIdentifiers1 = Arrays.asList(createUserIdentifier(OfflineDataUploadUserIdentifierType.HASHED_EMAIL, emailAddresses.get(0)), createUserIdentifier(OfflineDataUploadUserIdentifierType.STATE, "New York"));
offlineDataList.add(createOfflineData(transactionTime1, transactionAmount1, "USD", conversionName, userIdentifiers1));
// Create the second offline data for upload.
// This transaction occurred 14 days ago with amount of 450 EUR.
DateTime transactionTime2 = DateTime.now().minusDays(14);
long transactionAmount2 = 450_000_000L;
List<UserIdentifier> userIdentifiers2 = Arrays.asList(createUserIdentifier(OfflineDataUploadUserIdentifierType.HASHED_EMAIL, emailAddresses.get(1)), createUserIdentifier(OfflineDataUploadUserIdentifierType.STATE, "California"));
offlineDataList.add(createOfflineData(transactionTime2, transactionAmount2, "EUR", conversionName, userIdentifiers2));
// Create offline data upload object.
OfflineDataUpload offlineDataUpload = new OfflineDataUpload();
offlineDataUpload.setExternalUploadId(externalUploadId);
offlineDataUpload.setOfflineDataList(offlineDataList.toArray(new OfflineData[offlineDataList.size()]));
offlineDataUpload.setUploadType(offlineDataUploadType);
// Set the type and metadata of this upload.
StoreSalesUploadCommonMetadata storeSalesUploadMetadata;
if (OfflineDataUploadType.STORE_SALES_UPLOAD_FIRST_PARTY.equals(offlineDataUploadType)) {
storeSalesUploadMetadata = new FirstPartyUploadMetadata();
} else {
ThirdPartyUploadMetadata thirdPartyUploadMetadata = new ThirdPartyUploadMetadata();
thirdPartyUploadMetadata.setAdvertiserUploadTime(advertiserUploadTime);
thirdPartyUploadMetadata.setValidTransactionRate(1.0);
thirdPartyUploadMetadata.setPartnerMatchRate(1.0);
thirdPartyUploadMetadata.setPartnerUploadRate(1.0);
thirdPartyUploadMetadata.setBridgeMapVersionId(bridgeMapVersionId);
thirdPartyUploadMetadata.setPartnerId(partnerId);
storeSalesUploadMetadata = thirdPartyUploadMetadata;
}
storeSalesUploadMetadata.setLoyaltyRate(1.0);
storeSalesUploadMetadata.setTransactionUploadRate(1.0);
UploadMetadata uploadMetadata = new UploadMetadata();
uploadMetadata.setStoreSalesUploadCommonMetadata(storeSalesUploadMetadata);
offlineDataUpload.setUploadMetadata(uploadMetadata);
// Create an offline data upload operation.
List<OfflineDataUploadOperation> operations = new ArrayList<>();
OfflineDataUploadOperation offlineDataUploadOperation = new OfflineDataUploadOperation();
offlineDataUploadOperation.setOperator(Operator.ADD);
offlineDataUploadOperation.setOperand(offlineDataUpload);
operations.add(offlineDataUploadOperation);
// Upload offline data on the server and print some information.
OfflineDataUploadReturnValue returnValue = offlineDataUploadService.mutate(operations.toArray(new OfflineDataUploadOperation[0]));
offlineDataUpload = returnValue.getValue(0);
System.out.printf("Uploaded offline data with external upload ID %d, and upload status %s.%n", offlineDataUpload.getExternalUploadId(), offlineDataUpload.getUploadStatus());
// Print any partial failure errors from the response.
if (returnValue.getPartialFailureErrors() != null) {
for (ApiError apiError : returnValue.getPartialFailureErrors()) {
// Get the index of the failed operation from the error's field path elements.
Integer operationIndex = getFieldPathElementIndex(apiError, "operations");
if (operationIndex != null) {
OfflineDataUpload failedOfflineDataUpload = operations.get(operationIndex).getOperand();
// Get the index of the entry in the offline data list from the error's field path
// elements.
Integer offlineDataListIndex = getFieldPathElementIndex(apiError, "offlineDataList");
System.out.printf("Offline data list entry %d in operation %d with external upload ID %d and type " + "'%s' has triggered a failure for the following reason: '%s'.%n", offlineDataListIndex, operationIndex, failedOfflineDataUpload.getExternalUploadId(), failedOfflineDataUpload.getUploadType(), apiError.getErrorString());
} else {
System.out.printf("A failure has occurred for the following reason: %s%n", apiError.getErrorString());
}
}
}
}
Aggregations