add user profile reporting
This commit is contained in:
@@ -8,15 +8,23 @@ import android.util.Log;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
||||
|
||||
import java.lang.Exception;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.yandex.metrica.YandexMetrica;
|
||||
import com.yandex.metrica.YandexMetricaConfig;
|
||||
import com.yandex.metrica.profile.UserProfile;
|
||||
import com.yandex.metrica.profile.Attribute;
|
||||
import com.yandex.metrica.profile.GenderAttribute;
|
||||
|
||||
import static com.facebook.react.bridge.ReadableType.Array;
|
||||
|
||||
public class AppMetricaModule extends ReactContextBaseJavaModule {
|
||||
final static String ModuleName = "AppMetrica";
|
||||
@@ -82,8 +90,125 @@ public class AppMetricaModule extends ReactContextBaseJavaModule {
|
||||
YandexMetrica.setUserProfileID(profileID);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void reportUserProfile(ReadableMap params) {
|
||||
UserProfile.Builder userProfileBuilder = UserProfile.newBuilder();
|
||||
ReadableMapKeySetIterator iterator = params.keySetIterator();
|
||||
|
||||
while (iterator.hasNextKey()) {
|
||||
String key = iterator.nextKey();
|
||||
|
||||
switch (key) {
|
||||
// predefined attributes
|
||||
case "name":
|
||||
userProfileBuilder.apply(
|
||||
params.isNull(key)
|
||||
? Attribute.name().withValueReset()
|
||||
: Attribute.name().withValue(params.getString(key))
|
||||
);
|
||||
break;
|
||||
case "gender":
|
||||
userProfileBuilder.apply(
|
||||
params.isNull(key)
|
||||
? Attribute.gender().withValueReset()
|
||||
: Attribute.gender().withValue(
|
||||
params.getString(key).equals("female")
|
||||
? GenderAttribute.Gender.FEMALE
|
||||
: params.getString(key).equals("male")
|
||||
? GenderAttribute.Gender.MALE
|
||||
: GenderAttribute.Gender.OTHER
|
||||
)
|
||||
);
|
||||
break;
|
||||
case "age":
|
||||
userProfileBuilder.apply(
|
||||
params.isNull(key)
|
||||
? Attribute.birthDate().withValueReset()
|
||||
: Attribute.birthDate().withAge(params.getInt(key))
|
||||
);
|
||||
break;
|
||||
case "birthDate":
|
||||
if (params.isNull(key)) {
|
||||
userProfileBuilder.apply(
|
||||
Attribute.birthDate().withValueReset()
|
||||
);
|
||||
} else if (params.getType(key) == Array) {
|
||||
// an array of [ year[, month][, day] ]
|
||||
ReadableArray date = params.getArray(key);
|
||||
if (date.size() == 1) {
|
||||
userProfileBuilder.apply(
|
||||
Attribute.birthDate().withBirthDate(
|
||||
date.getInt(0)
|
||||
)
|
||||
);
|
||||
} else if (date.size() == 2) {
|
||||
userProfileBuilder.apply(
|
||||
Attribute.birthDate().withBirthDate(
|
||||
date.getInt(0),
|
||||
date.getInt(1)
|
||||
)
|
||||
);
|
||||
} else {
|
||||
userProfileBuilder.apply(
|
||||
Attribute.birthDate().withBirthDate(
|
||||
date.getInt(0),
|
||||
date.getInt(1),
|
||||
date.getInt(2)
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// number of milliseconds since Unix epoch
|
||||
Date date = new Date((long)params.getInt(key));
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
userProfileBuilder.apply(
|
||||
Attribute.birthDate().withBirthDate(cal)
|
||||
);
|
||||
}
|
||||
break;
|
||||
case "notificationsEnabled":
|
||||
userProfileBuilder.apply(
|
||||
params.isNull(key)
|
||||
? Attribute.notificationsEnabled().withValueReset()
|
||||
: Attribute.notificationsEnabled().withValue(params.getBoolean(key))
|
||||
);
|
||||
break;
|
||||
// custom attributes
|
||||
default:
|
||||
// TODO: come up with a syntax solution to reset custom attributes. `null` will break type checking here
|
||||
switch (params.getType(key)) {
|
||||
case Boolean:
|
||||
userProfileBuilder.apply(
|
||||
Attribute.customBoolean(key).withValue(params.getBoolean(key))
|
||||
);
|
||||
break;
|
||||
case Number:
|
||||
userProfileBuilder.apply(
|
||||
Attribute.customNumber(key).withValue(params.getDouble(key))
|
||||
);
|
||||
break;
|
||||
case String:
|
||||
String value = params.getString(key);
|
||||
if (value.startsWith("+") || value.startsWith("-")) {
|
||||
userProfileBuilder.apply(
|
||||
Attribute.customCounter(key).withDelta(Double.parseDouble(value))
|
||||
);
|
||||
} else {
|
||||
userProfileBuilder.apply(
|
||||
Attribute.customString(key).withValue(value)
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
YandexMetrica.reportUserProfile(userProfileBuilder.build());
|
||||
}
|
||||
|
||||
private String convertReadableMapToJson(final ReadableMap readableMap) {
|
||||
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
|
||||
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user