Search in sources :

Example 1 with Location

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

the class AddGoogleMyBusinessLocationExtensions method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param gmbEmailAddress the email address of the owner or manager of the GMB account.
 * @param gmbAccessToken the OAuth2 access token for GMB.
 * @param businessAccountIdentifier optional identifier of the Google My Business account. This is
 *     required when the {@code gmbEmailAddress} is a GMB manager.
 * @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 InterruptedException if the thread was interrupted while sleeping between retries.
 */
private static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, String gmbEmailAddress, String gmbAccessToken, @Nullable String businessAccountIdentifier) throws RemoteException, InterruptedException {
    FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
    // Create a feed that will sync to the Google My Business account specified
    // by gmbEmailAddress. Do not add FeedAttributes to this object,
    // as AdWords will add them automatically because this will be a
    // system generated feed.
    Feed gmbFeed = new Feed();
    gmbFeed.setName("Google My Business feed #" + System.currentTimeMillis());
    PlacesLocationFeedData feedData = new PlacesLocationFeedData();
    feedData.setEmailAddress(gmbEmailAddress);
    feedData.setBusinessAccountIdentifier(businessAccountIdentifier);
    // Optional: specify labels to filter Google My Business listings. If
    // specified, only listings that have any of the labels set are
    // synchronized into FeedItems.
    feedData.setLabelFilters(new String[] { "Stores in New York City" });
    OAuthInfo oAuthInfo = new OAuthInfo();
    oAuthInfo.setHttpMethod("GET");
    oAuthInfo.setHttpRequestUrl(GetRefreshToken.ADWORDS_API_SCOPE);
    oAuthInfo.setHttpAuthorizationHeader(String.format("Bearer %s", gmbAccessToken));
    feedData.setOAuthInfo(oAuthInfo);
    gmbFeed.setSystemFeedGenerationData(feedData);
    // Since this feed's feed items will be managed by AdWords,
    // you must set its origin to ADWORDS.
    gmbFeed.setOrigin(FeedOrigin.ADWORDS);
    // Create an operation to add the feed.
    FeedOperation feedOperation = new FeedOperation();
    feedOperation.setOperand(gmbFeed);
    feedOperation.setOperator(Operator.ADD);
    // Add the feed. Since it is a system generated feed, AdWords will automatically:
    // 1. Set up the FeedAttributes on the feed.
    // 2. Set up a FeedMapping that associates the FeedAttributes of the feed
    // with the placeholder fields of the LOCATION placeholder type.
    FeedReturnValue addFeedResult = feedService.mutate(new FeedOperation[] { feedOperation });
    Feed addedFeed = addFeedResult.getValue(0);
    System.out.printf("Added GMB feed with ID %d%n", addedFeed.getId());
    // Add a CustomerFeed that associates the feed with this customer for
    // the LOCATION placeholder type.
    CustomerFeed customerFeed = new CustomerFeed();
    customerFeed.setFeedId(addedFeed.getId());
    customerFeed.setPlaceholderTypes(new int[] { PLACEHOLDER_LOCATION });
    // Create a matching function that will always evaluate to true.
    Function customerMatchingFunction = new Function();
    ConstantOperand constOperand = new ConstantOperand();
    constOperand.setType(ConstantOperandConstantType.BOOLEAN);
    constOperand.setBooleanValue(true);
    customerMatchingFunction.setLhsOperand(new FunctionArgumentOperand[] { constOperand });
    customerMatchingFunction.setOperator(FunctionOperator.IDENTITY);
    customerFeed.setMatchingFunction(customerMatchingFunction);
    // Create an operation to add the customer feed.
    CustomerFeedOperation customerFeedOperation = new CustomerFeedOperation();
    customerFeedOperation.setOperand(customerFeed);
    customerFeedOperation.setOperator(Operator.ADD);
    CustomerFeedServiceInterface customerFeedService = adWordsServices.get(session, CustomerFeedServiceInterface.class);
    // After the completion of the Feed ADD operation above the added feed will not be available
    // for usage in a CustomerFeed until the sync between the AdWords and GMB accounts
    // completes. The loop below will retry adding the CustomerFeed up to ten times with an
    // exponential back-off policy.
    CustomerFeed addedCustomerFeed = null;
    int numberOfAttempts = 0;
    do {
        numberOfAttempts++;
        try {
            CustomerFeedReturnValue customerFeedResult = customerFeedService.mutate(new CustomerFeedOperation[] { customerFeedOperation });
            addedCustomerFeed = customerFeedResult.getValue(0);
            System.out.printf("Attempt #%d to add the CustomerFeed was successful%n", numberOfAttempts);
        } catch (Exception e) {
            // Wait using exponential backoff policy
            long sleepSeconds = (long) Math.scalb(5, numberOfAttempts);
            System.out.printf("Attempt #%d to add the CustomerFeed was not successful. " + "Waiting %d seconds before trying again.%n", numberOfAttempts, sleepSeconds);
            Thread.sleep(sleepSeconds * 1000);
        }
    } while (numberOfAttempts < MAX_CUSTOMER_FEED_ADD_ATTEMPTS && addedCustomerFeed == null);
    if (addedCustomerFeed == null) {
        throw new RuntimeException("Could not create the CustomerFeed after " + MAX_CUSTOMER_FEED_ADD_ATTEMPTS + " attempts. Please retry " + "the CustomerFeed ADD operation later.");
    }
    System.out.printf("Added CustomerFeed for feed ID %d and placeholder type %d%n", addedCustomerFeed.getFeedId(), addedCustomerFeed.getPlaceholderTypes()[0]);
// OPTIONAL: Create a CampaignFeed to specify which FeedItems to use at the Campaign
// level. This will be similar to the CampaignFeed in the AddSiteLinks example, except
// you can also filter based on the business name and category of each FeedItem
// by using a FeedAttributeOperand in your matching function.
// OPTIONAL: Create an AdGroupFeed for even more fine grained control over
// which feed items are used at the AdGroup level.
}
Also used : ConstantOperand(com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand) FeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.FeedReturnValue) CustomerFeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedReturnValue) CustomerFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedServiceInterface) PlacesLocationFeedData(com.google.api.ads.adwords.axis.v201809.cm.PlacesLocationFeedData) FeedOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedOperation) CustomerFeedOperation(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedOperation) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) RemoteException(java.rmi.RemoteException) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) Function(com.google.api.ads.adwords.axis.v201809.cm.Function) CustomerFeed(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeed) FeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface) CustomerFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedServiceInterface) OAuthInfo(com.google.api.ads.adwords.axis.v201809.cm.OAuthInfo) CustomerFeedOperation(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedOperation) CustomerFeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedReturnValue) CustomerFeed(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeed) Feed(com.google.api.ads.adwords.axis.v201809.cm.Feed)

