From c301a1090ed747c26955309ca5fcaa683e84286c Mon Sep 17 00:00:00 2001 From: Ben Wheeler Date: Thu, 13 Aug 2020 18:24:25 -0400 Subject: [PATCH] add generic setAnalyticsPref functions for iOS and Android --- .../android/JavaScriptDirectInterface.java | 7 +++++++ .../scratchjr/android/ScratchJrActivity.java | 19 +++++++++++++++++++ ios/ScratchJr/src/ScratchJr.h | 1 + ios/ScratchJr/src/ViewController.m | 10 ++++++++++ src/tablet/Android.js | 4 ++++ src/tablet/OS.js | 4 ++++ src/tablet/iOS.js | 4 ++++ 7 files changed, 49 insertions(+) diff --git a/android/ScratchJr/app/src/main/java/org/scratchjr/android/JavaScriptDirectInterface.java b/android/ScratchJr/app/src/main/java/org/scratchjr/android/JavaScriptDirectInterface.java index 856eaef..603b5c1 100644 --- a/android/ScratchJr/app/src/main/java/org/scratchjr/android/JavaScriptDirectInterface.java +++ b/android/ScratchJr/app/src/main/java/org/scratchjr/android/JavaScriptDirectInterface.java @@ -629,4 +629,11 @@ public class JavaScriptDirectInterface { _activity.setAnalyticsPlacePref(place); } } + + @JavascriptInterface + public void setAnalyticsPref(String prefObjStr) { + if (prefObjStr != null) { + _activity.setAnalyticsPref(prefObjStr); + } + } } diff --git a/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrActivity.java b/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrActivity.java index a58b89f..23c6795 100644 --- a/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrActivity.java +++ b/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrActivity.java @@ -41,6 +41,9 @@ import java.io.FileNotFoundException; import java.io.IOException; 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. * @@ -520,6 +523,22 @@ public class ScratchJrActivity _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) { float wx = _webView.getX(); float wy = _webView.getY(); diff --git a/ios/ScratchJr/src/ScratchJr.h b/ios/ScratchJr/src/ScratchJr.h index 873bf71..4201b3c 100644 --- a/ios/ScratchJr/src/ScratchJr.h +++ b/ios/ScratchJr/src/ScratchJr.h @@ -128,6 +128,7 @@ - (NSString *) deviceName; - (NSString *) analyticsEvent:(NSString *)category :(NSString *)action :(NSString *)label; - (void) setAnalyticsPlacePref:(NSString *)place; +- (void) setAnalyticsPref:(NSString *)prefObjStr; @end @interface ViewController : UIViewController diff --git a/ios/ScratchJr/src/ViewController.m b/ios/ScratchJr/src/ViewController.m index 5571b85..58d0d6d 100644 --- a/ios/ScratchJr/src/ViewController.m +++ b/ios/ScratchJr/src/ViewController.m @@ -333,6 +333,16 @@ JSContext *js; [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) - (NSString*) deviceName { return [[UIDevice currentDevice] name]; diff --git a/src/tablet/Android.js b/src/tablet/Android.js index 90d0fee..7ceebf5 100644 --- a/src/tablet/Android.js +++ b/src/tablet/Android.js @@ -261,6 +261,10 @@ export default class Android { AndroidInterface.setAnalyticsPlacePref(preferredPlace); } + static setAnalyticsPref (jsonStr) { + AndroidInterface.setAnalyticsPref(jsonStr); + } + // // Web Wiew delegate call backs // // static pageError (desc) { diff --git a/src/tablet/OS.js b/src/tablet/OS.js index 794b7ef..2124a6b 100644 --- a/src/tablet/OS.js +++ b/src/tablet/OS.js @@ -253,6 +253,10 @@ export default class OS { tabletInterface.setAnalyticsPlacePref(preferredPlace); } + static setAnalyticsPref (key, value) { + tabletInterface.setAnalyticsPref(JSON.stringify({[key]: value})); + } + // Web Wiew delegate call backs static pageError (desc) { diff --git a/src/tablet/iOS.js b/src/tablet/iOS.js index 747751e..e5db955 100644 --- a/src/tablet/iOS.js +++ b/src/tablet/iOS.js @@ -268,6 +268,10 @@ export default class iOS { window.tablet.setAnalyticsPlacePref(preferredPlace); } + static setAnalyticsPref (jsonStr) { + window.tablet.setAnalyticsPref(jsonStr); + } + // // Web Wiew delegate call backs // // static pageError (desc) {