add generic setAnalyticsPref functions for iOS and Android

This commit is contained in:
Ben Wheeler 2020-08-13 18:24:25 -04:00
parent d6e1a77327
commit c301a1090e
7 changed files with 49 additions and 0 deletions

View file

@ -629,4 +629,11 @@ public class JavaScriptDirectInterface {
_activity.setAnalyticsPlacePref(place); _activity.setAnalyticsPlacePref(place);
} }
} }
@JavascriptInterface
public void setAnalyticsPref(String prefObjStr) {
if (prefObjStr != null) {
_activity.setAnalyticsPref(prefObjStr);
}
}
} }

View file

@ -41,6 +41,9 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
/** /**
* Main activity for Scratch Jr., consisting of a full-screen landscape WebView. * Main activity for Scratch Jr., consisting of a full-screen landscape WebView.
* *
@ -520,6 +523,22 @@ public class ScratchJrActivity
_FirebaseAnalytics.setUserProperty("place_preference", place); _FirebaseAnalytics.setUserProperty("place_preference", place);
} }
/**
* Record a user property
* @param prefObjStr like "{\"school\": \"Central High\"}"
*/
public void setAnalyticsPref(String prefObjStr) {
try {
JSONObject jsonObject = new JSONObject(prefObjStr);
JSONArray jsonArray = jsonObject.names();
String key = jsonArray.getString(0);
String value = jsonObject.getString(key);
_FirebaseAnalytics.setUserProperty(key, value);
} catch (JSONException e) {
// don't log anything to Firebase
}
}
public void translateAndScaleRectToContainerCoords(RectF rect, float devicePixelRatio) { public void translateAndScaleRectToContainerCoords(RectF rect, float devicePixelRatio) {
float wx = _webView.getX(); float wx = _webView.getX();
float wy = _webView.getY(); float wy = _webView.getY();

View file

@ -128,6 +128,7 @@
- (NSString *) deviceName; - (NSString *) deviceName;
- (NSString *) analyticsEvent:(NSString *)category :(NSString *)action :(NSString *)label; - (NSString *) analyticsEvent:(NSString *)category :(NSString *)action :(NSString *)label;
- (void) setAnalyticsPlacePref:(NSString *)place; - (void) setAnalyticsPlacePref:(NSString *)place;
- (void) setAnalyticsPref:(NSString *)prefObjStr;
@end @end
@interface ViewController : UIViewController <JSExports,UIWebViewDelegate,MFMailComposeViewControllerDelegate> @interface ViewController : UIViewController <JSExports,UIWebViewDelegate,MFMailComposeViewControllerDelegate>

View file

@ -333,6 +333,16 @@ JSContext *js;
[FIRAnalytics setUserPropertyString:place forName:@"place_preference"]; [FIRAnalytics setUserPropertyString:place forName:@"place_preference"];
} }
// @param prefObjStr like "{\"place_preference\": \"School\"}"
-(void) setAnalyticsPref:(NSString*)prefObjStr {
NSData* data = [prefObjStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data options:0 error: nil];
NSString *key = [[dict allKeys] firstObject];
NSString *value = [dict objectForKey: key];
[FIRAnalytics setUserPropertyString:value forName:key];
}
// iPad name (used for information in the name/sharing dialog to help people using Airdrop) // iPad name (used for information in the name/sharing dialog to help people using Airdrop)
- (NSString*) deviceName { - (NSString*) deviceName {
return [[UIDevice currentDevice] name]; return [[UIDevice currentDevice] name];

View file

@ -261,6 +261,10 @@ export default class Android {
AndroidInterface.setAnalyticsPlacePref(preferredPlace); AndroidInterface.setAnalyticsPlacePref(preferredPlace);
} }
static setAnalyticsPref (jsonStr) {
AndroidInterface.setAnalyticsPref(jsonStr);
}
// // Web Wiew delegate call backs // // Web Wiew delegate call backs
// //
// static pageError (desc) { // static pageError (desc) {

View file

@ -253,6 +253,10 @@ export default class OS {
tabletInterface.setAnalyticsPlacePref(preferredPlace); tabletInterface.setAnalyticsPlacePref(preferredPlace);
} }
static setAnalyticsPref (key, value) {
tabletInterface.setAnalyticsPref(JSON.stringify({[key]: value}));
}
// Web Wiew delegate call backs // Web Wiew delegate call backs
static pageError (desc) { static pageError (desc) {

View file

@ -268,6 +268,10 @@ export default class iOS {
window.tablet.setAnalyticsPlacePref(preferredPlace); window.tablet.setAnalyticsPlacePref(preferredPlace);
} }
static setAnalyticsPref (jsonStr) {
window.tablet.setAnalyticsPref(jsonStr);
}
// // Web Wiew delegate call backs // // Web Wiew delegate call backs
// //
// static pageError (desc) { // static pageError (desc) {