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.remarketing;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201302.cm.AdWordsConversionTracker;
19
import com.google.api.ads.adwords.axis.v201302.cm.ConversionTracker;
20
import com.google.api.ads.adwords.axis.v201302.cm.ConversionTrackerPage;
21
import com.google.api.ads.adwords.axis.v201302.cm.ConversionTrackerServiceInterface;
22
import com.google.api.ads.adwords.axis.v201302.cm.Operator;
23
import com.google.api.ads.adwords.axis.v201302.cm.Predicate;
24
import com.google.api.ads.adwords.axis.v201302.cm.PredicateOperator;
25
import com.google.api.ads.adwords.axis.v201302.cm.RemarketingUserList;
26
import com.google.api.ads.adwords.axis.v201302.cm.Selector;
27
import com.google.api.ads.adwords.axis.v201302.cm.UserList;
28
import com.google.api.ads.adwords.axis.v201302.cm.UserListConversionType;
29
import com.google.api.ads.adwords.axis.v201302.cm.UserListMembershipStatus;
30
import com.google.api.ads.adwords.axis.v201302.cm.UserListOperation;
31
import com.google.api.ads.adwords.axis.v201302.cm.UserListReturnValue;
32
import com.google.api.ads.adwords.axis.v201302.cm.UserListServiceInterface;
33
import com.google.api.ads.adwords.lib.client.AdWordsSession;
34
import com.google.api.ads.common.lib.auth.OfflineCredentials;
35
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
36
import com.google.api.client.auth.oauth2.Credential;
37
 
38
import java.util.ArrayList;
39
import java.util.HashMap;
40
import java.util.List;
41
import java.util.Map;
42
 
43
/**
44
 * This example adds a remarketing user list (a.k.a. audience).
45
 *
46
 * Credentials and properties in {@code fromFile()} are pulled from the
47
 * "ads.properties" file. See README for more info.
48
 *
49
 * Tags: UserListService.mutate
50
 *
51
 * @author Kevin Winter
52
 */
53
public class AddAudience {
54
 
55
  public static void main(String[] args) throws Exception {
56
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
57
    // and can be used in place of a service account.
58
    Credential oAuth2Credential = new OfflineCredentials.Builder()
59
        .forApi(Api.ADWORDS)
60
        .fromFile()
61
        .build()
62
        .generateCredential();
63
 
64
    // Construct an AdWordsSession.
65
    AdWordsSession session = new AdWordsSession.Builder()
66
        .fromFile()
67
        .withOAuth2Credential(oAuth2Credential)
68
        .build();
69
 
70
    AdWordsServices adWordsServices = new AdWordsServices();
71
 
72
    runExample(adWordsServices, session);
73
  }
74
 
75
  public static void runExample(
76
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
77
    // Get the UserListService.
78
    UserListServiceInterface userListService =
79
        adWordsServices.get(session, UserListServiceInterface.class);
80
 
81
    // Get the ConversionTrackerService.
82
    ConversionTrackerServiceInterface conversionTrackerService =
83
        adWordsServices.get(session, ConversionTrackerServiceInterface.class);
84
 
85
    // Create conversion type (tag).
86
    UserListConversionType conversionType = new UserListConversionType();
87
    conversionType.setName("Mars cruise customers #" + System.currentTimeMillis());
88
 
89
    // Create remarketing user list.
90
    RemarketingUserList userList = new RemarketingUserList();
91
    userList.setName("Mars cruise customers #" + System.currentTimeMillis());
92
    userList.setDescription("A list of mars cruise customers in the last year");
93
    userList.setMembershipLifeSpan(365L);
94
    userList.setConversionTypes(new UserListConversionType[] {conversionType});
95
 
96
    // You can optionally provide these field(s).
97
    userList.setStatus(UserListMembershipStatus.OPEN);
98
 
99
    // Create operations.
100
    UserListOperation operation = new UserListOperation();
101
    operation.setOperand(userList);
102
    operation.setOperator(Operator.ADD);
103
 
104
    UserListOperation[] operations = new UserListOperation[] {operation};
105
 
106
    // Add user list.
107
    UserListReturnValue result = userListService.mutate(operations);
108
 
109
    // Display results.
110
    // Capture the ID(s) of the conversion.
111
    List<String> conversionIds = new ArrayList<String>();
112
    for (UserList userListResult : result.getValue()) {
113
      if (userListResult instanceof RemarketingUserList) {
114
        RemarketingUserList remarketingUserList = (RemarketingUserList) userListResult;
115
        for (UserListConversionType userListConversionType : remarketingUserList
116
            .getConversionTypes()) {
117
          conversionIds.add(userListConversionType.getId().toString());
118
        }
119
      }
120
    }
121
 
122
    // Create predicate and selector.
123
    Predicate predicate = new Predicate();
124
    predicate.setField("Id");
125
    predicate.setOperator(PredicateOperator.IN);
126
    predicate.setValues(conversionIds.toArray(new String[0]));
127
    Selector selector = new Selector();
128
    selector.setFields(new String[] {"Id"});
129
    selector.setPredicates(new Predicate[] {predicate});
130
 
131
    // Get all conversion trackers.
132
    Map<Long, AdWordsConversionTracker> conversionTrackers =
133
        new HashMap<Long, AdWordsConversionTracker>();
134
    ConversionTrackerPage page = conversionTrackerService.get(selector);
135
    if (page != null && page.getEntries() != null) {
136
      for (ConversionTracker conversionTracker : page.getEntries()) {
137
        conversionTrackers.put(conversionTracker.getId(),
138
            (AdWordsConversionTracker) conversionTracker);
139
      }
140
    }
141
 
142
    // Display user lists.
143
    for (UserList userListResult : result.getValue()) {
144
      System.out.printf("User list with name '%s' and id '%d' was added.\n",
145
          userListResult.getName(), userListResult.getId());
146
 
147
      // Display user list associated conversion code snippets.
148
      if (userListResult instanceof RemarketingUserList) {
149
        RemarketingUserList remarketingUserList = (RemarketingUserList) userListResult;
150
        for (UserListConversionType userListConversionType : remarketingUserList
151
            .getConversionTypes()) {
152
          System.out.printf("Conversion type code snippet associated to the list:\n%s\n",
153
              conversionTrackers.get(userListConversionType.getId()).getSnippet());
154
        }
155
      }
156
    }
157
  }
158
}