Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9005 manish.sha 1
// Copyright 2013 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
package adwords.axis.v201306.campaignmanagement;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201306.cm.CampaignCriterion;
19
import com.google.api.ads.adwords.axis.v201306.cm.CampaignCriterionOperation;
20
import com.google.api.ads.adwords.axis.v201306.cm.CampaignCriterionReturnValue;
21
import com.google.api.ads.adwords.axis.v201306.cm.CampaignCriterionServiceInterface;
22
import com.google.api.ads.adwords.axis.v201306.cm.Operator;
23
import com.google.api.ads.adwords.axis.v201306.cm.Platform;
24
import com.google.api.ads.adwords.lib.client.AdWordsSession;
25
import com.google.api.ads.common.lib.auth.OfflineCredentials;
26
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
27
import com.google.api.client.auth.oauth2.Credential;
28
 
29
/**
30
 * This example sets a bid modifier for the mobile platform on given campaign.
31
 * NOTE: the campaign must be an enhanced type of campaign.
32
 * To get campaigns, run basicoperations/GetCampaigns.java.
33
 * To enhance a campaign, run campaignmanagement/SetCampaignEnhanced.java.
34
 *
35
 * Credentials and properties in {@code fromFile()} are pulled from the
36
 * "ads.properties" file. See README for more info.
37
 *
38
 * Tags: CampaignCriterionService.mutate
39
 *
40
 * @author Kevin Winter
41
 */
42
public class SetBidModifier {
43
 
44
  private static final double BID_MODIFIER = 1.5;
45
 
46
  public static void main(String[] args) throws Exception {
47
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
48
    // and can be used in place of a service account.
49
    Credential oAuth2Credential = new OfflineCredentials.Builder()
50
        .forApi(Api.ADWORDS)
51
        .fromFile()
52
        .build()
53
        .generateCredential();
54
 
55
    // Construct an AdWordsSession.
56
    AdWordsSession session = new AdWordsSession.Builder()
57
        .fromFile()
58
        .withOAuth2Credential(oAuth2Credential)
59
        .build();
60
 
61
    Long campaignId = Long.valueOf("INSERT_CAMPAIGN_ID_HERE");
62
 
63
    AdWordsServices adWordsServices = new AdWordsServices();
64
 
65
    runExample(adWordsServices, session, campaignId);
66
  }
67
 
68
  public static void runExample(
69
      AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
70
    // Get the CampaignCriterionService.
71
    CampaignCriterionServiceInterface campaignCriterionService =
72
        adWordsServices.get(session, CampaignCriterionServiceInterface.class);
73
 
74
    // Create mobile platform. The ID can be found in the documentation.
75
    // https://developers.google.com/adwords/api/docs/appendix/platforms
76
    Platform mobile = new Platform();
77
    mobile.setId(30001L);
78
 
79
    // Create criterion with modified bid.
80
    CampaignCriterion campaignCriterion = new CampaignCriterion();
81
    campaignCriterion.setCampaignId(campaignId);
82
    campaignCriterion.setCriterion(mobile);
83
    campaignCriterion.setBidModifier(BID_MODIFIER);
84
 
85
    // Create SET operation.
86
    CampaignCriterionOperation operation = new CampaignCriterionOperation();
87
    operation.setOperand(campaignCriterion);
88
    operation.setOperator(Operator.SET);
89
 
90
    // Update campaign criterion.
91
    CampaignCriterionReturnValue result =
92
        campaignCriterionService.mutate(new CampaignCriterionOperation[] {operation});
93
    for (CampaignCriterion campaignCriterionResult : result.getValue()) {
94
      System.out.printf("Campaign criterion with campaign id '%s', criterion id '%s', "
95
          + "and type '%s' was modified with bid %.2f.\n",
96
          campaignCriterionResult.getCampaignId(),
97
          campaignCriterionResult.getCriterion().getId(),
98
          campaignCriterionResult.getCriterion().getType(),
99
          campaignCriterionResult.getBidModifier());
100
    }
101
  }
102
}