Example 2 with Location

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

the class AddSiteLinksUsingFeeds method createSiteLinksFeedItems.

private static void createSiteLinksFeedItems(AdWordsServicesInterface adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData) throws RemoteException {
    // Get the FeedItemService.
    FeedItemServiceInterface feedItemService = adWordsServices.get(session, FeedItemServiceInterface.class);
    // Create operations to add FeedItems.
    FeedItemOperation home = newSiteLinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com", "Home line 2", "Home line 3");
    FeedItemOperation stores = newSiteLinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores", "Stores line 2", "Stores line 3");
    FeedItemOperation onSale = newSiteLinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale", "On Sale line 2", "On Sale line 3");
    FeedItemOperation support = newSiteLinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support", "Support line 2", "Support line 3");
    FeedItemOperation products = newSiteLinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods", "Products line 2", "Products line 3");
    // This site link is using geographical targeting to use LOCATION_OF_PRESENCE.
    FeedItemOperation aboutUs = newSiteLinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about", "About Us line 2", "About Us line 3", true);
    FeedItemOperation[] operations = new FeedItemOperation[] { home, stores, onSale, support, products, aboutUs };
    FeedItemReturnValue result = feedItemService.mutate(operations);
    for (FeedItem item : result.getValue()) {
        System.out.printf("FeedItem with feedItemId %d was added.%n", item.getFeedItemId());
        siteLinksData.siteLinkFeedItemIds.add(item.getFeedItemId());
    }
    // Target the "aboutUs" sitelink to geographically target California.
    // See https://developers.google.com/adwords/api/docs/appendix/geotargeting for
    // location criteria for supported locations.
    restrictFeedItemToGeoTarget(adWordsServices, session, result.getValue(5), 21137L);
}
Also used : FeedItem(com.google.api.ads.adwords.axis.v201809.cm.FeedItem) FeedItemServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedItemServiceInterface) FeedItemReturnValue(com.google.api.ads.adwords.axis.v201809.cm.FeedItemReturnValue) FeedItemOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedItemOperation)

Example 3 with Location

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

the class AddSiteLinksUsingFeeds method newSiteLinkFeedItemAddOperation.

