| 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.accountmanagement;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201302.ch.AdGroupChangeData;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201302.ch.CampaignChangeData;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201302.ch.ChangeStatus;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201302.ch.CustomerChangeData;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201302.ch.CustomerSyncSelector;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201302.ch.CustomerSyncServiceInterface;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201302.cm.Campaign;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignPage;
|
|
|
26 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignServiceInterface;
|
|
|
27 |
import com.google.api.ads.adwords.axis.v201302.cm.DateTimeRange;
|
|
|
28 |
import com.google.api.ads.adwords.axis.v201302.cm.Selector;
|
|
|
29 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
30 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
31 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
32 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
33 |
|
|
|
34 |
import org.apache.commons.lang.ArrayUtils;
|
|
|
35 |
import org.apache.commons.lang.StringUtils;
|
|
|
36 |
|
|
|
37 |
import java.text.SimpleDateFormat;
|
|
|
38 |
import java.util.ArrayList;
|
|
|
39 |
import java.util.Date;
|
|
|
40 |
import java.util.List;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* This example gets the changes in the account during the last 24 hours.
|
|
|
44 |
*
|
|
|
45 |
* Tags: CustomerSyncService.get
|
|
|
46 |
*
|
|
|
47 |
* @author Kevin Winter
|
|
|
48 |
*/
|
|
|
49 |
public class GetAccountChanges {
|
|
|
50 |
|
|
|
51 |
public static void main(String[] args) throws Exception {
|
|
|
52 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
53 |
// and can be used in place of a service account.
|
|
|
54 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
55 |
.forApi(Api.ADWORDS)
|
|
|
56 |
.fromFile()
|
|
|
57 |
.build()
|
|
|
58 |
.generateCredential();
|
|
|
59 |
|
|
|
60 |
// Construct an AdWordsSession.
|
|
|
61 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
62 |
.fromFile()
|
|
|
63 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
64 |
.build();
|
|
|
65 |
|
|
|
66 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
67 |
|
|
|
68 |
runExample(adWordsServices, session);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public static void runExample(AdWordsServices adWordsServices, AdWordsSession session)
|
|
|
72 |
throws Exception {
|
|
|
73 |
// Get the CampaignService.
|
|
|
74 |
CampaignServiceInterface campaignService =
|
|
|
75 |
adWordsServices.get(session, CampaignServiceInterface.class);
|
|
|
76 |
|
|
|
77 |
// Get the CustomerSyncService.
|
|
|
78 |
CustomerSyncServiceInterface customerSyncService =
|
|
|
79 |
adWordsServices.get(session, CustomerSyncServiceInterface.class);
|
|
|
80 |
|
|
|
81 |
// Get a list of all campaign IDs.
|
|
|
82 |
List<Long> campaignIds = new ArrayList<Long>();
|
|
|
83 |
Selector selector = new Selector();
|
|
|
84 |
selector.setFields(new String[] {"Id"});
|
|
|
85 |
CampaignPage campaigns = campaignService.get(selector);
|
|
|
86 |
if (campaigns.getEntries() != null) {
|
|
|
87 |
for (Campaign campaign : campaigns.getEntries()) {
|
|
|
88 |
campaignIds.add(campaign.getId());
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// Create date time range for the past 24 hours.
|
|
|
93 |
DateTimeRange dateTimeRange = new DateTimeRange();
|
|
|
94 |
dateTimeRange.setMin(new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date(System
|
|
|
95 |
.currentTimeMillis() - 1000L * 60 * 60 * 24)));
|
|
|
96 |
dateTimeRange.setMax(new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date()));
|
|
|
97 |
|
|
|
98 |
// Create selector.
|
|
|
99 |
CustomerSyncSelector customerSyncSelector = new CustomerSyncSelector();
|
|
|
100 |
customerSyncSelector.setDateTimeRange(dateTimeRange);
|
|
|
101 |
customerSyncSelector
|
|
|
102 |
.setCampaignIds(ArrayUtils.toPrimitive(campaignIds.toArray(new Long[] {})));
|
|
|
103 |
|
|
|
104 |
// Get all account changes for campaign.
|
|
|
105 |
CustomerChangeData accountChanges = customerSyncService.get(customerSyncSelector);
|
|
|
106 |
|
|
|
107 |
// Display changes.
|
|
|
108 |
if (accountChanges != null && accountChanges.getChangedCampaigns() != null) {
|
|
|
109 |
System.out.println("Most recent change: "
|
|
|
110 |
+ accountChanges.getLastChangeTimestamp() + "\n");
|
|
|
111 |
for (CampaignChangeData campaignChanges : accountChanges.getChangedCampaigns()) {
|
|
|
112 |
System.out.println("Campaign with id \"" + campaignChanges.getCampaignId()
|
|
|
113 |
+ "\" was changed: ");
|
|
|
114 |
System.out.println("\tCampaign changed status: "
|
|
|
115 |
+ campaignChanges.getCampaignChangeStatus());
|
|
|
116 |
if (campaignChanges.getCampaignChangeStatus() != ChangeStatus.NEW) {
|
|
|
117 |
System.out.println("\tAdded ad extensions: "
|
|
|
118 |
+ getFormattedList(campaignChanges.getAddedAdExtensions()));
|
|
|
119 |
System.out.println("\tAdded campaign criteria: "
|
|
|
120 |
+ getFormattedList(campaignChanges.getAddedCampaignCriteria()));
|
|
|
121 |
System.out.println("\tAdded campaign targeting: "
|
|
|
122 |
+ campaignChanges.getCampaignTargetingChanged());
|
|
|
123 |
System.out.println("\tDeleted ad extensions: "
|
|
|
124 |
+ getFormattedList(campaignChanges.getDeletedAdExtensions()));
|
|
|
125 |
System.out.println("\tDeleted campaign criteria: "
|
|
|
126 |
+ getFormattedList(campaignChanges.getDeletedCampaignCriteria()));
|
|
|
127 |
|
|
|
128 |
if (campaignChanges.getChangedAdGroups() != null) {
|
|
|
129 |
for (AdGroupChangeData adGroupChanges : campaignChanges.getChangedAdGroups()) {
|
|
|
130 |
System.out.println("\tAd goup with id \"" + adGroupChanges.getAdGroupId()
|
|
|
131 |
+ "\" was changed: ");
|
|
|
132 |
System.out.println("\t\tAd goup changed status: "
|
|
|
133 |
+ adGroupChanges.getAdGroupChangeStatus());
|
|
|
134 |
if (adGroupChanges.getAdGroupChangeStatus() != ChangeStatus.NEW) {
|
|
|
135 |
System.out.println("\t\tAds changed: "
|
|
|
136 |
+ getFormattedList(adGroupChanges.getChangedAds()));
|
|
|
137 |
System.out.println("\t\tCriteria changed: "
|
|
|
138 |
+ getFormattedList(adGroupChanges.getChangedCriteria()));
|
|
|
139 |
System.out.println("\t\tCriteria deleted: "
|
|
|
140 |
+ getFormattedList(adGroupChanges.getDeletedCriteria()));
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
System.out.println("");
|
|
|
146 |
}
|
|
|
147 |
} else {
|
|
|
148 |
System.out.println("No account changes were found.");
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Gets a formatted list of a long array in the form {1,2,3}.
|
|
|
154 |
* @param idList the long array
|
|
|
155 |
* @return the formatted list
|
|
|
156 |
*/
|
|
|
157 |
private static String getFormattedList(long[] idList) {
|
|
|
158 |
if (idList == null) {
|
|
|
159 |
idList = new long[]{};
|
|
|
160 |
}
|
|
|
161 |
return new StringBuilder().append("{")
|
|
|
162 |
.append(StringUtils.join(ArrayUtils.toObject(idList), ','))
|
|
|
163 |
.append("}").toString();
|
|
|
164 |
}
|
|
|
165 |
}
|