Search in sources :

Example 16 with Ad

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

the class GetAdGroups method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param campaignId the ID of the campaign to use to find ad groups.
 * @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 campaignId) throws RemoteException {
    // Get the AdGroupService.
    AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
    int offset = 0;
    boolean morePages = true;
    // Create selector.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(AdGroupField.Id, AdGroupField.Name).orderAscBy(AdGroupField.Name).offset(offset).limit(PAGE_SIZE).equals(AdGroupField.CampaignId, campaignId.toString()).build();
    while (morePages) {
        // Get all ad groups.
        AdGroupPage page = adGroupService.get(selector);
        // Display ad groups.
        if (page.getEntries() != null) {
            for (AdGroup adGroup : page.getEntries()) {
                System.out.printf("Ad group with name '%s' and ID %d was found.%n", adGroup.getName(), adGroup.getId());
            }
        } else {
            System.out.println("No ad groups were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
        morePages = offset < page.getTotalNumEntries();
    }
}
Also used : AdGroupServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) AdGroupPage(com.google.api.ads.adwords.axis.v201809.cm.AdGroupPage) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 17 with Ad

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

the class HandlePartialFailures method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group.
 * @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 {
    // Enable partial failure.
    session.setPartialFailure(true);
    // Get the AdGroupCriterionService.
    AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
    List<AdGroupCriterionOperation> operations = new ArrayList<>();
    // Create keywords.
    String[] keywords = new String[] { "mars cruise", "inv@lid cruise", "venus cruise", "b(a)d keyword cruise" };
    for (String keywordText : keywords) {
        // Create keyword
        Keyword keyword = new Keyword();
        keyword.setText(keywordText);
        keyword.setMatchType(KeywordMatchType.BROAD);
        // Create biddable ad group criterion.
        BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
        keywordBiddableAdGroupCriterion.setAdGroupId(adGroupId);
        keywordBiddableAdGroupCriterion.setCriterion(keyword);
        // Create operation.
        AdGroupCriterionOperation keywordAdGroupCriterionOperation = new AdGroupCriterionOperation();
        keywordAdGroupCriterionOperation.setOperand(keywordBiddableAdGroupCriterion);
        keywordAdGroupCriterionOperation.setOperator(Operator.ADD);
        operations.add(keywordAdGroupCriterionOperation);
    }
    // Add ad group criteria.
    AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations.toArray(new AdGroupCriterionOperation[] {}));
    // Display results.
    Arrays.stream(result.getValue()).filter(adGroupCriterionResult -> adGroupCriterionResult.getCriterion() != null).forEach(adGroupCriterionResult -> System.out.printf("Ad group criterion with ad group ID %d, and criterion ID %d, " + "and keyword '%s' was added.%n", adGroupCriterionResult.getAdGroupId(), adGroupCriterionResult.getCriterion().getId(), ((Keyword) adGroupCriterionResult.getCriterion()).getText()));
    for (ApiError apiError : result.getPartialFailureErrors()) {
        // Get the index of the failed operation from the error's field path elements.
        FieldPathElement[] fieldPathElements = apiError.getFieldPathElements();
        FieldPathElement firstFieldPathElement = null;
        if (fieldPathElements != null && fieldPathElements.length > 0) {
            firstFieldPathElement = fieldPathElements[0];
        }
        if (firstFieldPathElement != null && "operations".equals(firstFieldPathElement.getField()) && firstFieldPathElement.getIndex() != null) {
            int operationIndex = firstFieldPathElement.getIndex();
            AdGroupCriterion adGroupCriterion = operations.get(operationIndex).getOperand();
            System.out.printf("Ad group criterion with ad group ID %d and keyword '%s' " + "triggered a failure for the following reason: %s.%n", adGroupCriterion.getAdGroupId(), ((Keyword) adGroupCriterion.getCriterion()).getText(), apiError.getErrorString());
        } else {
            System.out.printf("A failure has occurred for the following reason: %s%n", apiError.getErrorString());
        }
    }
}
Also used : KeywordMatchType(com.google.api.ads.adwords.axis.v201809.cm.KeywordMatchType) Arrays(java.util.Arrays) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) Parameter(com.beust.jcommander.Parameter) AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) ArrayList(java.util.ArrayList) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) FieldPathElement(com.google.api.ads.adwords.axis.v201809.cm.FieldPathElement) ArgumentNames(com.google.api.ads.adwords.lib.utils.examples.ArgumentNames) Credential(com.google.api.client.auth.oauth2.Credential) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdGroupCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionReturnValue) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) Operator(com.google.api.ads.adwords.axis.v201809.cm.Operator) CodeSampleParams(com.google.api.ads.common.lib.utils.examples.CodeSampleParams) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) List(java.util.List) AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) ArrayList(java.util.ArrayList) FieldPathElement(com.google.api.ads.adwords.axis.v201809.cm.FieldPathElement) AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) AdGroupCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionReturnValue) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError)

Example 18 with Ad

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

