Package v201306

Source Code of v201306.AddSiteLinks$SiteLinksDataHolder

// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v201306;

import com.google.api.adwords.lib.AdWordsService;
import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.v201306.cm.AttributeFieldMapping;
import com.google.api.adwords.v201306.cm.CampaignFeed;
import com.google.api.adwords.v201306.cm.CampaignFeedOperation;
import com.google.api.adwords.v201306.cm.CampaignFeedReturnValue;
import com.google.api.adwords.v201306.cm.CampaignFeedServiceInterface;
import com.google.api.adwords.v201306.cm.ConstantOperand;
import com.google.api.adwords.v201306.cm.ConstantOperandConstantType;
import com.google.api.adwords.v201306.cm.Feed;
import com.google.api.adwords.v201306.cm.FeedAttribute;
import com.google.api.adwords.v201306.cm.FeedAttributeType;
import com.google.api.adwords.v201306.cm.FeedItem;
import com.google.api.adwords.v201306.cm.FeedItemAttributeValue;
import com.google.api.adwords.v201306.cm.FeedItemOperation;
import com.google.api.adwords.v201306.cm.FeedItemReturnValue;
import com.google.api.adwords.v201306.cm.FeedItemServiceInterface;
import com.google.api.adwords.v201306.cm.FeedMapping;
import com.google.api.adwords.v201306.cm.FeedMappingOperation;
import com.google.api.adwords.v201306.cm.FeedMappingReturnValue;
import com.google.api.adwords.v201306.cm.FeedMappingServiceInterface;
import com.google.api.adwords.v201306.cm.FeedOperation;
import com.google.api.adwords.v201306.cm.FeedOrigin;
import com.google.api.adwords.v201306.cm.FeedReturnValue;
import com.google.api.adwords.v201306.cm.FeedServiceInterface;
import com.google.api.adwords.v201306.cm.Function;
import com.google.api.adwords.v201306.cm.FunctionArgumentOperand;
import com.google.api.adwords.v201306.cm.FunctionOperand;
import com.google.api.adwords.v201306.cm.FunctionOperator;
import com.google.api.adwords.v201306.cm.Operator;
import com.google.api.adwords.v201306.cm.RequestContextOperand;
import com.google.api.adwords.v201306.cm.RequestContextOperandContextType;

import java.util.ArrayList;
import java.util.List;

/**
* This example adds a sitelinks feed and associated it with a campaign.
*
* Tags: FeedService.mutate, FeedItemService.mutate, FeedMappingService.mutate,
* Tags: CampaignFeedService.mutate
*
* @author thagikura@google.com (Takeshi Hagikura)
*/
public class AddSiteLinks {

  public static void main(String[] args) throws Exception {
    AdWordsServiceLogger.log();

    // Get AdWordsUser from "~/adwords.properties".
    AdWordsUser user = new AdWordsUser();
    Long campaignId = Long.valueOf("INSERT_CAMPAIGN_ID_HERE");

    runExample(user, campaignId);
  }

  public static void runExample(AdWordsUser user, Long campaignId) throws Exception {
    SiteLinksDataHolder siteLinksData = new SiteLinksDataHolder();
    createSiteLinksFeed(user, siteLinksData);
    createSiteLinksFeedItems(user, siteLinksData);
    createSiteLinksFeedMapping(user, siteLinksData);
    createSiteLinksCampaignFeed(user, siteLinksData, campaignId);
  }

  private static void createSiteLinksFeed(AdWordsUser user, SiteLinksDataHolder siteLinksData)
      throws Exception {
    // Get the FeedService.
    FeedServiceInterface feedService = user.getService(AdWordsService.V201306.FEED_SERVICE);

    // Create attributes.
    FeedAttribute textAttribute = new FeedAttribute();
    textAttribute.setType(FeedAttributeType.STRING);
    textAttribute.setName("Link Text");
    FeedAttribute urlAttribute = new FeedAttribute();
    urlAttribute.setType(FeedAttributeType.URL);
    urlAttribute.setName("Link URL");

    // Create the feed.
    Feed siteLinksFeed = new Feed();
    siteLinksFeed.setName("Feed For Site Links");
    siteLinksFeed.setAttributes(new FeedAttribute[] {textAttribute, urlAttribute});
    siteLinksFeed.setOrigin(FeedOrigin.USER);

    // Create operation.
    FeedOperation operation = new FeedOperation();
    operation.setOperand(siteLinksFeed);
    operation.setOperator(Operator.ADD);

    // Add the feed.
    FeedReturnValue result = feedService.mutate(new FeedOperation[] {operation});

    Feed savedFeed = result.getValue()[0];
    siteLinksData.siteLinksFeedId = savedFeed.getId();
    FeedAttribute[] savedAttributes = savedFeed.getAttributes();
    siteLinksData.linkTextFeedAttributeId = savedAttributes[0].getId();
    siteLinksData.linkUrlFeedAttributeId = savedAttributes[1].getId();
    System.out.printf("Campaign with name %s and id %d with linkTextAttributeId %d"
        + " and linkUrlAttributeId %s was created.\n", savedFeed.getName(), savedFeed.getId(),
        savedAttributes[0].getId(), savedAttributes[1].getId());
  }

