更新/设置广告组中特定购物产品的maxcpc

时间:2016-09-15 09:09:11

标签: google-adwords

我想更新adwords中特定产品的最高每次点击费用出价。 通过adwords的webui,这是一项微不足道的任务,但我无法让它在代码中工作,这是我到目前为止所做的。

private boolean inProgress = false;

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (!inProgress)
        return super.dispatchTouchEvent(ev);
    return true;
 }

执行此操作会导致错误:'解组错误:cvc-elt.4.2:无法解析&#ns; ns2:ProductScope'到元素的类型定义&#ns; ns2:标准''。

这是发送给谷歌的(anonimized)数据:

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.v201607.cm.*;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.client.auth.oauth2.Credential;
import java.rmi.RemoteException;
public class ChangeBidOnSpecificProduct {

public static void main(String[] args) throws Exception {
    OfflineCredentials build = new OfflineCredentials.Builder()
            .forApi(OfflineCredentials.Api.ADWORDS)
            .fromFile()
            .build();
    Credential oAuth2Credential = build
            .generateCredential();

    // Construct an AdWordsSession.
    AdWordsSession session = new AdWordsSession.Builder()
            .fromFile()
            .withOAuth2Credential(oAuth2Credential)
            .build();
    String accountId = "ACCOUNT_ID";
    Long campaignId = Long.valueOf("CAMPAIGN_ID");
    long adGroupId = Long.valueOf("ADGROUP_ID");
    session.setClientCustomerId(accountId);
    Money money = new Money(null, 40000L);
    String productId = "9200000050670959";
    changeBidViaApi(session, campaignId, adGroupId, productId, money);
}

private static void changeBidViaApi(AdWordsSession session, Long campaignId, long adGroupId, String productId, Money newValue) throws RemoteException {
    ProductOfferId productOfferId = new ProductOfferId();
    productOfferId.setValue(productId);
    ProductScope productScope = new ProductScope();
    productScope.setDimensions(new ProductDimension[] {productOfferId});

    BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
    biddableAdGroupCriterion.setAdGroupId(adGroupId);
    biddableAdGroupCriterion.setCriterion(productScope);

    BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
    CpcBid bid = new CpcBid();
    bid.setBid(newValue);
    biddingStrategyConfiguration.setBids(new Bids[]{bid});
    biddableAdGroupCriterion.setBiddingStrategyConfiguration(biddingStrategyConfiguration);

    AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
    operation.setOperand(biddableAdGroupCriterion);
    operation.setOperator(Operator.SET);

    AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[]{operation};

    AdWordsServices adWordsServices = new AdWordsServices();
    AdGroupCriterionServiceInterface adGroupCriterionService =
            adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
    AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
    }
}

任何建议都是错误的原因或我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

我设法通过以图表的形式访问adwords项目,迭代它并在找到匹配项时更改其出价值来实现目标。

private static void changeBidViaApi(AdWordsSession session, long adGroupId, String productId, Money newValue) throws RemoteException {
    AdWordsServices adWordsServices = new AdWordsServices();
    ProductPartitionTree partitionTree =
            ProductPartitionTree.createAdGroupTree(adWordsServices, session, adGroupId);

    for (ProductPartitionNode node : partitionTree.getRoot().getChildren()) {
        ProductPartitionNode productPartitionNode = node.asBiddableUnit();
        try {
            ProductOfferId dimension = (ProductOfferId) productPartitionNode.getDimension();
            if (dimension != null) {
                String productIdInShopping = dimension.getValue();
                if (productId.equals(productIdInShopping)) {
                    Long newBid = newValue.getMicroAmount();
                    productPartitionNode.setBid(newBid);
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    AdGroupCriterionServiceInterface adGroupCriterionService =
            adWordsServices.get(session, AdGroupCriterionServiceInterface.class);

    List<AdGroupCriterionOperation> mutateOperations = partitionTree.getMutateOperations();

    if (mutateOperations.isEmpty()) {
        System.out.println("Nothing to do.");
    } else {
        adGroupCriterionService.mutate(mutateOperations.toArray(new AdGroupCriterionOperation[0]));
    }
}

答案 1 :(得分:0)

这是有效的源代码。更改产品分区组的出价(最高每次点击费用)。它是用php编写的,但是您可以将此代码修改为java。

$adWordsServices = new AdWordsServices();
$session = $this->getSession();
$adGroupCriterionService = $adWordsServices->get($session, AdGroupCriterionService::class);
$operations = [];
$adGroupCriterion = new BiddableAdGroupCriterion();
$adGroupCriterion->setAdGroupId(22122723325); // id of my adgroup
$adGroupCriterion->setCriterion(new Criterion(302190832)); // id of partition group. you can get find this id in PRODUCT_PARTITION_REPORT in ID field (which full name is Criterion ID)
//
$bid = new CpcBid();
$money = new Money();
$money->setMicroAmount(((float)4)*1000000);
$bid->setBid($money);
$biddingStrategyConfiguration = new BiddingStrategyConfiguration();
$biddingStrategyConfiguration->setBids([$bid]);
$adGroupCriterion->setBiddingStrategyConfiguration($biddingStrategyConfiguration);
$operation = new AdGroupCriterionOperation();
$operation->setOperand($adGroupCriterion);
$operation->setOperator(Operator::SET);
$operations[] = $operation;
//
$adGroupCriterionService->mutate($operations);