Search in sources :

Example 6 with Customer

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

the class AddExpandedTextAdWithUpgradedUrls method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group where the ad will be created.
 * @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, long adGroupId) throws RemoteException {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    // Create expanded text ad with a tracking template and custom parameters.
    ExpandedTextAd expandedTextAd = new ExpandedTextAd();
    expandedTextAd.setHeadlinePart1("Luxury Cruise to Mars");
    expandedTextAd.setHeadlinePart2("Visit the Red Planet in style.");
    expandedTextAd.setDescription("Low-gravity fun for everyone!");
    // Specify a tracking url for 3rd party tracking provider. You may
    // specify one at customer, campaign, ad group, ad, criterion or
    // feed item levels.
    expandedTextAd.setTrackingUrlTemplate("http://tracker.example.com/?season={_season}&promocode={_promocode}&u={lpurl}");
    // Since your tracking url has two custom parameters, provide their
    // values too. This can be provided at campaign, ad group, ad, criterion
    // or feed item levels.
    CustomParameter seasonParameter = new CustomParameter();
    seasonParameter.setKey("season");
    seasonParameter.setValue("christmas");
    CustomParameter promoCodeParameter = new CustomParameter();
    promoCodeParameter.setKey("promocode");
    promoCodeParameter.setValue("NYC123");
    CustomParameters trackingUrlParameters = new CustomParameters();
    trackingUrlParameters.setParameters(new CustomParameter[] { seasonParameter, promoCodeParameter });
    expandedTextAd.setUrlCustomParameters(trackingUrlParameters);
    // Specify a list of final urls. This field cannot be set if url field is
    // set. This may be specified at ad, criterion, and feed item levels.
    expandedTextAd.setFinalUrls(new String[] { "http://www.example.com/cruise/space/", "http://www.example.com/locations/mars/" });
    // Specify a list of final mobile urls. This field cannot be set if url field is
    // set or finalUrls is not set. This may be specified at ad, criterion, and feed
    // item levels.
    expandedTextAd.setFinalMobileUrls(new String[] { "http://mobile.example.com/cruise/space/", "http://mobile.example.com/locations/mars/" });
    // Create ad group ad.
    AdGroupAd textAdGroupAd = new AdGroupAd();
    textAdGroupAd.setAdGroupId(adGroupId);
    textAdGroupAd.setAd(expandedTextAd);
    // Optional: Set status.
    textAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create operation.
    AdGroupAdOperation textAdGroupAdOperation = new AdGroupAdOperation();
    textAdGroupAdOperation.setOperand(textAdGroupAd);
    textAdGroupAdOperation.setOperator(Operator.ADD);
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] { textAdGroupAdOperation };
    // Add ad.
    AdGroupAd adGroupAdResult = adGroupAdService.mutate(operations).getValue(0);
    // Display ad.
    System.out.printf("Ad with ID %d and tracking URL template '%s' was added.", adGroupAdResult.getAd().getId(), adGroupAdResult.getAd().getTrackingUrlTemplate());
}
Also used : AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) CustomParameter(com.google.api.ads.adwords.axis.v201809.cm.CustomParameter) CustomParameters(com.google.api.ads.adwords.axis.v201809.cm.CustomParameters)

Example 7 with Customer

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

the class GetAccountHierarchy 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 ServicedAccountService.
    ManagedCustomerServiceInterface managedCustomerService = adWordsServices.get(session, ManagedCustomerServiceInterface.class);
    // Create selector builder.
    int offset = 0;
    SelectorBuilder selectorBuilder = new SelectorBuilder().fields(ManagedCustomerField.CustomerId, ManagedCustomerField.Name).offset(offset).limit(PAGE_SIZE);
    // Get results.
    ManagedCustomerPage page;
    // Map from customerId to customer node.
    Map<Long, ManagedCustomerTreeNode> customerIdToCustomerNode = Maps.newHashMap();
    // Map from each parent customer ID to its set of linked child customer IDs.
    SortedSetMultimap<Long, Long> parentIdToChildIds = TreeMultimap.create();
    do {
        page = managedCustomerService.get(selectorBuilder.build());
        if (page.getEntries() != null) {
            // Create account tree nodes for each customer.
            for (ManagedCustomer customer : page.getEntries()) {
                ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
                node.account = customer;
                customerIdToCustomerNode.put(customer.getCustomerId(), node);
            }
            // Update the map of parent customer ID to child customer IDs.
            if (page.getLinks() != null) {
                for (ManagedCustomerLink link : page.getLinks()) {
                    parentIdToChildIds.put(link.getManagerCustomerId(), link.getClientCustomerId());
                }
            }
        }
        offset += PAGE_SIZE;
        selectorBuilder.increaseOffsetBy(PAGE_SIZE);
    } while (offset < page.getTotalNumEntries());
    // of its parentNode.
    for (Entry<Long, Long> parentIdEntry : parentIdToChildIds.entries()) {
        ManagedCustomerTreeNode parentNode = customerIdToCustomerNode.get(parentIdEntry.getKey());
        ManagedCustomerTreeNode childNode = customerIdToCustomerNode.get(parentIdEntry.getValue());
        childNode.parentNode = parentNode;
        parentNode.childAccounts.add(childNode);
    }
    // Find the root account node in the tree.
    ManagedCustomerTreeNode rootNode = customerIdToCustomerNode.values().stream().filter(node -> node.parentNode == null).findFirst().orElse(null);
    // Display serviced account graph.
    if (rootNode != null) {
        // Display account tree.
        System.out.println("CustomerId, Name");
        System.out.println(rootNode.toTreeString(0, new StringBuffer()));
    } else {
        System.out.println("No serviced accounts were found.");
    }
}
Also used : SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) ManagedCustomer(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomer) ManagedCustomerLink(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerLink) ManagedCustomerServiceInterface(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerServiceInterface) ManagedCustomerPage(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerPage)

