Search in sources :

Example 1 with ConversionTrackerServiceInterface

use of com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerServiceInterface in project googleads-java-lib by googleads.

the class AddAudience method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException {
    // Get the UserListService.
    AdwordsUserListServiceInterface userListService = adWordsServices.get(session, AdwordsUserListServiceInterface.class);
    // Get the ConversionTrackerService.
    ConversionTrackerServiceInterface conversionTrackerService = adWordsServices.get(session, ConversionTrackerServiceInterface.class);
    // Create conversion type (tag).
    UserListConversionType conversionType = new UserListConversionType();
    conversionType.setName("Mars cruise customers #" + System.currentTimeMillis());
    // Create remarketing user list.
    BasicUserList userList = new BasicUserList();
    userList.setName("Mars cruise customers #" + System.currentTimeMillis());
    userList.setDescription("A list of mars cruise customers in the last year");
    userList.setMembershipLifeSpan(365L);
    userList.setConversionTypes(new UserListConversionType[] { conversionType });
    // You can optionally provide these field(s).
    userList.setStatus(UserListMembershipStatus.OPEN);
    // Create operations.
    UserListOperation operation = new UserListOperation();
    operation.setOperand(userList);
    operation.setOperator(Operator.ADD);
    UserListOperation[] operations = new UserListOperation[] { operation };
    // Add user list.
    UserListReturnValue result = userListService.mutate(operations);
    // Display results.
    // Capture the ID(s) of the conversion.
    List<String> conversionIds = new ArrayList<>();
    for (UserList userListResult : result.getValue()) {
        if (userListResult instanceof BasicUserList) {
            BasicUserList remarketingUserList = (BasicUserList) userListResult;
            for (UserListConversionType userListConversionType : remarketingUserList.getConversionTypes()) {
                conversionIds.add(userListConversionType.getId().toString());
            }
        }
    }
    // Create predicate and selector.
    Selector selector = new SelectorBuilder().fields("Id", "GoogleGlobalSiteTag", "GoogleEventSnippet").in(AdwordsUserListField.Id, conversionIds.toArray(new String[0])).build();
    // Get all conversion trackers.
    Map<Long, AdWordsConversionTracker> conversionTrackers = new HashMap<Long, AdWordsConversionTracker>();
    ConversionTrackerPage page = conversionTrackerService.get(selector);
    if (page != null && page.getEntries() != null) {
        conversionTrackers = Arrays.stream(page.getEntries()).collect(Collectors.toMap(conversionTracker -> conversionTracker.getId(), conversionTracker -> (AdWordsConversionTracker) conversionTracker));
    }
    // Display user lists.
    for (UserList userListResult : result.getValue()) {
        System.out.printf("User list with name '%s' and ID %d was added.%n", userListResult.getName(), userListResult.getId());
        // Display user list associated conversion code snippets.
        if (userListResult instanceof BasicUserList) {
            BasicUserList remarketingUserList = (BasicUserList) userListResult;
            for (UserListConversionType userListConversionType : remarketingUserList.getConversionTypes()) {
                ConversionTracker conversionTracker = conversionTrackers.get(userListConversionType.getId());
                System.out.printf("Google global site tag:%n%s%n%n", conversionTracker.getGoogleGlobalSiteTag());
                System.out.printf("Google event snippet:%n%s%n%n", conversionTracker.getGoogleEventSnippet());
            }
        }
    }
}
Also used : ConversionTrackerPage(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerPage) AdwordsUserListServiceInterface(com.google.api.ads.adwords.axis.v201809.rm.AdwordsUserListServiceInterface) AdWordsConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.AdWordsConversionTracker) ConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.ConversionTracker) AdWordsConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.AdWordsConversionTracker) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BasicUserList(com.google.api.ads.adwords.axis.v201809.rm.BasicUserList) UserListOperation(com.google.api.ads.adwords.axis.v201809.rm.UserListOperation) UserListReturnValue(com.google.api.ads.adwords.axis.v201809.rm.UserListReturnValue) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) ConversionTrackerServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerServiceInterface) UserList(com.google.api.ads.adwords.axis.v201809.rm.UserList) BasicUserList(com.google.api.ads.adwords.axis.v201809.rm.BasicUserList) UserListConversionType(com.google.api.ads.adwords.axis.v201809.rm.UserListConversionType) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 2 with ConversionTrackerServiceInterface

use of com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerServiceInterface in project googleads-java-lib by googleads.

