Skip to content

Commit 29f7629

Browse files
authored
Fix product key mappings (#36)
* map keys under properties.products
1 parent d92e343 commit 29f7629

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/main/java/com/segment/analytics/android/integrations/firebase/FirebaseIntegration.java

+55-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import com.segment.analytics.integrations.TrackPayload;
2020
import com.segment.analytics.integrations.ScreenPayload;
2121

22+
import java.util.ArrayList;
2223
import java.util.HashMap;
24+
import java.util.List;
2325
import java.util.Map;
2426

2527
import static com.segment.analytics.internal.Utils.hasPermission;
@@ -104,6 +106,19 @@ private static Map<String, String> createPropertyMap() {
104106
return PROPERTY_MAPPER;
105107
}
106108

109+
private static final Map<String, String> PRODUCT_MAPPER = createProductMap();
110+
111+
private static Map<String, String> createProductMap() {
112+
Map<String, String> MAPPER = new HashMap<>();
113+
MAPPER.put("category", Param.ITEM_CATEGORY);
114+
MAPPER.put("product_id", Param.ITEM_ID);
115+
MAPPER.put("id", Param.ITEM_ID);
116+
MAPPER.put("name", Param.ITEM_NAME);
117+
MAPPER.put("price", Param.PRICE);
118+
MAPPER.put("quantity", Param.QUANTITY);
119+
return MAPPER;
120+
}
121+
107122
public FirebaseIntegration(Context context, Logger logger) {
108123
this.logger = logger;
109124
this.firebaseAnalytics = FirebaseAnalytics.getInstance(context);
@@ -196,23 +211,52 @@ && isNullOrEmpty(properties.currency())) {
196211
} else {
197212
property = makeKey(property);
198213
}
199-
if (value instanceof Integer) {
200-
int intValue = (int) value;
201-
bundle.putInt(property, intValue);
202-
} else if (value instanceof Double) {
203-
double doubleValue = (double) value;
204-
bundle.putDouble(property, doubleValue);
205-
} else if (value instanceof Long) {
206-
long longValue = (long) value;
207-
bundle.putLong(property, longValue);
214+
if (property.equals(Param.ITEMS)) {
215+
List<ValueMap> products = properties.getList("products", ValueMap.class);
216+
ArrayList<Bundle> mappedProducts = formatProducts(products);
217+
bundle.putParcelableArrayList(property, mappedProducts);
208218
} else {
209-
String stringValue = String.valueOf(value);
210-
bundle.putString(property, stringValue);
219+
putValue(bundle, property, value);
211220
}
212221
}
213222
return bundle;
214223
}
215224

225+
private static ArrayList<Bundle> formatProducts(List<ValueMap> products) {
226+
ArrayList<Bundle> mappedProducts = new ArrayList<>();
227+
for (ValueMap product : products) {
228+
Bundle mappedProduct = new Bundle();
229+
for (Map.Entry<String, Object> innerEntry : product.entrySet()) {
230+
String key = innerEntry.getKey();
231+
Object value = innerEntry.getValue();
232+
if (PRODUCT_MAPPER.containsKey(key)) {
233+
key = PRODUCT_MAPPER.get(key);
234+
} else {
235+
key = makeKey(key);
236+
}
237+
putValue(mappedProduct, key, value);
238+
}
239+
mappedProducts.add(mappedProduct);
240+
}
241+
return mappedProducts;
242+
}
243+
244+
private static void putValue(Bundle bundle, String key, Object value) {
245+
if (value instanceof Integer) {
246+
int intValue = (int) value;
247+
bundle.putInt(key, intValue);
248+
} else if (value instanceof Double) {
249+
double doubleValue = (double) value;
250+
bundle.putDouble(key, doubleValue);
251+
} else if (value instanceof Long) {
252+
long longValue = (long) value;
253+
bundle.putLong(key, longValue);
254+
} else {
255+
String stringValue = String.valueOf(value);
256+
bundle.putString(key, stringValue);
257+
}
258+
}
259+
216260
public static String makeKey(String key) {
217261
String[] forbiddenChars = {".", "-", " "};
218262
for (String forbidden : forbiddenChars) {

src/test/java/com/segment/analytics/android/integration/firebase/FirebaseTest.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.skyscreamer.jsonassert.JSONAssert;
2929
import org.skyscreamer.jsonassert.JSONCompareMode;
3030

31+
import java.util.ArrayList;
3132
import java.util.Date;
3233
import java.util.HashMap;
3334
import java.util.Map;
@@ -125,14 +126,28 @@ public void trackPurchaseWithProducts() {
125126
.putValue("revenue", 100.0)
126127
.putValue("currency", "USD");
127128
Properties.Product product1 = new Properties.Product( "123", "abc", 10.0);
129+
product1.putName("foo");
130+
product1.put("category", "bar");
131+
product1.put("quantity", 2);
128132
properties.putProducts(product1);
129133

130134
integration.track(new TrackPayload.Builder().anonymousId("1234").properties(properties).event("Order Completed").build());
131135

136+
Bundle product = new Bundle();
137+
product.putString("item_id", "123");
138+
product.putString("item_name", "foo");
139+
product.putString("item_category", "bar");
140+
product.putDouble("price", 10.0);
141+
product.putString("sku", "abc");
142+
product.putInt("quantity", 2);
143+
144+
ArrayList<Bundle> items = new ArrayList<>();
145+
items.add(product);
146+
132147
Bundle expected = new Bundle();
133148
expected.putDouble("value", 100.0);
134149
expected.putString("currency", "USD");
135-
expected.putString("items", "[{id=123, sku=abc, price=10.0}]");
150+
expected.putParcelableArrayList("items", items);
136151

137152
verify(firebase).logEvent(eq("purchase"), bundleEq(expected));
138153
}

0 commit comments

Comments
 (0)