Example 8 with Customer

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

the class ParallelReportDownload method main.

public static void main(String[] args) {
    ImmutableAdWordsSession session;
    try {
        // Generate a refreshable OAuth2 credential.
        Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS).fromFile().build().generateCredential();
        // Construct an ImmutableAdWordsSession to use as a prototype when creating a session for each
        // managed customer.
        session = new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).buildImmutable();
    } catch (ConfigurationLoadException cle) {
        System.err.printf("Failed to load configuration from the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, cle);
        return;
    } catch (ValidationException ve) {
        System.err.printf("Invalid configuration in the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, ve);
        return;
    } catch (OAuthException oe) {
        System.err.printf("Failed to create OAuth credentials. Check OAuth settings in the %s file. " + "Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, oe);
        return;
    }
    AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
    // Adjust these values as needed.
    int numberOfThreads = 5;
    int maxElapsedSecondsPerCustomer = 60 * 5;
    try {
        runExample(adWordsServices, session, numberOfThreads, maxElapsedSecondsPerCustomer);
    } catch (DetailedReportDownloadResponseException dre) {
        // A DetailedReportDownloadResponseException will be thrown if the HTTP status code in the
        // response indicates an error occurred and the response body contains XML with further
        // information, such as the fieldPath and trigger.
        System.err.printf("Report was not downloaded due to a %s with errorText '%s', trigger '%s' and " + "field path '%s'%n", dre.getClass().getSimpleName(), dre.getErrorText(), dre.getTrigger(), dre.getFieldPath());
    } catch (ApiException apiException) {
        // ApiException is the base class for most exceptions thrown by an API request. Instances
        // of this exception have a message and a collection of ApiErrors that indicate the
        // type and underlying cause of the exception. Every exception object in the adwords.axis
        // packages will return a meaningful value from toString
        // 
        // ApiException extends RemoteException, so this catch block must appear before the
        // catch block for RemoteException.
        System.err.println("Request failed due to ApiException. Underlying ApiErrors:");
        if (apiException.getErrors() != null) {
            int i = 0;
            for (ApiError apiError : apiException.getErrors()) {
                System.err.printf("  Error %d: %s%n", i++, apiError);
            }
        }
    } catch (RemoteException re) {
        System.err.printf("Request failed unexpectedly due to RemoteException: %s%n", re);
    } catch (ReportDownloadResponseException rde) {
        // A ReportDownloadResponseException will be thrown if the HTTP status code in the response
        // indicates an error occurred, but the response did not contain further details.
        System.err.printf("Report was not downloaded due to: %s%n", rde);
    } catch (ReportException re) {
        // A ReportException will be thrown if the download failed due to a transport layer exception.
        System.err.printf("Report was not downloaded due to transport layer exception: %s%n", re);
    } catch (IOException ioe) {
        // An IOException in this example indicates that the report's contents could not be read from
        // the response.
        System.err.printf("Report was not read due to an IOException: %s%n", ioe);
    } catch (InterruptedException ie) {
        System.err.printf("Thread was interrupted while waiting for reports to complete: %s%n", ie);
    } catch (ValidationException ve) {
        System.err.printf("Failed to create a session for a customer: %s%n", ve);
    }
}
Also used : ImmutableAdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession.ImmutableAdWordsSession) Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) DetailedReportDownloadResponseException(com.google.api.ads.adwords.lib.utils.DetailedReportDownloadResponseException) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) IOException(java.io.IOException) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) ReportException(com.google.api.ads.adwords.lib.utils.ReportException) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) DetailedReportDownloadResponseException(com.google.api.ads.adwords.lib.utils.DetailedReportDownloadResponseException) ReportDownloadResponseException(com.google.api.ads.adwords.lib.utils.ReportDownloadResponseException) RemoteException(java.rmi.RemoteException) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException)

