Skip to content

Commit 7dda56b

Browse files
authored
Add params for uploading offline conversion by GBRAID or WBRAID (#582)
1 parent e91f6ab commit 7dda56b

2 files changed

Lines changed: 70 additions & 6 deletions

File tree

google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadOfflineConversion.java

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.ads.googleads.v10.utils.ResourceNames;
3232
import java.io.FileNotFoundException;
3333
import java.io.IOException;
34+
import java.util.Arrays;
3435

3536
/** Imports offline conversion values for specific clicks to an account. */
3637
public class UploadOfflineConversion {
@@ -42,9 +43,39 @@ private static class UploadOfflineConversionParams extends CodeSampleParams {
4243
@Parameter(names = ArgumentNames.CONVERSION_ACTION_ID, required = true)
4344
private long conversionActionId;
4445

45-
@Parameter(names = ArgumentNames.GCLID, required = true)
46+
@Parameter(
47+
names = ArgumentNames.GCLID,
48+
required = false,
49+
description =
50+
"The Google Click identifier. If setting this value, do not set "
51+
+ ArgumentNames.GBRAID
52+
+ " or "
53+
+ ArgumentNames.WBRAID
54+
+ ".")
4655
private String gclid;
4756

57+
@Parameter(
58+
names = ArgumentNames.GBRAID,
59+
required = false,
60+
description =
61+
"The GBRAID identifier for an iOS app conversion. If setting this value, do not set "
62+
+ ArgumentNames.GCLID
63+
+ " or "
64+
+ ArgumentNames.WBRAID
65+
+ ".")
66+
private String gbraid;
67+
68+
@Parameter(
69+
names = ArgumentNames.WBRAID,
70+
required = false,
71+
description =
72+
"The WBRAID identifer for an iOS web conversion. If setting this value, do not set "
73+
+ ArgumentNames.GCLID
74+
+ " or "
75+
+ ArgumentNames.GBRAID
76+
+ ".")
77+
private String wbraid;
78+
4879
@Parameter(
4980
names = ArgumentNames.CONVERSION_DATE_TIME,
5081
required = true,
@@ -78,7 +109,10 @@ public static void main(String[] args) {
78109
// into the code here. See the parameter class definition above for descriptions.
79110
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
80111
params.conversionActionId = Long.parseLong("INSERT_CONVERSION_ACTION_ID_HERE");
81-
params.gclid = "INSERT_GCL_ID_HERE";
112+
// Set exactly one of gclid, gbraid, or wbraid.
113+
params.gclid = "INSERT_GCLID_HERE";
114+
params.gbraid = null;
115+
params.wbraid = null;
82116
params.conversionDateTime = "INSERT_CONVERSION_DATE_TIME_HERE";
83117
params.conversionValue = Double.parseDouble("INSERT_CONVERSION_VALUE_HERE");
84118
// Optionally specify the conversion custom variable ID and value you want to
@@ -108,6 +142,8 @@ public static void main(String[] args) {
108142
params.customerId,
109143
params.conversionActionId,
110144
params.gclid,
145+
params.gbraid,
146+
params.wbraid,
111147
params.conversionDateTime,
112148
params.conversionValue,
113149
params.conversionCustomVariableId,
@@ -135,7 +171,12 @@ public static void main(String[] args) {
135171
* @param googleAdsClient the Google Ads API client.
136172
* @param customerId the client customer ID.
137173
* @param conversionActionId conversion action ID associated with this conversion.
138-
* @param gclid the GCLID for the conversion.
174+
* @param gclid the GCLID for the conversion. If set, {@code gbraid} and {@code wbraid} must be
175+
* null.
176+
* @param gbraid the GBRAID for the iOS app conversion. If set, {@code gclid} and {@code wbraid}
177+
* must be null.
178+
* @param wbraid the WBRAID for the iOS web conversion. If set, {@code gclid} and {@code gbraid}
179+
* must be null.
139180
* @param conversionDateTime date and time of the conversion.
140181
* @param conversionValue the value of the conversion.
141182
* @param conversionCustomVariableId the ID of the conversion custom variable to associate with
@@ -150,12 +191,25 @@ private void runExample(
150191
long customerId,
151192
long conversionActionId,
152193
String gclid,
194+
String gbraid,
195+
String wbraid,
153196
String conversionDateTime,
154197
Double conversionValue,
155198
Long conversionCustomVariableId,
156199
String conversionCustomVariableValue,
157200
String orderId) {
158-
// Gets the conversion action resource name.
201+
// Verifies that exactly one of gclid, gbraid, and wbraid is specified, as required.
202+
// See https://developers.google.com/google-ads/api/docs/conversions/upload-clicks for details.
203+
long numberOfIdsSpecified =
204+
Arrays.asList(gclid, gbraid, wbraid).stream().filter(idField -> idField != null).count();
205+
if (numberOfIdsSpecified != 1) {
206+
throw new IllegalArgumentException(
207+
"Exactly 1 of gclid, gbraid, or wbraid is required, but "
208+
+ numberOfIdsSpecified
209+
+ " ID values were provided");
210+
}
211+
212+
// Constructs the conversion action resource name from the customer and conversion action IDs.
159213
String conversionActionResourceName =
160214
ResourceNames.conversionAction(customerId, conversionActionId);
161215

@@ -165,8 +219,16 @@ private void runExample(
165219
.setConversionAction(conversionActionResourceName)
166220
.setConversionDateTime(conversionDateTime)
167221
.setConversionValue(conversionValue)
168-
.setCurrencyCode("USD")
169-
.setGclid(gclid);
222+
.setCurrencyCode("USD");
223+
224+
// Sets the single specified ID field.
225+
if (gclid != null) {
226+
clickConversionBuilder.setGclid(gclid);
227+
} else if (gbraid != null) {
228+
clickConversionBuilder.setGbraid(gbraid);
229+
} else {
230+
clickConversionBuilder.setWbraid(wbraid);
231+
}
170232

171233
if (conversionCustomVariableId != null && conversionCustomVariableValue != null) {
172234
// Sets the custom variable and value, if provided.

google-ads-examples/src/main/java/com/google/ads/googleads/examples/utils/ArgumentNames.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public final class ArgumentNames {
7171
public static final String FINAL_URL = "--finalUrl";
7272
public static final String FLIGHT_PLACEHOLDER_FIELD_NAME = "--flightPlaceholderFieldName";
7373
public static final String FREEFORM_KEYWORD_TEXT = "--freeformKeywordText";
74+
public static final String GBRAID = "--gbraid";
7475
public static final String GCLID = "--gclid";
7576
public static final String GEO_TARGET_CONSTANT_ID = "--geoTargetConstantId";
7677
public static final String BUSINESS_PROFILE_ACCESS_TOKEN = "--businessProfileAccessToken";
@@ -112,4 +113,5 @@ public final class ArgumentNames {
112113
public static final String USER_AGENT = "--userAgent";
113114
public static final String USER_LIST_ID = "--userListId";
114115
public static final String USER_LIST_IDS = "--userListIds";
116+
public static final String WBRAID = "--wbraid";
115117
}

0 commit comments

Comments
 (0)