  private static void createSiteLinksFeedItems(AdWordsUser user, SiteLinksDataHolder siteLinksData)
      throws Exception {
    // Get the FeedItemService.
    FeedItemServiceInterface feedItemService =
        user.getService(AdWordsService.V201306.FEED_ITEM_SERVICE);

    // Create operations to add FeedItems.
    FeedItemOperation home =
        newSiteLinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com");
    FeedItemOperation stores =
        newSiteLinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores");
    FeedItemOperation onSale =
        newSiteLinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale");
    FeedItemOperation support =
        newSiteLinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support");
    FeedItemOperation products =
        newSiteLinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods");
    FeedItemOperation aboutUs =
        newSiteLinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about");

    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());
    }
  }

  // See the Placeholder reference page for a list of all the placeholder types and fields.
  //   https://developers.google.com/adwords/api/docs/appendix/placeholders.html
  private static final int PLACEHOLDER_SITELINKS = 1;

  // See the Placeholder reference page for a list of all the placeholder types and fields.
  private static final int PLACEHOLDER_FIELD_SITELINK_LINK_TEXT = 1;
  private static final int PLACEHOLDER_FIELD_SITELINK_URL = 2;

  private static void createSiteLinksFeedMapping(
      AdWordsUser user, SiteLinksDataHolder siteLinksData) throws Exception {
    // Get the FeedItemService.
    FeedMappingServiceInterface feedMappingService =
        user.getService(AdWordsService.V201306.FEED_MAPPING_SERVICE);

    // Map the FeedAttributeIds to the fieldId constants.
    AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping();
    linkTextFieldMapping.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
    linkTextFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_LINK_TEXT);
    AttributeFieldMapping linkUrlFieldMapping = new AttributeFieldMapping();
    linkUrlFieldMapping.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
    linkUrlFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_URL);

    // Create the FieldMapping and operation.
    FeedMapping feedMapping = new FeedMapping();
    feedMapping.setPlaceholderType(PLACEHOLDER_SITELINKS);
    feedMapping.setFeedId(siteLinksData.siteLinksFeedId);
    feedMapping.setAttributeFieldMappings(
        new AttributeFieldMapping[] {linkTextFieldMapping, linkUrlFieldMapping});
    FeedMappingOperation operation = new FeedMappingOperation();
    operation.setOperand(feedMapping);
    operation.setOperator(Operator.ADD);

    // Save the field mapping.
    FeedMappingReturnValue result =
        feedMappingService.mutate(new FeedMappingOperation[] {operation});
    for (FeedMapping savedFeedMapping : result.getValue()) {
      System.out.printf(
          "Feed mapping with id %d and placeholderType %d was saved for feed with id %d.\n",
          savedFeedMapping.getFeedMappingId(), savedFeedMapping.getPlaceholderType(),
          savedFeedMapping.getFeedId());
    }
  }

  private static void createSiteLinksCampaignFeed(
      AdWordsUser user, SiteLinksDataHolder siteLinksData, Long campaignId) throws Exception {
    // Get the CampaignFeedService.
    CampaignFeedServiceInterface campaignFeedService =
        user.getService(AdWordsService.V201306.CAMPAIGN_FEED_SERVICE);

    RequestContextOperand requestContextOperand = new RequestContextOperand();
    requestContextOperand.setContextType(RequestContextOperandContextType.FEED_ITEM_ID);

    Function feedItemFunction = new Function();
    feedItemFunction.setLhsOperand(new FunctionArgumentOperand[] {requestContextOperand});
    feedItemFunction.setOperator(FunctionOperator.IN);

    List<FunctionArgumentOperand> operands = new ArrayList<FunctionArgumentOperand>();
    for (long feedItemId : siteLinksData.siteLinkFeedItemIds) {
      ConstantOperand constantOperand = new ConstantOperand();
      constantOperand.setLongValue(feedItemId);
      // constantOperand.setType(ConstantOperandConstantType.INTEGER);
      constantOperand.setType(ConstantOperandConstantType.LONG);
      operands.add(constantOperand);
    }
    feedItemFunction.setRhsOperand(operands.toArray(new FunctionArgumentOperand[operands.size()]));

    // Optional: to target to a platform, define a function and 'AND' it with
    // the feed item ID link:
    RequestContextOperand platformRequestContextOperand = new RequestContextOperand();
    platformRequestContextOperand.setContextType(RequestContextOperandContextType.DEVICE_PLATFORM);

    ConstantOperand platformOperand = new ConstantOperand();
    platformOperand.setStringValue("Mobile");
    platformOperand.setType(ConstantOperandConstantType.STRING);

    Function platformFunction = new Function();
    platformFunction.setLhsOperand(new FunctionArgumentOperand[] {platformRequestContextOperand});
    platformFunction.setOperator(FunctionOperator.EQUALS);
    platformFunction.setRhsOperand(new FunctionArgumentOperand[] {platformOperand});

    // Combine the two functions using an AND operation.
    FunctionOperand feedItemFunctionOperand = new FunctionOperand();
    feedItemFunctionOperand.setValue(feedItemFunction);

    FunctionOperand platformFunctionOperand = new FunctionOperand();
    platformFunctionOperand.setValue(platformFunction);

    Function combinedFunction = new Function();
    combinedFunction.setOperator(FunctionOperator.AND);
    combinedFunction.setLhsOperand(new FunctionArgumentOperand[] {
        feedItemFunctionOperand, platformFunctionOperand});

    CampaignFeed campaignFeed = new CampaignFeed();
    campaignFeed.setFeedId(siteLinksData.siteLinksFeedId);
    campaignFeed.setCampaignId(campaignId);
    campaignFeed.setMatchingFunction(combinedFunction);

    // Specifying placeholder types on the CampaignFeed allows the same feed
    // to be used for different placeholders in different Campaigns.
    campaignFeed.setPlaceholderTypes(new int[] {PLACEHOLDER_SITELINKS});

    CampaignFeedOperation operation = new CampaignFeedOperation();
    operation.setOperand(campaignFeed);
    operation.setOperator(Operator.ADD);
    CampaignFeedReturnValue result =
        campaignFeedService.mutate(new CampaignFeedOperation[] {operation});
    for (CampaignFeed savedCampaignFeed : result.getValue()) {
      System.out.printf("Campaign with id %d was associated with feed with id %d.\n",
          savedCampaignFeed.getCampaignId(), savedCampaignFeed.getFeedId());
    }
  }

  private static FeedItemOperation newSiteLinkFeedItemAddOperation(
      SiteLinksDataHolder siteLinksData, String text, String url) {
    // Create the FeedItemAttributeValues for our text values.
    FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue();
    linkTextAttributeValue.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
    linkTextAttributeValue.setStringValue(text);
    FeedItemAttributeValue linkUrlAttributeValue = new FeedItemAttributeValue();
    linkUrlAttributeValue.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
    linkUrlAttributeValue.setStringValue(url);

    // Create the feed item and operation.
    FeedItem item = new FeedItem();
    item.setFeedId(siteLinksData.siteLinksFeedId);
    item.setAttributeValues(
        new FeedItemAttributeValue[] {linkTextAttributeValue, linkUrlAttributeValue});
    FeedItemOperation operation = new FeedItemOperation();
    operation.setOperand(item);
    operation.setOperator(Operator.ADD);
    return operation;
  }

  private static class SiteLinksDataHolder {
    private Long siteLinksFeedId;
    private Long linkTextFeedAttributeId;
    private Long linkUrlFeedAttributeId;
    private List<Long> siteLinkFeedItemIds = new ArrayList<Long>();
  }
}
TOP

Related Classes of v201306.AddSiteLinks$SiteLinksDataHolder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.