Subversion Repositories SmartDukaan

Rev

Rev 9005 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9005 Rev 9227
Line 22... Line 22...
22
import com.google.api.ads.adwords.axis.v201309.cm.BiddableAdGroupCriterion;
22
import com.google.api.ads.adwords.axis.v201309.cm.BiddableAdGroupCriterion;
23
import com.google.api.ads.adwords.axis.v201309.cm.BiddingStrategyConfiguration;
23
import com.google.api.ads.adwords.axis.v201309.cm.BiddingStrategyConfiguration;
24
import com.google.api.ads.adwords.axis.v201309.cm.Bids;
24
import com.google.api.ads.adwords.axis.v201309.cm.Bids;
25
import com.google.api.ads.adwords.axis.v201309.cm.CpcBid;
25
import com.google.api.ads.adwords.axis.v201309.cm.CpcBid;
26
import com.google.api.ads.adwords.axis.v201309.cm.Criterion;
26
import com.google.api.ads.adwords.axis.v201309.cm.Criterion;
-
 
27
import com.google.api.ads.adwords.axis.v201309.cm.Keyword;
-
 
28
import com.google.api.ads.adwords.axis.v201309.cm.KeywordMatchType;
27
import com.google.api.ads.adwords.axis.v201309.cm.Money;
29
import com.google.api.ads.adwords.axis.v201309.cm.Money;
28
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
30
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
29
import com.google.api.ads.adwords.lib.client.AdWordsSession;
31
import com.google.api.ads.adwords.lib.client.AdWordsSession;
30
import com.google.api.ads.common.lib.auth.OfflineCredentials;
32
import com.google.api.ads.common.lib.auth.OfflineCredentials;
31
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
33
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
Line 44... Line 46...
44
 *
46
 *
45
 * @author Kevin Winter
47
 * @author Kevin Winter
46
 */
48
 */
