Skip to content

Commit 12661e6

Browse files
authored
Merge pull request #36 from googleads/release-v0_5-d70c6cf9cbeb136e09d1
Changes for release v0_5.
2 parents d33754b + 027d525 commit 12661e6

480 files changed

Lines changed: 122494 additions & 7171 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Copyright 2018 Google LLC
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+
// https://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 com.google.ads.googleads.examples.accountmanagement;
16+
17+
import com.beust.jcommander.Parameter;
18+
import com.google.ads.googleads.examples.utils.ArgumentNames;
19+
import com.google.ads.googleads.examples.utils.CodeSampleParams;
20+
import com.google.ads.googleads.lib.GoogleAdsClient;
21+
import com.google.ads.googleads.lib.GoogleAdsException;
22+
import com.google.ads.googleads.v0.errors.GoogleAdsError;
23+
import com.google.ads.googleads.v0.resources.ChangeStatus;
24+
import com.google.ads.googleads.v0.services.GoogleAdsRow;
25+
import com.google.ads.googleads.v0.services.GoogleAdsServiceClient;
26+
import com.google.ads.googleads.v0.services.GoogleAdsServiceClient.SearchPagedResponse;
27+
import java.io.FileNotFoundException;
28+
import java.io.IOException;
29+
import java.util.Optional;
30+
31+
/** This example gets the changes in the account made in the last 7 days. */
32+
public class GetAccountChanges {
33+
34+
private static class GetAccountChangesParams extends CodeSampleParams {
35+
36+
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
37+
private Long customerId;
38+
}
39+
40+
public static void main(String[] args) {
41+
GetAccountChangesParams params = new GetAccountChangesParams();
42+
if (!params.parseArguments(args)) {
43+
44+
// Either pass the required parameters for this example on the command line, or insert them
45+
// into the code here. See the parameter class definition above for descriptions.
46+
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
47+
}
48+
49+
GoogleAdsClient googleAdsClient;
50+
try {
51+
googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
52+
} catch (FileNotFoundException fnfe) {
53+
System.err.printf(
54+
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
55+
return;
56+
} catch (IOException ioe) {
57+
System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
58+
return;
59+
}
60+
61+
try {
62+
new GetAccountChanges().runExample(googleAdsClient, params.customerId);
63+
} catch (GoogleAdsException gae) {
64+
// GoogleAdsException is the base class for most exceptions thrown by an API request.
65+
// Instances of this exception have a message and a GoogleAdsFailure that contains a
66+
// collection of GoogleAdsErrors that indicate the underlying causes of the
67+
// GoogleAdsException.
68+
System.err.printf(
69+
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
70+
gae.getRequestId());
71+
int i = 0;
72+
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
73+
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
74+
}
75+
}
76+
}
77+
78+
/**
79+
* Runs the example.
80+
*
81+
* @param googleAdsClient the client instance.
82+
* @param customerId the customerId for which to retrieve change status.
83+
*/
84+
private void runExample(GoogleAdsClient googleAdsClient, long customerId) {
85+
String query =
86+
"SELECT change_status.resource_name, "
87+
+ "change_status.last_change_date_time, "
88+
+ "change_status.resource_type, "
89+
+ "change_status.campaign, "
90+
+ "change_status.ad_group, "
91+
+ "change_status.resource_status, "
92+
+ "change_status.ad_group_ad, "
93+
+ "change_status.ad_group_criterion, "
94+
+ "change_status.campaign_criterion "
95+
+ "FROM change_status "
96+
+ "WHERE change_status.last_change_date_time DURING LAST_7_DAYS "
97+
+ "ORDER BY change_status.last_change_date_time";
98+
99+
try (GoogleAdsServiceClient client = googleAdsClient.getGoogleAdsServiceClient()) {
100+
SearchPagedResponse response = client.search(String.valueOf(customerId), query);
101+
102+
for (GoogleAdsRow row : response.iterateAll()) {
103+
Optional<String> resourceNameOfChangedEntity =
104+
getResourceNameForResourceType(row.getChangeStatus());
105+
106+
System.out.printf(
107+
"On '%s', change status '%s' shows a resource type of '%s' "
108+
+ "with resource name '%s' was '%s'.%n",
109+
row.getChangeStatus().getLastChangeDateTime().getValue(),
110+
row.getChangeStatus().getResourceName(),
111+
row.getChangeStatus().getResourceType().name(),
112+
resourceNameOfChangedEntity.orElse(""),
113+
row.getChangeStatus().getResourceStatus().name());
114+
}
115+
}
116+
}
117+
118+
/**
119+
* Each returned row contains all possible changed fields. This function returns the resource name
120+
* of the changed field based on the resource type. The changed field's parent is also populated
121+
* but is not used.
122+
*
123+
* @param changeStatus the change status for which to get affected resource name.
124+
* @return an Optional has a value when one could be obtained for the change resource type.
125+
*/
126+
private static Optional<String> getResourceNameForResourceType(ChangeStatus changeStatus) {
127+
String resourceName = null;
128+
// This is the list of all known resource names but may be subject to change in the future.
129+
// See https://developers.google.com/google-ads/api/docs/change-status for a description.
130+
switch (changeStatus.getResourceType()) {
131+
case AD_GROUP:
132+
resourceName = changeStatus.getAdGroup().getValue();
133+
break;
134+
case AD_GROUP_AD:
135+
resourceName = changeStatus.getAdGroupAd().getValue();
136+
break;
137+
case AD_GROUP_CRITERION:
138+
resourceName = changeStatus.getAdGroup().getValue();
139+
break;
140+
case CAMPAIGN:
141+
resourceName = changeStatus.getCampaign().getValue();
142+
break;
143+
case CAMPAIGN_CRITERION:
144+
resourceName = changeStatus.getCampaignCriterion().getValue();
145+
break;
146+
}
147+
return Optional.ofNullable(resourceName);
148+
}
149+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright 2018 Google LLC
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+
// https://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+
package com.google.ads.googleads.examples.billing;
15+
16+
import com.beust.jcommander.Parameter;
17+
import com.google.ads.googleads.examples.utils.ArgumentNames;
18+
import com.google.ads.googleads.examples.utils.CodeSampleParams;
19+
import com.google.ads.googleads.lib.GoogleAdsClient;
20+
import com.google.ads.googleads.lib.GoogleAdsException;
21+
import com.google.ads.googleads.v0.errors.GoogleAdsError;
22+
import com.google.ads.googleads.v0.resources.AccountBudget;
23+
import com.google.ads.googleads.v0.services.GoogleAdsRow;
24+
import com.google.ads.googleads.v0.services.GoogleAdsServiceClient;
25+
import com.google.ads.googleads.v0.services.GoogleAdsServiceClient.SearchPagedResponse;
26+
import java.io.FileNotFoundException;
27+
import java.io.IOException;
28+
29+
/** This example retrieves all account budgets for a Google Ads customer. */
30+
public class GetAccountBudgets {
31+
32+
private static class GetAccountBudgetParams extends CodeSampleParams {
33+
34+
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
35+
private Long customerId;
36+
}
37+
38+
public static void main(String[] args) {
39+
GetAccountBudgetParams params = new GetAccountBudgetParams();
40+
if (!params.parseArguments(args)) {
41+
42+
// Either pass the required parameters for this example on the command line, or insert them
43+
// into the code here. See the parameter class definition above for descriptions.
44+
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
45+
}
46+
47+
GoogleAdsClient googleAdsClient;
48+
try {
49+
googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
50+
} catch (FileNotFoundException fnfe) {
51+
System.err.printf(
52+
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
53+
return;
54+
} catch (IOException ioe) {
55+
System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
56+
return;
57+
}
58+
59+
try {
60+
new GetAccountBudgets().runExample(googleAdsClient, params.customerId);
61+
} catch (GoogleAdsException gae) {
62+
// GoogleAdsException is the base class for most exceptions thrown by an API request.
63+
// Instances of this exception have a message and a GoogleAdsFailure that contains a
64+
// collection of GoogleAdsErrors that indicate the underlying causes of the
65+
// GoogleAdsException.
66+
System.err.printf(
67+
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
68+
gae.getRequestId());
69+
int i = 0;
70+
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
71+
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
72+
}
73+
}
74+
}
75+
76+
/**
77+
* Runs the example.
78+
*
79+
* @param googleAdsClient the Google Ads API client.
80+
* @param customerId the customer ID for which to retrieve account budgets.
81+
*/
82+
private void runExample(GoogleAdsClient googleAdsClient, long customerId) {
83+
String query =
84+
"SELECT "
85+
+ "account_budget.status, "
86+
+ "account_budget.billing_setup, "
87+
+ "account_budget.approved_spending_limit_micros, "
88+
+ "account_budget.approved_spending_limit_type, "
89+
+ "account_budget.proposed_spending_limit_micros, "
90+
+ "account_budget.proposed_spending_limit_type, "
91+
+ "account_budget.approved_start_date_time, "
92+
+ "account_budget.proposed_start_date_time, "
93+
+ "account_budget.approved_end_date_time, "
94+
+ "account_budget.approved_end_time_type,"
95+
+ "account_budget.proposed_end_date_time, "
96+
+ "account_budget.proposed_end_time_type "
97+
+ "FROM account_budget";
98+
try (GoogleAdsServiceClient googleAdsServiceClient =
99+
googleAdsClient.getGoogleAdsServiceClient()) {
100+
// Issues the search request.
101+
SearchPagedResponse searchPagedResponse =
102+
googleAdsServiceClient.search(Long.toString(customerId), query);
103+
104+
// Iterates over all rows in all pages and prints the requested field values for the account
105+
// budget in each row.
106+
for (GoogleAdsRow googleAdsRow : searchPagedResponse.iterateAll()) {
107+
AccountBudget accountBudget = googleAdsRow.getAccountBudget();
108+
109+
System.out.printf(
110+
"Account budget '%s' with "
111+
+ "status '%s' "
112+
+ "billing setup '%s' "
113+
+ "amount served %.2f "
114+
+ "total adjustments %.2f "
115+
+ "approved spending limit '%s' "
116+
+ "(proposed '%s') "
117+
+ "approved start time '%s' "
118+
+ "(proposed '%s') "
119+
+ "approved end time '%s' "
120+
+ "(proposed '%s')"
121+
+ ".%n",
122+
accountBudget.getResourceName(),
123+
accountBudget.getStatus(),
124+
accountBudget.getBillingSetup().getValue(),
125+
accountBudget.getAmountServedMicros().getValue() / 1_000_000.0,
126+
accountBudget.getTotalAdjustmentsMicros().getValue() / 1_000_000.0,
127+
accountBudget.hasApprovedSpendingLimitMicros()
128+
? String.format(
129+
"%.2f", accountBudget.getApprovedSpendingLimitMicros().getValue() / 1_000_000.0)
130+
: accountBudget.getApprovedSpendingLimitType().name(),
131+
accountBudget.hasProposedSpendingLimitMicros()
132+
? String.format(
133+
"%.2f", accountBudget.getProposedSpendingLimitMicros().getValue() / 1_000_000.0)
134+
: accountBudget.getProposedSpendingLimitType().name(),
135+
accountBudget.getApprovedStartDateTime().getValue(),
136+
accountBudget.getProposedStartDateTime().getValue(),
137+
accountBudget.hasApprovedEndDateTime()
138+
? accountBudget.getApprovedEndDateTime().getValue()
139+
: accountBudget.getApprovedEndTimeType(),
140+
accountBudget.hasProposedEndDateTime()
141+
? accountBudget.getProposedEndDateTime().getValue()
142+
: accountBudget.getProposedEndTimeType());
143+
}
144+
}
145+
}
146+
}

google-ads-examples/src/main/java/com/google/ads/googleads/examples/billing/GetBillingSetup.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ private void runExample(GoogleAdsClient googleAdsClient, long customerId) {
8888
// Define a GAQL query to retrieve all billing setup information.
8989
String searchQuery = "SELECT billing_setup.id, "
9090
+ " billing_setup.status, "
91-
+ " billing_setup.payments_account_id, "
92-
+ " billing_setup.payments_account_name, "
93-
+ " billing_setup.payments_profile_id, "
94-
+ " billing_setup.payments_profile_name, "
95-
+ " billing_setup.secondary_payments_profile_id "
91+
+ " billing_setup.payments_account, "
92+
+ " billing_setup.payments_account_info.payments_account_id, "
93+
+ " billing_setup.payments_account_info.payments_account_name, "
94+
+ " billing_setup.payments_account_info.payments_profile_id, "
95+
+ " billing_setup.payments_account_info.payments_profile_name, "
96+
+ " billing_setup.payments_account_info.secondary_payments_profile_id "
9697
+ "FROM billing_setup";
9798

9899
// Creates a request that will retrieve all billing setups using pages of the specified
@@ -113,18 +114,20 @@ private void runExample(GoogleAdsClient googleAdsClient, long customerId) {
113114
System.out.printf(
114115
"Billing setup with ID '%d', "
115116
+ "status '%s', "
117+
+ "payments_account '%s', "
116118
+ "payments_account_id '%s', "
117119
+ "payments_account_name '%s', "
118120
+ "payments_profile_id '%s', "
119121
+ "payments_profile_name '%s', "
120122
+ "secondary_payments_profile_id '%s'.%n",
121123
billingSetup.getId().getValue(),
122124
billingSetup.getStatus(),
123-
billingSetup.getPaymentsAccountId().getValue(),
124-
billingSetup.getPaymentsAccountName().getValue(),
125-
billingSetup.getPaymentsProfileId().getValue(),
126-
billingSetup.getPaymentsProfileName().getValue(),
127-
billingSetup.getSecondaryPaymentsProfileId().getValue());
125+
billingSetup.getPaymentsAccount().getValue(),
126+
billingSetup.getPaymentsAccountInfo().getPaymentsAccountId().getValue(),
127+
billingSetup.getPaymentsAccountInfo().getPaymentsAccountName().getValue(),
128+
billingSetup.getPaymentsAccountInfo().getPaymentsProfileId().getValue(),
129+
billingSetup.getPaymentsAccountInfo().getPaymentsProfileName().getValue(),
130+
billingSetup.getPaymentsAccountInfo().getSecondaryPaymentsProfileId().getValue());
128131
}
129132
}
130133
}

0 commit comments

Comments
 (0)