Skip to content

Commit

Permalink
Map CommerceEvent product SKUs to af_content_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Dozor committed Aug 7, 2018
1 parent e583d8b commit 05a9a16
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/main/java/com/mparticle/kits/AppsFlyerKit.java
Expand Up @@ -51,7 +51,6 @@ public class AppsFlyerKit extends KitIntegration implements KitIntegration.Event
* This key will be present when returning a result from AppsFlyer's onAppOpenAttribution API
*/
public static final String APP_OPEN_ATTRIBUTION_RESULT = "MPARTICLE_APPSFLYER_APP_OPEN_ATTRIBUTION_RESULT";
private AttributionResult mLatestConversionData, mLatestOpenData;

@Override
public Object getInstance() {
Expand Down Expand Up @@ -133,6 +132,7 @@ public List<ReportingMessage> logEvent(CommerceEvent event) {
}
} else {
String eventName = event.getProductAction().equals(Product.CHECKOUT) ? AFInAppEventType.INITIATED_CHECKOUT : AFInAppEventType.PURCHASE;
eventValues.put(AFInAppEventParameterName.CONTENT_ID, AppsFlyerKit.generateProductIdList(event));
if (event.getProducts() != null && event.getProducts().size() > 0) {
double totalQuantity = 0;
for (Product product : event.getProducts()) {
Expand Down Expand Up @@ -172,6 +172,25 @@ public List<ReportingMessage> logEvent(CommerceEvent event) {
return messages;
}

static String generateProductIdList(CommerceEvent event) {
if (event == null || event.getProducts() == null || event.getProducts().size() == 0) {
return null;
}
StringBuilder productIdList = new StringBuilder();
for (Product product : event.getProducts()) {
String sku = product.getSku();
if (!KitUtils.isEmpty(sku)) {
productIdList.append(sku.replace(",","%2C"));
productIdList.append(",");
}
}
if (productIdList.length() > 0) {
return productIdList
.substring(0, productIdList.length() - 1);
}
return null;
}

@Override
public List<ReportingMessage> logEvent(MPEvent event) {
HashMap<String, Object> hashMap = null;
Expand Down Expand Up @@ -266,7 +285,6 @@ public void onInstallConversionDataLoaded(Map<String, String> conversionData) {
AttributionResult result = new AttributionResult()
.setParameters(jsonResult)
.setServiceProviderId(getConfiguration().getKitId());
mLatestConversionData = result;
getKitManager().onResult(result);

}
Expand Down Expand Up @@ -297,7 +315,6 @@ public void onAppOpenAttribution(Map<String, String> attributionData) {
AttributionResult result = new AttributionResult()
.setParameters(jsonResult)
.setServiceProviderId(getConfiguration().getKitId());
mLatestOpenData = result;
getKitManager().onResult(result);
}

Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/mparticle/kits/AppsflyerKitTests.java
Expand Up @@ -3,13 +3,20 @@

import android.content.Context;

import com.mparticle.MParticle;
import com.mparticle.commerce.CommerceEvent;
import com.mparticle.commerce.Product;
import com.mparticle.commerce.TransactionAttributes;

import org.junit.Test;
import org.mockito.Mockito;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -55,4 +62,31 @@ public void testClassName() throws Exception {
}
fail(className + " not found as a known integration.");
}

@Test
public void testGenerateSkuString() throws Exception {
MParticle.setInstance(Mockito.mock(MParticle.class));
Mockito.when(MParticle.getInstance().getEnvironment()).thenReturn(MParticle.Environment.Production);
assertNull(AppsFlyerKit.generateProductIdList(null));
Product product = new Product.Builder("foo-name", "foo-sku", 50).build();
CommerceEvent event = new CommerceEvent.Builder(Product.PURCHASE, product)
.transactionAttributes(new TransactionAttributes("foo"))
.build();
assertEquals("foo-sku", AppsFlyerKit.generateProductIdList(event));

Product product2 = new Product.Builder("foo-name-2", "foo-sku-2", 50).build();
CommerceEvent event2 = new CommerceEvent.Builder(Product.PURCHASE, product)
.addProduct(product2)
.transactionAttributes(new TransactionAttributes("foo"))
.build();
assertEquals("foo-sku,foo-sku-2", AppsFlyerKit.generateProductIdList(event2));

Product product3 = new Product.Builder("foo-name-3", "foo-sku-,3", 50).build();
CommerceEvent event3 = new CommerceEvent.Builder(Product.PURCHASE, product)
.addProduct(product2)
.addProduct(product3)
.transactionAttributes(new TransactionAttributes("foo"))
.build();
assertEquals("foo-sku,foo-sku-2,foo-sku-%2C3", AppsFlyerKit.generateProductIdList(event3));
}
}

0 comments on commit 05a9a16

Please sign in to comment.