the class AddResponsiveSearchAd 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);
    List<AdGroupAdOperation> operations = new ArrayList<>();
    ResponsiveSearchAd responsiveSearchAd = new ResponsiveSearchAd();
    List<AssetLink> headlines = new ArrayList<>();
    AssetLink pinnedHeadline = createAssetLinkForText("Cruise to Mars #" + System.currentTimeMillis());
    // Set a pinning to always choose this asset for HEADLINE_1. Pinning is optional; if no pinning
    // is set, then headlines and descriptions will be rotated and the ones that perform best will
    // be used more often.
    pinnedHeadline.setPinnedField(ServedAssetFieldType.HEADLINE_1);
    headlines.add(pinnedHeadline);
    headlines.add(createAssetLinkForText("Best Space Cruise Line"));
    headlines.add(createAssetLinkForText("Experience the Stars"));
    responsiveSearchAd.setHeadlines(headlines.toArray(new AssetLink[0]));
    List<AssetLink> descriptions = new ArrayList<>();
    descriptions.add(createAssetLinkForText("Buy your tickets now"));
    descriptions.add(createAssetLinkForText("Visit the Red Planet"));
    responsiveSearchAd.setDescriptions(descriptions.toArray(new AssetLink[0]));
    responsiveSearchAd.setFinalUrls(new String[] { "http://www.example.com/cruise" });
    responsiveSearchAd.setPath1("all-inclusive");
    responsiveSearchAd.setPath2("deals");
    // Create ad group ad.
    AdGroupAd responsiveSearchAdGroupAd = new AdGroupAd();
    responsiveSearchAdGroupAd.setAdGroupId(adGroupId);
    responsiveSearchAdGroupAd.setAd(responsiveSearchAd);
    // Optional: set the status.
    responsiveSearchAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create the operation.
    AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
    adGroupAdOperation.setOperand(responsiveSearchAdGroupAd);
    adGroupAdOperation.setOperator(Operator.ADD);
    operations.add(adGroupAdOperation);
    // Add ad.
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations.toArray(new AdGroupAdOperation[0]));
    for (AdGroupAd adGroupAdResult : result.getValue()) {
        ResponsiveSearchAd newAd = (ResponsiveSearchAd) adGroupAdResult.getAd();
        System.out.printf("New responsive search ad with ID %d was added.%n", adGroupAdResult.getAd().getId());
        System.out.println("Headlines:");
        for (AssetLink headline : newAd.getHeadlines()) {
            ServedAssetFieldType pinning = headline.getPinnedField();
            System.out.printf("    %s%n", ((TextAsset) headline.getAsset()).getAssetText());
            if (pinning != null) {
                System.out.printf("    (pinned to %s)%n", pinning);
            }
        }
        System.out.println("Descriptions:");
        for (AssetLink description : newAd.getDescriptions()) {
            ServedAssetFieldType pinning = description.getPinnedField();
            System.out.printf("    %s%n", ((TextAsset) description.getAsset()).getAssetText());
            if (pinning != null) {
                System.out.printf("    (pinned to %s)%n", pinning);
            }
        }
    }
}
Also used : AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) ServedAssetFieldType(com.google.api.ads.adwords.axis.v201809.cm.ServedAssetFieldType) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ArrayList(java.util.ArrayList) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) ResponsiveSearchAd(com.google.api.ads.adwords.axis.v201809.cm.ResponsiveSearchAd) AssetLink(com.google.api.ads.adwords.axis.v201809.cm.AssetLink)

Example 19 with Ad

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

the class GetExpandedTextAds method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group to use to find expanded text ads.
 * @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);
    int offset = 0;
    boolean morePages = true;
    // Create selector.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(AdGroupAdField.Id, AdGroupAdField.Status, AdGroupAdField.HeadlinePart1, AdGroupAdField.HeadlinePart2, AdGroupAdField.Description).orderAscBy(AdGroupAdField.Id).offset(offset).limit(PAGE_SIZE).equals(AdGroupAdField.AdGroupId, adGroupId.toString()).in(AdGroupAdField.Status, "ENABLED", "PAUSED").equals("AdType", "EXPANDED_TEXT_AD").build();
    while (morePages) {
        // Get all ads.
        AdGroupAdPage page = adGroupAdService.get(selector);
        // Display ads.
        if (page.getEntries() != null && page.getEntries().length > 0) {
            for (AdGroupAd adGroupAd : page.getEntries()) {
                ExpandedTextAd expandedTextAd = (ExpandedTextAd) adGroupAd.getAd();
                System.out.printf("Expanded text ad with ID %d, status '%s', and headline '%s - %s' was found.%n", adGroupAd.getAd().getId(), adGroupAd.getStatus(), expandedTextAd.getHeadlinePart1(), expandedTextAd.getHeadlinePart2());
            }
        } else {
            System.out.println("No expanded text ads were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
        morePages = offset < page.getTotalNumEntries();
    }
}
Also used : AdGroupAdPage(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdPage) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) 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) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 20 with Ad

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

the class AddShoppingDynamicRemarketingCampaign method createAdGroup.

/**
 * Creates an ad group in the specified campaign.
 *
 * @param campaign the campaign to which the ad group should be attached.
 * @return the ad group that was created.
 */
private static AdGroup createAdGroup(AdWordsServicesInterface services, AdWordsSession session, Campaign campaign) throws RemoteException {
    AdGroupServiceInterface adGroupService = services.get(session, AdGroupServiceInterface.class);
    AdGroup group = new AdGroup();
    group.setName("Dynamic remarketing ad group");
    group.setCampaignId(campaign.getId());
    group.setStatus(AdGroupStatus.ENABLED);
    AdGroupOperation op = new AdGroupOperation();
    op.setOperand(group);
    op.setOperator(Operator.ADD);
    AdGroupReturnValue result = adGroupService.mutate(new AdGroupOperation[] { op });
    return result.getValue(0);
}
Also used : AdGroupServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface) AdGroupReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupReturnValue) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)

Aggregations

AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)20 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)19 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)16 BiddableAdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion)16 AdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion)15 AdGroupCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation)15 ArrayList (java.util.ArrayList)14 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)13 AdGroup (com.google.api.ads.adwords.axis.v201809.cm.AdGroup)12 AdGroupAdReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue)12 AdGroupCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface)12 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)10 AdGroupOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)10 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)10 CpcBid (com.google.api.ads.adwords.axis.v201809.cm.CpcBid)10 AdGroupServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface)9 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)9 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)9 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)7 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)7