the class AddConversionTrackers method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException {
    // Get the ConversionTrackerService.
    ConversionTrackerServiceInterface service = adWordsServices.get(session, ConversionTrackerServiceInterface.class);
    List<ConversionTracker> conversionTrackers = new ArrayList<>();
    // Create an AdWords conversion tracker.
    AdWordsConversionTracker adWordsConversionTracker = new AdWordsConversionTracker();
    adWordsConversionTracker.setName("Earth to Mars Cruises Conversion # " + System.currentTimeMillis());
    adWordsConversionTracker.setCategory(ConversionTrackerCategory.DEFAULT);
    // You can optionally provide these field(s).
    adWordsConversionTracker.setStatus(ConversionTrackerStatus.ENABLED);
    adWordsConversionTracker.setViewthroughLookbackWindow(15);
    adWordsConversionTracker.setDefaultRevenueValue(1d);
    adWordsConversionTracker.setAlwaysUseDefaultRevenueValue(Boolean.TRUE);
    conversionTrackers.add(adWordsConversionTracker);
    // Create an upload conversion for offline conversion imports.
    UploadConversion uploadConversion = new UploadConversion();
    // Set an appropriate category. This field is optional, and will be set to
    // DEFAULT if not mentioned.
    uploadConversion.setCategory(ConversionTrackerCategory.LEAD);
    uploadConversion.setName("Upload Conversion #" + System.currentTimeMillis());
    uploadConversion.setViewthroughLookbackWindow(30);
    uploadConversion.setCtcLookbackWindow(90);
    // Optional: Set the default currency code to use for conversions
    // that do not specify a conversion currency. This must be an ISO 4217
    // 3-character currency code such as "EUR" or "USD".
    // If this field is not set on this UploadConversion, AdWords will use
    // the account's currency.
    uploadConversion.setDefaultRevenueCurrencyCode("EUR");
    // Optional: Set the default revenue value to use for conversions
    // that do not specify a conversion value. Note that this value
    // should NOT be in micros.
    uploadConversion.setDefaultRevenueValue(2.50);
    // Optional: To upload fractional conversion credits, mark the upload conversion
    // as externally attributed. See
    // https://developers.google.com/adwords/api/docs/guides/conversion-tracking#importing_externally_attributed_conversions
    // to learn more about importing externally attributed conversions.
    // uploadConversion.setIsExternallyAttributed(true);
    conversionTrackers.add(uploadConversion);
    // Create operations.
    List<ConversionTrackerOperation> operations = conversionTrackers.stream().map(conversionTracker -> {
        ConversionTrackerOperation operation = new ConversionTrackerOperation();
        operation.setOperator(Operator.ADD);
        operation.setOperand(conversionTracker);
        return operation;
    }).collect(Collectors.toList());
    // Add the conversions.
    ConversionTrackerReturnValue result = service.mutate(operations.toArray(new ConversionTrackerOperation[operations.size()]));
    // Display conversion.
    for (ConversionTracker conversionTracker : result.getValue()) {
        System.out.printf("Conversion with ID %d, name '%s', status '%s', " + "category '%s' was added.%n", conversionTracker.getId(), conversionTracker.getName(), conversionTracker.getStatus(), conversionTracker.getCategory());
        if (conversionTracker instanceof AdWordsConversionTracker) {
            System.out.printf("Google global site tag:%n%s%n%n", conversionTracker.getGoogleGlobalSiteTag());
            System.out.printf("Google event snippet:%n%s%n%n", conversionTracker.getGoogleEventSnippet());
        }
    }
}
Also used : ConversionTrackerCategory(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerCategory) ConversionTrackerReturnValue(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerReturnValue) ArrayList(java.util.ArrayList) ConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.ConversionTracker) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) Credential(com.google.api.client.auth.oauth2.Credential) ConversionTrackerOperation(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerOperation) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) ConversionTrackerServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerServiceInterface) ConversionTrackerStatus(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerStatus) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) Operator(com.google.api.ads.adwords.axis.v201809.cm.Operator) Collectors(java.util.stream.Collectors) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) AdWordsConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.AdWordsConversionTracker) List(java.util.List) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) UploadConversion(com.google.api.ads.adwords.axis.v201809.cm.UploadConversion) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) ConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.ConversionTracker) AdWordsConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.AdWordsConversionTracker) AdWordsConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.AdWordsConversionTracker) ConversionTrackerOperation(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerOperation) ArrayList(java.util.ArrayList) ConversionTrackerServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerServiceInterface) UploadConversion(com.google.api.ads.adwords.axis.v201809.cm.UploadConversion) ConversionTrackerReturnValue(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerReturnValue)

Aggregations

AdWordsConversionTracker (com.google.api.ads.adwords.axis.v201809.cm.AdWordsConversionTracker)2 ConversionTracker (com.google.api.ads.adwords.axis.v201809.cm.ConversionTracker)2 ConversionTrackerServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerServiceInterface)2 ArrayList (java.util.ArrayList)2 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)1 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)1 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)1 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)1 ConversionTrackerCategory (com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerCategory)1 ConversionTrackerOperation (com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerOperation)1 ConversionTrackerPage (com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerPage)1 ConversionTrackerReturnValue (com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerReturnValue)1 ConversionTrackerStatus (com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerStatus)1 Operator (com.google.api.ads.adwords.axis.v201809.cm.Operator)1 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)1 UploadConversion (com.google.api.ads.adwords.axis.v201809.cm.UploadConversion)1 AdwordsUserListServiceInterface (com.google.api.ads.adwords.axis.v201809.rm.AdwordsUserListServiceInterface)1 BasicUserList (com.google.api.ads.adwords.axis.v201809.rm.BasicUserList)1 UserList (com.google.api.ads.adwords.axis.v201809.rm.UserList)1 UserListConversionType (com.google.api.ads.adwords.axis.v201809.rm.UserListConversionType)1