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