private static FeedItemOperation newSiteLinkFeedItemAddOperation(SiteLinksDataHolder siteLinksData, String text, String finalUrl, String line2, String line3, boolean restrictToLop) {
    // Create the FeedItemAttributeValues for our text values.
    FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue();
    linkTextAttributeValue.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
    linkTextAttributeValue.setStringValue(text);
    FeedItemAttributeValue linkFinalUrlAttributeValue = new FeedItemAttributeValue();
    linkFinalUrlAttributeValue.setFeedAttributeId(siteLinksData.linkFinalUrlFeedAttributeId);
    linkFinalUrlAttributeValue.setStringValues(new String[] { finalUrl });
    FeedItemAttributeValue line2TextAttributeValue = new FeedItemAttributeValue();
    line2TextAttributeValue.setFeedAttributeId(siteLinksData.line2FeedAttributeId);
    line2TextAttributeValue.setStringValue(line2);
    FeedItemAttributeValue line3TextAttributeValue = new FeedItemAttributeValue();
    line3TextAttributeValue.setFeedAttributeId(siteLinksData.line3FeedAttributeId);
    line3TextAttributeValue.setStringValue(line3);
    // Create the feed item and operation.
    FeedItem item = new FeedItem();
    item.setFeedId(siteLinksData.siteLinksFeedId);
    item.setAttributeValues(new FeedItemAttributeValue[] { linkTextAttributeValue, linkFinalUrlAttributeValue, line2TextAttributeValue, line3TextAttributeValue });
    // OPTIONAL: Restrict targeting only to people physically within the location.
    if (restrictToLop) {
        FeedItemGeoRestriction geoTargetingRestriction = new FeedItemGeoRestriction();
        geoTargetingRestriction.setGeoRestriction(GeoRestriction.LOCATION_OF_PRESENCE);
        item.setGeoTargetingRestriction(geoTargetingRestriction);
    }
    // Optional: use item.setStartTime() and item.setEndTime() to specify the
    // time period for the feed to deliver.  The example below will make the feed
    // start now and stop in one month.
    // Make sure you specify the DateTime in the customer's time zone.  You can
    // retrieve this from customer.getDateTimeZone().
    // item.setStartTime(new DateTime(customerTimeZone).toString("yyyyMMdd HHmmss"));
    // item.setEndTime(new DateTime(customerTimeZone).plusMonths(1).toString("yyyyMMdd HHmmss"));
    // Optional: use item.setScheduling() to specify time and days of the week for feed to deliver.
    FeedItemOperation operation = new FeedItemOperation();
    operation.setOperand(item);
    operation.setOperator(Operator.ADD);
    return operation;
}
Also used : FeedItem(com.google.api.ads.adwords.axis.v201809.cm.FeedItem) FeedItemAttributeValue(com.google.api.ads.adwords.axis.v201809.cm.FeedItemAttributeValue) FeedItemGeoRestriction(com.google.api.ads.adwords.axis.v201809.cm.FeedItemGeoRestriction) FeedItemOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedItemOperation)

Example 4 with Location

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

the class LookupLocation method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param locationNames the location names to use for the lookup.
 * @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, String[] locationNames) throws RemoteException {
    // Get the LocationCriterionService.
    LocationCriterionServiceInterface locationCriterionService = adWordsServices.get(session, LocationCriterionServiceInterface.class);
    Selector selector = new SelectorBuilder().fields("Id", "LocationName", "CanonicalName", "DisplayType", "ParentLocations", "Reach", "TargetingStatus").in("LocationName", locationNames).equals("Locale", "en").build();
    // Make the get request.
    LocationCriterion[] locationCriteria = locationCriterionService.get(selector);
    // Display the resulting location criteria.
    for (LocationCriterion locationCriterion : locationCriteria) {
        String parentString = getParentLocationString(locationCriterion.getLocation().getParentLocations());
        System.out.printf("The search term '%s' returned the location '%s (%d)' of type '%s' " + "with parent locations '%s' and reach %d (%s).%n", locationCriterion.getSearchTerm(), locationCriterion.getLocation().getLocationName(), locationCriterion.getLocation().getId(), locationCriterion.getLocation().getDisplayType(), parentString, locationCriterion.getReach(), locationCriterion.getLocation().getTargetingStatus());
    }
}
Also used : SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) LocationCriterion(com.google.api.ads.adwords.axis.v201809.cm.LocationCriterion) LocationCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.LocationCriterionServiceInterface) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 5 with Location

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