47
public class UpdateKeyword {
49
public class UpdateKeyword {
48
 
50
 
49
  public static void main(String[] args) throws Exception {
51
	public static Long runExample(Long adGroupId, Long keywordId, String text, Long bidAmount, String type)
-
 
52
	throws Exception {
-
 
53
		
50
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
54
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
51
    // and can be used in place of a service account.
55
		// and can be used in place of a service account.
52
    Credential oAuth2Credential = new OfflineCredentials.Builder()
56
		Credential oAuth2Credential = new OfflineCredentials.Builder()
53
        .forApi(Api.ADWORDS)
57
		.forApi(Api.ADWORDS)
54
        .fromFile()
58
		.fromFile()
55
        .build()
59
		.build()
56
        .generateCredential();
60
		.generateCredential();
57
 
61
 
58
    // Construct an AdWordsSession.
62
		// Construct an AdWordsSession.
59
    AdWordsSession session = new AdWordsSession.Builder()
63
		AdWordsSession session = new AdWordsSession.Builder()
60
        .fromFile()
64
		.fromFile()
61
        .withOAuth2Credential(oAuth2Credential)
65
		.withOAuth2Credential(oAuth2Credential)
62
        .build();
66
		.build();
63
 
67
 
64
    long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
68
		AdWordsServices adWordsServices = new AdWordsServices();
65
    long keywordId = Long.parseLong("INSERT_KEYWORD_ID_HERE");
-
 
66
 
69
		
-
 
70
		// Get the AdGroupCriterionService.
67
    AdWordsServices adWordsServices = new AdWordsServices();
71
		AdGroupCriterionServiceInterface adGroupCriterionService =
68
 
-
 
69
    runExample(adWordsServices, session, adGroupId, keywordId);
72
			adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
70
  }
-
 
71
 
73
 
72
  public static void runExample(
74
		Keyword keyword = new Keyword();
73
      AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId, Long keywordId)
75
		if(text!=null && ("").equalsIgnoreCase(text)){
74
      throws Exception {
76
			keyword.setText(text);
-
 
77
		}
75
    // Get the AdGroupCriterionService.
78
		if(type!=null && ("").equalsIgnoreCase(type)){
76
    AdGroupCriterionServiceInterface adGroupCriterionService =
-
 
77
        adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
79
			keyword.setMatchType(KeywordMatchType.fromString(type));
78
 
80
		}
-
 
81
		keyword.setId(keywordId);
79
    // Create ad group criterion with updated bid.
82
		// Create ad group criterion with updated bid.
80
    Criterion criterion = new Criterion();
83
		/*Criterion criterion = new Criterion();
81
    criterion.setId(keywordId);
84
		criterion.setId(keywordId);*/
82
 
85
		
83
    BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
86
		BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
84
    biddableAdGroupCriterion.setAdGroupId(adGroupId);
87
		biddableAdGroupCriterion.setAdGroupId(adGroupId);
85
    biddableAdGroupCriterion.setCriterion(criterion);
88
		biddableAdGroupCriterion.setCriterion(keyword);
86
 
89
		
-
 
90
		if(bidAmount > -1l){
87
    // Create bids.
91
			// Create bids.
88
    BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
92
			BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
89
    CpcBid bid = new CpcBid();
93
			CpcBid bid = new CpcBid();
90
    bid.setBid(new Money(null, 10000000L));
94
			bid.setBid(new Money(null, bidAmount));
91
    biddingStrategyConfiguration.setBids(new Bids[] {bid});
95
			biddingStrategyConfiguration.setBids(new Bids[] {bid});
92
    biddableAdGroupCriterion.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
96
			biddableAdGroupCriterion.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
93
 
97
		}
94
    // Create operations.
98
		// Create operations.
95
    AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
99
		AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
96
    operation.setOperand(biddableAdGroupCriterion);
100
		operation.setOperand(biddableAdGroupCriterion);
97
    operation.setOperator(Operator.SET);
101
		operation.setOperator(Operator.SET);
98
 
102
 
99
    AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] {operation};
103
		AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] {operation};
100
 
104
		
-
 
105
		if((text!=null && ("").equalsIgnoreCase(text)) || (type!=null && ("").equalsIgnoreCase(type)) || (bidAmount > -1l)){
101
    // Update ad group criteria.
106
			// Update ad group criteria.
102
    AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
107
			AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
103
 
108
			
-
 
109
			Long criterionId = 0l;
104
    // Display ad group criteria.
110
			// Display ad group criteria.
105
    for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
111
			for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
106
      if (adGroupCriterionResult instanceof BiddableAdGroupCriterion) {
112
				if (adGroupCriterionResult instanceof BiddableAdGroupCriterion) {
107
        biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterionResult;
113
					biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterionResult;
108
        System.out.println(
114
					System.out.println(
109
            "Ad group criterion with ad group id \""
115
							"Ad group criterion with ad group id \""
110
                + biddableAdGroupCriterion.getAdGroupId() + "\", criterion id \""
116
							+ biddableAdGroupCriterion.getAdGroupId() + "\", criterion id \""
111
                + biddableAdGroupCriterion.getCriterion().getId() + "\", type \""
117
							+ biddableAdGroupCriterion.getCriterion().getId() + "\", type \""
112
                + biddableAdGroupCriterion.getCriterion().getCriterionType()
118
							+ biddableAdGroupCriterion.getCriterion().getCriterionType()
113
                + "\", and bid \""
119
							+ "\", and bid \""
114
                + ((CpcBid) biddableAdGroupCriterion.getBiddingStrategyConfiguration()
120
							+ ((CpcBid) biddableAdGroupCriterion.getBiddingStrategyConfiguration()
115
                    .getBids()[0]).getBid().getMicroAmount() + "\" was updated.");
121
									.getBids()[0]).getBid().getMicroAmount() + "\" was updated.");
-
 
122
					criterionId = biddableAdGroupCriterion.getCriterion().getId();
116
      }
123
				}
117
    }
124
			}
-
 
125
			return criterionId;
118
  }
126
		}
-
 
127
		
-
 
128
		return 0l;
-
 
129
	}
119
}
130
}