Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9005 manish.sha 1
// Copyright 2012 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.v201302.advancedoperations;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupAd;
19
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupAdOperation;
20
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupAdReturnValue;
21
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupAdServiceInterface;
22
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupAdStatus;
23
import com.google.api.ads.adwords.axis.v201302.cm.Operator;
24
import com.google.api.ads.adwords.axis.v201302.cm.TemplateAd;
25
import com.google.api.ads.adwords.axis.v201302.cm.TemplateElement;
26
import com.google.api.ads.adwords.axis.v201302.cm.TemplateElementField;
27
import com.google.api.ads.adwords.axis.v201302.cm.TemplateElementFieldType;
28
import com.google.api.ads.adwords.lib.client.AdWordsSession;
29
import com.google.api.ads.common.lib.auth.OfflineCredentials;
30
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
31
import com.google.api.client.auth.oauth2.Credential;
32
 
33
/**
34
 * This example adds a click-to-download templateAd to given ad group. To get
35
 * adGroupId, run AddAdGroup.java.
36
 *
37
 * Credentials and properties in {@code fromFile()} are pulled from the
38
 * "ads.properties" file. See README for more info.
39
 *
40
 * Tags: AdGroupAdService.mutate
41
 *
42
 * Category: adx-exclude
43
 *
44
 * @author Kevin Winter
45
 */
46
public class AddClickToDownloadAd {
47
 
48
  public static void main(String[] args) throws Exception {
49
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
50
    // and can be used in place of a service account.
51
    Credential oAuth2Credential = new OfflineCredentials.Builder()
52
        .forApi(Api.ADWORDS)
53
        .fromFile()
54
        .build()
55
        .generateCredential();
56
 
57
    // Construct an AdWordsSession.
58
    AdWordsSession session = new AdWordsSession.Builder()
59
        .fromFile()
60
        .withOAuth2Credential(oAuth2Credential)
61
        .build();
62
 
63
    long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
64
 
65
    AdWordsServices adWordsServices = new AdWordsServices();
66
 
67
    runExample(adWordsServices, session, adGroupId);
68
  }
69
 
70
  public static void runExample(
71
      AdWordsServices adWordsServices, AdWordsSession session, long adGroupId) throws Exception {
72
    // Get the AdGroupAdService.
73
    AdGroupAdServiceInterface adGroupAdService =
74
        adWordsServices.get(session, AdGroupAdServiceInterface.class);
75
 
76
    // Create the template ad.
77
    TemplateAd clickToDownloadAppAd = new TemplateAd();
78
    clickToDownloadAppAd.setName("Ad for jupiter adventure game");
79
    clickToDownloadAppAd.setTemplateId(353L);
80
 
81
    clickToDownloadAppAd.setUrl(
82
        "http://play.google.com/store/apps/details?id=com.example.jupiteradventure");
83
    clickToDownloadAppAd.setDisplayUrl("play.google.com");
84
 
85
    // Create the template elements for the ad. You can refer to
86
    // https://developers.google.com/adwords/api/docs/appendix/templateads
87
    // for the list of available template fields.
88
    TemplateElementField headline = new TemplateElementField();
89
    headline.setName("headline");
90
    headline.setFieldText("Enjoy a Jupiter Adventure");
91
    headline.setType(TemplateElementFieldType.TEXT);
92
 
93
    TemplateElementField description1 = new TemplateElementField();
94
    description1.setName("description1");
95
    description1.setFieldText("Realistic physics simulation");
96
    description1.setType(TemplateElementFieldType.TEXT);
97
 
98
    TemplateElementField description2 = new TemplateElementField();
99
    description2.setName("description2");
100
    description2.setFieldText("Race against players online");
101
    description2.setType(TemplateElementFieldType.TEXT);
102
 
103
    TemplateElementField appId = new TemplateElementField();
104
    appId.setName("appId");
105
    appId.setFieldText("com.example.jupiteradventure");
106
    appId.setType(TemplateElementFieldType.TEXT);
107
 
108
    TemplateElementField appStore = new TemplateElementField();
109
    appStore.setName("appStore");
110
    appStore.setFieldText("2");
111
    appStore.setType(TemplateElementFieldType.ENUM);
112
 
113
    TemplateElement adData = new TemplateElement();
114
    adData.setUniqueName("adData");
115
    adData.setFields(new TemplateElementField[] {headline, description1, description2, appId,
116
        appStore});
117
 
118
    clickToDownloadAppAd.setTemplateElements(new TemplateElement[] {adData});
119
 
120
    // Create the AdGroupAd.
121
    AdGroupAd clickToDownloadAppAdGroupAd = new AdGroupAd();
122
    clickToDownloadAppAdGroupAd.setAdGroupId(adGroupId);
123
    clickToDownloadAppAdGroupAd.setAd(clickToDownloadAppAd);
124
 
125
    // Optional: Set the status.
126
    clickToDownloadAppAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
127
 
128
    // Create the operation.
129
    AdGroupAdOperation operation = new AdGroupAdOperation();
130
    operation.setOperator(Operator.ADD);
131
    operation.setOperand(clickToDownloadAppAdGroupAd);
132
 
133
    // Create the ads.
134
    AdGroupAdReturnValue result = adGroupAdService.mutate(new AdGroupAdOperation[] {operation});
135
 
136
    for (AdGroupAd adGroupAd : result.getValue()) {
137
      System.out.printf("New click-to-download ad with id = \"%d\" and url = \"%s\" " +
138
          "was created.", adGroupAd.getAd().getId(), adGroupAd.getAd().getUrl());
139
    }
140
  }
141
}