Example 9 with Customer

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

the class AddCustomerNegativeCriteria 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 CustomerNegativeCriterionService.
    CustomerNegativeCriterionServiceInterface customerNegativeCriterionService = adWordsServices.get(session, CustomerNegativeCriterionServiceInterface.class);
    List<Criterion> criteria = new ArrayList<>();
    // Exclude tragedy & conflict content.
    ContentLabel tragedyContentLabel = new ContentLabel();
    tragedyContentLabel.setContentLabelType(ContentLabelType.TRAGEDY);
    criteria.add(tragedyContentLabel);
    // Exclude a specific placement.
    Placement placement = new Placement();
    placement.setUrl("http://www.example.com");
    criteria.add(placement);
    // Additional criteria types are available for this service. See the types listed
    // under Criterion here:
    // https://developers.google.com/adwords/api/docs/reference/latest/CustomerNegativeCriterionService.Criterion
    // Create operations to add each of the criteria above.
    List<CustomerNegativeCriterionOperation> operations = new ArrayList<>();
    for (Criterion criterion : criteria) {
        CustomerNegativeCriterion negativeCriterion = new CustomerNegativeCriterion();
        negativeCriterion.setCriterion(criterion);
        CustomerNegativeCriterionOperation operation = new CustomerNegativeCriterionOperation();
        operation.setOperator(Operator.ADD);
        operation.setOperand(negativeCriterion);
        operations.add(operation);
    }
    // Send the request to add the criteria.
    CustomerNegativeCriterionReturnValue result = customerNegativeCriterionService.mutate(operations.toArray(new CustomerNegativeCriterionOperation[operations.size()]));
    // Display the results.
    for (CustomerNegativeCriterion negativeCriterion : result.getValue()) {
        System.out.printf("Customer negative criterion with criterion ID %d and type '%s' was added.%n", negativeCriterion.getCriterion().getId(), negativeCriterion.getCriterion().getCriterionType());
    }
}
Also used : ContentLabel(com.google.api.ads.adwords.axis.v201809.cm.ContentLabel) CustomerNegativeCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterionOperation) CustomerNegativeCriterion(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterion) Criterion(com.google.api.ads.adwords.axis.v201809.cm.Criterion) Placement(com.google.api.ads.adwords.axis.v201809.cm.Placement) CustomerNegativeCriterion(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterion) CustomerNegativeCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterionReturnValue) ArrayList(java.util.ArrayList) CustomerNegativeCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterionServiceInterface)

Example 10 with Customer

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

the class CreateAdWordsSessionWithoutPropertiesFile method runExample.

public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException {
    CustomerServiceInterface customerService = adWordsServices.get(session, CustomerServiceInterface.class);
    // Sends a getCustomers request, which will return all customers to which the session's OAuth
    // credentials have direct access.
    System.out.println("You are logged in as a user with access to the following customers:");
    for (Customer customer : customerService.getCustomers()) {
        System.out.printf("  %s%n", customer.getCustomerId());
    }
}
Also used : CustomerServiceInterface(com.google.api.ads.adwords.axis.v201809.mcm.CustomerServiceInterface) Customer(com.google.api.ads.adwords.axis.v201809.mcm.Customer)

Aggregations

ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)3 ManagedCustomer (com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomer)3 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)3 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)3 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)3 RemoteException (java.rmi.RemoteException)3 ArrayList (java.util.ArrayList)3 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)2 AssetOperation (com.google.api.ads.adwords.axis.v201809.cm.AssetOperation)2 AssetServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AssetServiceInterface)2 ImageAsset (com.google.api.ads.adwords.axis.v201809.cm.ImageAsset)2 Customer (com.google.api.ads.adwords.axis.v201809.mcm.Customer)2 CustomerServiceInterface (com.google.api.ads.adwords.axis.v201809.mcm.CustomerServiceInterface)2 ManagedCustomerServiceInterface (com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerServiceInterface)2 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)1 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)1 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)1 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)1 Asset (com.google.api.ads.adwords.axis.v201809.cm.Asset)1 AssetReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AssetReturnValue)1