Skip to content

Commit

Permalink
Merge pull request #720 from Iterable/evan/MOB-8226-add-attribution-i…
Browse files Browse the repository at this point in the history
…nfo-to-track-purchase

[MOB-8226] add attribution info to track purchase
  • Loading branch information
evantk91 committed Apr 10, 2024
2 parents 0341b1d + f925e44 commit e95295f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void testTrackPurchaseWithDataFields() throws Exception {
JSONObject dataFields = new JSONObject();
dataFields.put("field", "testValue");

IterableApi.sharedInstance.trackPurchase(100.0, items, dataFields);
IterableApi.sharedInstance.trackPurchase(100.0, items, dataFields, null);

RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS);
assertNotNull(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;

/**
* Represents a product. These are used by the commerce API; see {@link IterableApi#trackPurchase(double, List, JSONObject)}
* Represents a product. These are used by the commerce API; see {@link IterableApi#trackPurchase(double, List, JSONObject, IterableAttributionInfo)}
*/
public class CommerceItem {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ public void updateCart(@NonNull List<CommerceItem> items) {
* @param items list of purchased items
*/
public void trackPurchase(double total, @NonNull List<CommerceItem> items) {
trackPurchase(total, items, null);
trackPurchase(total, items, null, null);
}

/**
Expand All @@ -1040,7 +1040,22 @@ public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Null
return;
}

apiClient.trackPurchase(total, items, dataFields);
apiClient.trackPurchase(total, items, dataFields, null);
}

/**
* Tracks a purchase.
* @param total total purchase amount
* @param items list of purchased items
* @param dataFields a `JSONObject` containing any additional information to save along with the event
* @param attributionInfo a `JSONObject` containing information about what the purchase was attributed to
*/
public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Nullable JSONObject dataFields, @Nullable IterableAttributionInfo attributionInfo) {
if (!checkSDKInitialization()) {
return;
}

apiClient.trackPurchase(total, items, dataFields, attributionInfo);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void updateCart(@NonNull List<CommerceItem> items) {
}
}

public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Nullable JSONObject dataFields) {
public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Nullable JSONObject dataFields, @Nullable IterableAttributionInfo attributionInfo) {
JSONObject requestJSON = new JSONObject();
try {
JSONArray itemsArray = new JSONArray();
Expand All @@ -136,6 +136,11 @@ public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Null
requestJSON.put(IterableConstants.KEY_DATA_FIELDS, dataFields);
}

if (attributionInfo != null) {
requestJSON.putOpt(IterableConstants.KEY_CAMPAIGN_ID, attributionInfo.campaignId);
requestJSON.putOpt(IterableConstants.KEY_TEMPLATE_ID, attributionInfo.templateId);
}

sendPostRequest(IterableConstants.ENDPOINT_TRACK_PURCHASE, requestJSON);
} catch (JSONException e) {
e.printStackTrace();
Expand Down

0 comments on commit e95295f

Please sign in to comment.