1
1
package io .fullstack .firestack .analytics ;
2
2
3
- import java .util .Map ;
4
3
import android .util .Log ;
5
- import android .os .Bundle ;
6
4
import android .app .Activity ;
5
+ import android .support .annotation .Nullable ;
7
6
7
+ import com .facebook .react .bridge .Arguments ;
8
8
import com .facebook .react .bridge .ReactMethod ;
9
9
import com .facebook .react .bridge .ReadableMap ;
10
10
import com .google .firebase .analytics .FirebaseAnalytics ;
11
11
import com .facebook .react .bridge .ReactApplicationContext ;
12
12
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
13
13
14
- import io .fullstack .firestack .Utils ;
15
14
16
15
public class FirestackAnalytics extends ReactContextBaseJavaModule {
17
16
18
17
private static final String TAG = "FirestackAnalytics" ;
19
18
20
- private ReactApplicationContext context ;
21
- private FirebaseAnalytics mFirebaseAnalytics ;
22
-
23
19
public FirestackAnalytics (ReactApplicationContext reactContext ) {
24
20
super (reactContext );
25
- context = reactContext ;
26
21
Log .d (TAG , "New instance" );
27
- mFirebaseAnalytics = FirebaseAnalytics .getInstance (this .context );
28
22
}
29
23
30
24
/**
@@ -37,11 +31,8 @@ public String getName() {
37
31
}
38
32
39
33
@ 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 ));
45
36
}
46
37
47
38
/**
@@ -50,7 +41,7 @@ public void logEvent(final String name, final ReadableMap params) {
50
41
*/
51
42
@ ReactMethod
52
43
public void setAnalyticsCollectionEnabled (final Boolean enabled ) {
53
- mFirebaseAnalytics .setAnalyticsCollectionEnabled (enabled );
44
+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setAnalyticsCollectionEnabled (enabled );
54
45
}
55
46
56
47
/**
@@ -62,12 +53,12 @@ public void setAnalyticsCollectionEnabled(final Boolean enabled) {
62
53
public void setCurrentScreen (final String screenName , final String screenClassOverride ) {
63
54
final Activity activity = getCurrentActivity ();
64
55
if (activity != null ) {
65
- Log .d (TAG , "setCurrentScreen " + screenName + " - " + screenClassOverride );
66
56
// needs to be run on main thread
57
+ Log .d (TAG , "setCurrentScreen " + screenName + " - " + screenClassOverride );
67
58
activity .runOnUiThread (new Runnable () {
68
59
@ Override
69
60
public void run () {
70
- mFirebaseAnalytics .setCurrentScreen (activity , screenName , screenClassOverride );
61
+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setCurrentScreen (activity , screenName , screenClassOverride );
71
62
}
72
63
});
73
64
}
@@ -79,7 +70,7 @@ public void run() {
79
70
*/
80
71
@ ReactMethod
81
72
public void setMinimumSessionDuration (final double milliseconds ) {
82
- mFirebaseAnalytics .setMinimumSessionDuration ((long ) milliseconds );
73
+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setMinimumSessionDuration ((long ) milliseconds );
83
74
}
84
75
85
76
/**
@@ -88,7 +79,7 @@ public void setMinimumSessionDuration(final double milliseconds) {
88
79
*/
89
80
@ ReactMethod
90
81
public void setSessionTimeoutDuration (final double milliseconds ) {
91
- mFirebaseAnalytics .setSessionTimeoutDuration ((long ) milliseconds );
82
+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setSessionTimeoutDuration ((long ) milliseconds );
92
83
}
93
84
94
85
/**
@@ -97,7 +88,7 @@ public void setSessionTimeoutDuration(final double milliseconds) {
97
88
*/
98
89
@ ReactMethod
99
90
public void setUserId (final String id ) {
100
- mFirebaseAnalytics .setUserId (id );
91
+ FirebaseAnalytics . getInstance ( getReactApplicationContext ()) .setUserId (id );
101
92
}
102
93
103
94
/**
@@ -107,144 +98,6 @@ public void setUserId(final String id) {
107
98
*/
108
99
@ ReactMethod
109
100
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 );
249
102
}
250
103
}
0 commit comments