11package io .fullstack .firestack .analytics ;
22
3- import java .util .Map ;
43import android .util .Log ;
5- import android .os .Bundle ;
64import android .app .Activity ;
5+ import android .support .annotation .Nullable ;
76
7+ import com .facebook .react .bridge .Arguments ;
88import com .facebook .react .bridge .ReactMethod ;
99import com .facebook .react .bridge .ReadableMap ;
1010import com .google .firebase .analytics .FirebaseAnalytics ;
1111import com .facebook .react .bridge .ReactApplicationContext ;
1212import com .facebook .react .bridge .ReactContextBaseJavaModule ;
1313
14- import io .fullstack .firestack .Utils ;
1514
1615public class FirestackAnalytics extends ReactContextBaseJavaModule {
1716
1817 private static final String TAG = "FirestackAnalytics" ;
1918
20- private ReactApplicationContext context ;
21- private FirebaseAnalytics mFirebaseAnalytics ;
22-
2319 public FirestackAnalytics (ReactApplicationContext reactContext ) {
2420 super (reactContext );
25- context = reactContext ;
2621 Log .d (TAG , "New instance" );
27- mFirebaseAnalytics = FirebaseAnalytics .getInstance (this .context );
2822 }
2923
3024 /**
@@ -37,11 +31,8 @@ public String getName() {
3731 }
3832
3933 @ ReactMethod
40- public void logEvent (final String name , final ReadableMap params ) {
41- Map <String , Object > m = Utils .recursivelyDeconstructReadableMap (params );
42- final Bundle bundle = makeEventBundle (name , m );
43- Log .d (TAG , "Logging event " + name );
44- mFirebaseAnalytics .logEvent (name , bundle );
34+ public void logEvent (final String name , @ Nullable final ReadableMap params ) {
35+ FirebaseAnalytics .getInstance (getReactApplicationContext ()).logEvent (name , Arguments .toBundle (params ));
4536 }
4637
4738 /**
@@ -50,7 +41,7 @@ public void logEvent(final String name, final ReadableMap params) {
5041 */
5142 @ ReactMethod
5243 public void setAnalyticsCollectionEnabled (final Boolean enabled ) {
53- mFirebaseAnalytics .setAnalyticsCollectionEnabled (enabled );
44+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setAnalyticsCollectionEnabled (enabled );
5445 }
5546
5647 /**
@@ -62,12 +53,12 @@ public void setAnalyticsCollectionEnabled(final Boolean enabled) {
6253 public void setCurrentScreen (final String screenName , final String screenClassOverride ) {
6354 final Activity activity = getCurrentActivity ();
6455 if (activity != null ) {
65- Log .d (TAG , "setCurrentScreen " + screenName + " - " + screenClassOverride );
6656 // needs to be run on main thread
57+ Log .d (TAG , "setCurrentScreen " + screenName + " - " + screenClassOverride );
6758 activity .runOnUiThread (new Runnable () {
6859 @ Override
6960 public void run () {
70- mFirebaseAnalytics .setCurrentScreen (activity , screenName , screenClassOverride );
61+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setCurrentScreen (activity , screenName , screenClassOverride );
7162 }
7263 });
7364 }
@@ -79,7 +70,7 @@ public void run() {
7970 */
8071 @ ReactMethod
8172 public void setMinimumSessionDuration (final double milliseconds ) {
82- mFirebaseAnalytics .setMinimumSessionDuration ((long ) milliseconds );
73+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setMinimumSessionDuration ((long ) milliseconds );
8374 }
8475
8576 /**
@@ -88,7 +79,7 @@ public void setMinimumSessionDuration(final double milliseconds) {
8879 */
8980 @ ReactMethod
9081 public void setSessionTimeoutDuration (final double milliseconds ) {
91- mFirebaseAnalytics .setSessionTimeoutDuration ((long ) milliseconds );
82+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setSessionTimeoutDuration ((long ) milliseconds );
9283 }
9384
9485 /**
@@ -97,7 +88,7 @@ public void setSessionTimeoutDuration(final double milliseconds) {
9788 */
9889 @ ReactMethod
9990 public void setUserId (final String id ) {
100- mFirebaseAnalytics .setUserId (id );
91+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setUserId (id );
10192 }
10293
10394 /**
@@ -107,144 +98,6 @@ public void setUserId(final String id) {
10798 */
10899 @ ReactMethod
109100 public void setUserProperty (final String name , final String value ) {
110- mFirebaseAnalytics .setUserProperty (name , value );
111- }
112-
113- // todo refactor/clean me
114- private Bundle makeEventBundle (final String name , final Map <String , Object > map ) {
115- Bundle bundle = new Bundle ();
116- // Available from the FirestackAnalytics event
117- if (map .containsKey ("id" )) {
118- String id = (String ) map .get ("id" );
119- bundle .putString (FirebaseAnalytics .Param .ITEM_ID , id );
120- }
121- if (map .containsKey ("name" )) {
122- String val = (String ) map .get ("name" );
123- bundle .putString (FirebaseAnalytics .Param .ITEM_NAME , val );
124- }
125- if (map .containsKey ("category" )) {
126- String val = (String ) map .get ("category" );
127- bundle .putString (FirebaseAnalytics .Param .ITEM_NAME , val );
128- }
129- if (map .containsKey ("quantity" )) {
130- double val = (double ) map .get ("quantity" );
131- bundle .putDouble (FirebaseAnalytics .Param .QUANTITY , val );
132- }
133- if (map .containsKey ("price" )) {
134- double val = (double ) map .get ("price" );
135- bundle .putDouble (FirebaseAnalytics .Param .PRICE , val );
136- }
137- if (map .containsKey ("value" )) {
138- double val = (double ) map .get ("value" );
139- bundle .putDouble (FirebaseAnalytics .Param .VALUE , val );
140- }
141- if (map .containsKey ("currency" )) {
142- String val = (String ) map .get ("currency" );
143- bundle .putString (FirebaseAnalytics .Param .CURRENCY , val );
144- }
145- if (map .containsKey ("origin" )) {
146- String val = (String ) map .get ("origin" );
147- bundle .putString (FirebaseAnalytics .Param .ORIGIN , val );
148- }
149- if (map .containsKey ("item_location_id" )) {
150- String val = (String ) map .get ("item_location_id" );
151- bundle .putString (FirebaseAnalytics .Param .ITEM_LOCATION_ID , val );
152- }
153- if (map .containsKey ("location" )) {
154- String val = (String ) map .get ("location" );
155- bundle .putString (FirebaseAnalytics .Param .LOCATION , val );
156- }
157- if (map .containsKey ("destination" )) {
158- String val = (String ) map .get ("destination" );
159- bundle .putString (FirebaseAnalytics .Param .DESTINATION , val );
160- }
161- if (map .containsKey ("start_date" )) {
162- String val = (String ) map .get ("start_date" );
163- bundle .putString (FirebaseAnalytics .Param .START_DATE , val );
164- }
165- if (map .containsKey ("end_date" )) {
166- String val = (String ) map .get ("end_date" );
167- bundle .putString (FirebaseAnalytics .Param .END_DATE , val );
168- }
169- if (map .containsKey ("transaction_id" )) {
170- String val = (String ) map .get ("transaction_id" );
171- bundle .putString (FirebaseAnalytics .Param .TRANSACTION_ID , val );
172- }
173- if (map .containsKey ("number_of_nights" )) {
174- long val = (long ) map .get ("number_of_nights" );
175- bundle .putLong (FirebaseAnalytics .Param .NUMBER_OF_NIGHTS , val );
176- }
177- if (map .containsKey ("number_of_rooms" )) {
178- long val = (long ) map .get ("number_of_rooms" );
179- bundle .putLong (FirebaseAnalytics .Param .NUMBER_OF_ROOMS , val );
180- }
181- if (map .containsKey ("number_of_passengers" )) {
182- long val = (long ) map .get ("number_of_passengers" );
183- bundle .putLong (FirebaseAnalytics .Param .NUMBER_OF_PASSENGERS , val );
184- }
185- if (map .containsKey ("travel_class" )) {
186- String val = (String ) map .get ("travel_class" );
187- bundle .putString (FirebaseAnalytics .Param .TRAVEL_CLASS , val );
188- }
189- if (map .containsKey ("coupon" )) {
190- String val = (String ) map .get ("coupon" );
191- bundle .putString (FirebaseAnalytics .Param .COUPON , val );
192- }
193- if (map .containsKey ("tax" )) {
194- long val = (long ) map .get ("tax" );
195- bundle .putLong (FirebaseAnalytics .Param .TAX , val );
196- }
197- if (map .containsKey ("shipping" )) {
198- double val = (double ) map .get ("shipping" );
199- bundle .putDouble (FirebaseAnalytics .Param .SHIPPING , val );
200- }
201- if (map .containsKey ("group_id" )) {
202- String val = (String ) map .get ("group_id" );
203- bundle .putString (FirebaseAnalytics .Param .GROUP_ID , val );
204- }
205- if (map .containsKey ("level" )) {
206- long val = (long ) map .get ("level" );
207- bundle .putLong (FirebaseAnalytics .Param .LEVEL , val );
208- }
209- if (map .containsKey ("character" )) {
210- String val = (String ) map .get ("character" );
211- bundle .putString (FirebaseAnalytics .Param .CHARACTER , val );
212- }
213- if (map .containsKey ("score" )) {
214- long val = (long ) map .get ("score" );
215- bundle .putLong (FirebaseAnalytics .Param .SCORE , val );
216- }
217- if (map .containsKey ("search_term" )) {
218- String val = (String ) map .get ("search_term" );
219- bundle .putString (FirebaseAnalytics .Param .SEARCH_TERM , val );
220- }
221- if (map .containsKey ("content_type" )) {
222- String val = (String ) map .get ("content_type" );
223- bundle .putString (FirebaseAnalytics .Param .CONTENT_TYPE , val );
224- }
225- if (map .containsKey ("sign_up_method" )) {
226- String val = (String ) map .get ("sign_up_method" );
227- bundle .putString (FirebaseAnalytics .Param .SIGN_UP_METHOD , val );
228- }
229- if (map .containsKey ("virtual_currency_name" )) {
230- String val = (String ) map .get ("virtual_currency_name" );
231- bundle .putString (FirebaseAnalytics .Param .VIRTUAL_CURRENCY_NAME , val );
232- }
233- if (map .containsKey ("achievement_id" )) {
234- String val = (String ) map .get ("achievement_id" );
235- bundle .putString (FirebaseAnalytics .Param .ACHIEVEMENT_ID , val );
236- }
237- if (map .containsKey ("flight_number" )) {
238- String val = (String ) map .get ("flight_number" );
239- bundle .putString (FirebaseAnalytics .Param .FLIGHT_NUMBER , val );
240- }
241-
242- for (Map .Entry <String , Object > entry : map .entrySet ()) {
243- if (bundle .getBundle (entry .getKey ()) == null ) {
244- bundle .putString (entry .getKey (), entry .getValue ().toString ());
245- }
246- }
247-
248- return bundle ;
101+ FirebaseAnalytics .getInstance (getReactApplicationContext ()).setUserProperty (name , value );
249102 }
250103}
0 commit comments