the class AddCampaignTargetingCriteria method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param campaignId the ID of the campaign where targeting criteria will be added.
 * @param locationFeedId optional ID of a location targeting feed.
 * @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, @Nullable Long locationFeedId) throws RemoteException {
    // Get the CampaignService.
    CampaignCriterionServiceInterface campaignCriterionService = adWordsServices.get(session, CampaignCriterionServiceInterface.class);
    // Create locations. The IDs can be found in the documentation or
    // retrieved with the LocationCriterionService.
    Location california = new Location();
    california.setId(21137L);
    Location mexico = new Location();
    mexico.setId(2484L);
    // Create languages. The IDs can be found in the documentation or
    // retrieved with the ConstantDataService.
    Language english = new Language();
    english.setId(1000L);
    Language spanish = new Language();
    spanish.setId(1003L);
    List<Criterion> criteria = new ArrayList<>(Arrays.asList(california, mexico, english, spanish));
    // Distance targeting. Area of 10 miles around the locations in the location feed.
    if (locationFeedId != null) {
        LocationGroups radiusLocationGroup = new LocationGroups();
        radiusLocationGroup.setFeedId(locationFeedId);
        ConstantOperand radius = new ConstantOperand();
        radius.setType(ConstantOperandConstantType.DOUBLE);
        radius.setUnit(ConstantOperandUnit.MILES);
        radius.setDoubleValue(10d);
        LocationExtensionOperand distance = new LocationExtensionOperand();
        distance.setRadius(radius);
        Function radiusMatchingFunction = new Function();
        radiusMatchingFunction.setOperator(FunctionOperator.IDENTITY);
        radiusMatchingFunction.setLhsOperand(new FunctionArgumentOperand[] { distance });
        radiusLocationGroup.setMatchingFunction(radiusMatchingFunction);
        criteria.add(radiusLocationGroup);
    }
    // Create operations to add each of the criteria above.
    List<CampaignCriterionOperation> operations = new ArrayList<>();
    for (Criterion criterion : criteria) {
        CampaignCriterionOperation operation = new CampaignCriterionOperation();
        CampaignCriterion campaignCriterion = new CampaignCriterion();
        campaignCriterion.setCampaignId(campaignId);
        campaignCriterion.setCriterion(criterion);
        operation.setOperand(campaignCriterion);
        operation.setOperator(Operator.ADD);
        operations.add(operation);
    }
    // Add a negative campaign criterion.
    Keyword negativeKeyword = new Keyword();
    negativeKeyword.setText("jupiter cruise");
    negativeKeyword.setMatchType(KeywordMatchType.BROAD);
    CampaignCriterion negativeCriterion = new NegativeCampaignCriterion();
    negativeCriterion.setCampaignId(campaignId);
    negativeCriterion.setCriterion(negativeKeyword);
    CampaignCriterionOperation operation = new CampaignCriterionOperation();
    operation.setOperand(negativeCriterion);
    operation.setOperator(Operator.ADD);
    operations.add(operation);
    CampaignCriterionReturnValue result = campaignCriterionService.mutate(operations.toArray(new CampaignCriterionOperation[operations.size()]));
    // Display campaigns.
    for (CampaignCriterion campaignCriterion : result.getValue()) {
        System.out.printf("Campaign criterion with campaign ID %d, criterion ID %d, " + "and type '%s' was added.%n", campaignCriterion.getCampaignId(), campaignCriterion.getCriterion().getId(), campaignCriterion.getCriterion().getCriterionType());
    }
}
Also used : ConstantOperand(com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand) CampaignCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionOperation) LocationGroups(com.google.api.ads.adwords.axis.v201809.cm.LocationGroups) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) NegativeCampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeCampaignCriterion) CampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion) ArrayList(java.util.ArrayList) NegativeCampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeCampaignCriterion) CampaignCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionReturnValue) Function(com.google.api.ads.adwords.axis.v201809.cm.Function) Language(com.google.api.ads.adwords.axis.v201809.cm.Language) NegativeCampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeCampaignCriterion) CampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion) Criterion(com.google.api.ads.adwords.axis.v201809.cm.Criterion) CampaignCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionServiceInterface) LocationExtensionOperand(com.google.api.ads.adwords.axis.v201809.cm.LocationExtensionOperand) Location(com.google.api.ads.adwords.axis.v201809.cm.Location)

Aggregations

Location (com.google.api.ads.adwords.axis.v201809.cm.Location)5 CampaignCriterion (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion)3 CampaignCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionServiceInterface)3 Criterion (com.google.api.ads.adwords.axis.v201809.cm.Criterion)3 Keyword (com.google.api.ads.adwords.axis.v201809.cm.Keyword)3 Language (com.google.api.ads.adwords.axis.v201809.cm.Language)3 ArrayList (java.util.ArrayList)3 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)2 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)2 CampaignCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionOperation)2 CampaignCriterionReturnValue (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionReturnValue)2 ConstantOperand (com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand)2 FeedItem (com.google.api.ads.adwords.axis.v201809.cm.FeedItem)2 FeedItemGeoRestriction (com.google.api.ads.adwords.axis.v201809.cm.FeedItemGeoRestriction)2 FeedItemOperation (com.google.api.ads.adwords.axis.v201809.cm.FeedItemOperation)2 Function (com.google.api.ads.adwords.axis.v201809.cm.Function)2 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)2 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)1 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)1 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)1