Skip to content

Commit 7eac37e

Browse files
author
Brandon Sneed
committed
fix(android): fixed context not passing thru
1 parent 5b90ce2 commit 7eac37e

File tree

7 files changed

+96
-181
lines changed

7 files changed

+96
-181
lines changed

packages/core/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ repositories {
3838

3939

4040
dependencies {
41-
api 'com.segment.analytics.android:analytics:4.+'
41+
api 'com.segment.analytics.android:analytics:4.5.0-beta.0'
4242

4343
api 'com.facebook.react:react-native:+'
4444
api 'org.jetbrains.kotlin:kotlin-stdlib:1.3.21'

packages/core/android/src/main/java/com/segment/analytics/reactnative/core/RNAnalyticsModule.kt

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,6 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
5959
}
6060
}
6161

62-
/**
63-
* Builds Options with integrations
64-
* The format for the integrations variable is { String: Boolean | Map }
65-
*/
66-
private fun getOptions(integrations: ReadableMap?): Options {
67-
var options = Options()
68-
69-
if (integrations !== null) {
70-
val iterator = integrations.keySetIterator()
71-
while (iterator.hasNextKey()) {
72-
val nextKey = iterator.nextKey()
73-
when (integrations.getType(nextKey)) {
74-
ReadableType.Boolean -> options.setIntegration(nextKey, integrations.getBoolean(nextKey))
75-
ReadableType.Map -> {
76-
options.setIntegration(nextKey, true)
77-
options.setIntegrationOptions(nextKey, integrations.getMap(nextKey)?.toHashMap())
78-
}
79-
}
80-
}
81-
}
82-
return options
83-
}
84-
8562
/**
8663
* Tracks application lifecycle events - Application Installed, Application Updated and Application Opened
8764
* This is built to exactly mirror the application lifecycle tracking in analytics-android
@@ -154,8 +131,8 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
154131

155132
if(options.hasKey("flushInterval")) {
156133
builder.flushInterval(
157-
options.getInt("flushInterval").toLong(),
158-
TimeUnit.MILLISECONDS
134+
options.getInt("flushInterval").toLong(),
135+
TimeUnit.MILLISECONDS
159136
)
160137
}
161138

@@ -169,7 +146,7 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
169146

170147
try {
171148
Analytics.setSingletonInstance(
172-
RNAnalytics.buildWithIntegrations(builder)
149+
RNAnalytics.buildWithIntegrations(builder)
173150
)
174151
} catch(e: Exception) {
175152
return promise.reject("E_SEGMENT_ERROR", e)
@@ -184,48 +161,84 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
184161
}
185162

186163
@ReactMethod
187-
fun track(event: String, properties: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
188-
analytics.track(event, Properties() from properties, this.getOptions(integrations))
164+
fun track(event: String, properties: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
165+
analytics.track(
166+
event,
167+
Properties() from properties,
168+
optionsFrom(context, integrations)
169+
)
189170

190171
@ReactMethod
191-
fun screen(name: String, properties: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
192-
analytics.screen(null, name, Properties() from properties, this.getOptions(integrations))
172+
fun screen(name: String?, properties: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
173+
analytics.screen(
174+
null,
175+
name,
176+
Properties() from properties,
177+
optionsFrom(context, integrations)
178+
)
193179

194180
@ReactMethod
195-
fun identify(userId: String, traits: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
196-
analytics.identify(userId, Traits() from traits, this.getOptions(integrations))
181+
fun identify(userId: String?, traits: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
182+
analytics.identify(
183+
userId,
184+
Traits() from traits,
185+
optionsFrom(context, integrations)
186+
)
197187

198188
@ReactMethod
199-
fun group(groupId: String, traits: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
200-
analytics.group(groupId, Traits() from traits, this.getOptions(integrations))
189+
fun group(groupId: String, traits: ReadableMap?, integrations: ReadableMap, context: ReadableMap) =
190+
analytics.group(
191+
groupId,
192+
Traits() from traits,
193+
optionsFrom(context, integrations)
194+
)
201195

202196
@ReactMethod
203-
fun alias(newId: String, context: ReadableMap, integrations: ReadableMap) =
204-
analytics.alias(newId, this.getOptions(integrations))
197+
fun alias(newId: String, integrations: ReadableMap?, context: ReadableMap?) =
198+
analytics.alias(
199+
newId,
200+
optionsFrom(context, integrations)
201+
)
205202

206203
@ReactMethod
207204
fun reset() =
208-
analytics.reset()
205+
analytics.reset()
209206

210207
@ReactMethod()
211208
fun flush() =
212-
analytics.flush()
209+
analytics.flush()
213210

214211
@ReactMethod
215212
fun enable() =
216-
analytics.optOut(false)
213+
analytics.optOut(false)
217214

218215
@ReactMethod
219216
fun disable() =
220-
analytics.optOut(true)
217+
analytics.optOut(true)
221218

222219
@ReactMethod
223220
fun getAnonymousId(promise: Promise) =
224-
promise.resolve(analytics.getAnalyticsContext().traits().anonymousId())
221+
promise.resolve(analytics.getAnalyticsContext().traits().anonymousId())
225222
}
226223

227-
private infix fun<T: ValueMap> T.from(source: ReadableMap): T {
228-
putAll(source.toHashMap())
224+
private fun optionsFrom(context: ReadableMap?, integrations: ReadableMap?): Options {
225+
var options = Options()
226+
227+
context?.toHashMap()?.forEach { (key, value) ->
228+
options.putContext(key, value)
229+
}
230+
231+
integrations?.toHashMap()?.forEach { (key, value) ->
232+
options.setIntegration(key, value.toString().toBoolean())
233+
}
234+
235+
return options
236+
}
237+
238+
private infix fun<T: ValueMap> T.from(source: ReadableMap?): T {
239+
if (source != null) {
240+
putAll(source.toHashMap())
241+
}
229242

230243
return this
231244
}

packages/core/docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
**Ƭ Integration**: *`function` \| `object`*
2626

27-
*Defined in [analytics.ts:8](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L8)*
27+
*Defined in analytics.ts:8*
2828

2929
___
3030

packages/core/docs/classes/analytics.client.md

Lines changed: 15 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@
3939

4040
**● ready**: *`false`* = false
4141

42-
*Defined in [analytics.ts:96](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L96)*
43-
44-
Whether the client is ready to send events to Segment.
45-
46-
This becomes `true` when `.setup()` succeeds. All calls will be queued until it becomes `true`.
42+
*Defined in analytics.ts:96*
4743

4844
___
4945

@@ -55,11 +51,7 @@ ___
5551

5652
**alias**(newId: *`string`*, options?: *[Options]()*): `Promise`<`void`>
5753

58-
*Defined in [analytics.ts:266](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L266)*
59-
60-
Merge two user identities, effectively connecting two sets of user data as one. This may not be supported by all integrations.
61-
62-
When you learn more about who the group is, you can record that information with group.
54+
*Defined in analytics.ts:266*
6355

6456
**Parameters:**
6557

@@ -77,11 +69,7 @@ ___
7769

7870
**catch**(handler: *[ErrorHandler]()*): `this`
7971

80-
*Defined in [analytics.ts:111](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L111)*
81-
82-
Catch React-Native bridge errors
83-
84-
These errors are emitted when calling the native counterpart. This only applies to methods with no return value (`Promise<void>`), methods like `getAnonymousId` do reject promises.
72+
*Defined in analytics.ts:111*
8573

8674
**Parameters:**
8775

@@ -98,11 +86,7 @@ ___
9886

9987
**disable**(): `Promise`<`void`>
10088

101-
*Defined in [analytics.ts:305](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L305)*
102-
103-
Completely disable the sending of any analytics data.
104-
105-
If you have a way for users to actively or passively (sometimes based on location) opt-out of analytics data collection, you can use this method to turn off all data collection.
89+
*Defined in analytics.ts:305*
10690

10791
**Returns:** `Promise`<`void`>
10892

@@ -113,11 +97,7 @@ ___
11397

11498
**enable**(): `Promise`<`void`>
11599

116-
*Defined in [analytics.ts:295](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L295)*
117-
118-
Enable the sending of analytics data. Enabled by default.
119-
120-
Occasionally used in conjunction with disable user opt-out handling.
100+
*Defined in analytics.ts:295*
121101

122102
**Returns:** `Promise`<`void`>
123103

@@ -128,11 +108,7 @@ ___
128108

129109
**flush**(): `Promise`<`void`>
130110

131-
*Defined in [analytics.ts:286](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L286)*
132-
133-
Trigger an upload of all queued events.
134-
135-
This is useful when you want to force all messages queued on the device to be uploaded. Please note that not all integrations respond to this method.
111+
*Defined in analytics.ts:286*
136112

137113
**Returns:** `Promise`<`void`>
138114

@@ -143,9 +119,7 @@ ___
143119

144120
**getAnonymousId**(): `Promise`<`string`>
145121

146-
*Defined in [analytics.ts:310](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L310)*
147-
148-
Retrieve the anonymousId.
122+
*Defined in analytics.ts:310*
149123

150124
**Returns:** `Promise`<`string`>
151125

@@ -156,11 +130,7 @@ ___
156130

157131
**group**(groupId: *`string`*, traits?: *[JsonMap]()*, options?: *[Options]()*): `Promise`<`void`>
158132

159-
*Defined in [analytics.ts:253](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L253)*
160-
161-
Associate a user with a group, organization, company, project, or w/e _you_ call them.
162-
163-
When you learn more about who the group is, you can record that information with group.
133+
*Defined in analytics.ts:253*
164134

165135
**Parameters:**
166136

@@ -179,11 +149,7 @@ ___
179149

180150
**identify**(user: *`string`*, traits?: *[JsonMap]()*, options?: *[Options]()*): `Promise`<`void`>
181151

182-
*Defined in [analytics.ts:240](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L240)*
183-
184-
Associate a user with their unique ID and record traits about them.
185-
186-
When you learn more about who your user is, you can record that information with identify.
152+
*Defined in analytics.ts:240*
187153

188154
**Parameters:**
189155

@@ -202,28 +168,7 @@ ___
202168

203169
**middleware**(middleware: *[Middleware]()*): `this`
204170

205-
*Defined in [analytics.ts:149](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L149)*
206-
207-
Append a new middleware to the middleware chain.
208-
209-
Middlewares are a powerful mechanism that can augment the events collected by the SDK. A middleware is a simple function that is invoked by the Segment SDK and can be used to monitor, modify or reject events.
210-
211-
Middlewares are invoked for all events, including automatically tracked events, and external event sources like Adjust and Optimizely. This offers you the ability the customize those messages to fit your use case even if the event was sent outside your source code.
212-
213-
The key thing to observe here is that the output produced by the first middleware feeds into the second. This allows you to chain and compose independent middlewares!
214-
215-
For example, you might want to record the device year class with your events. Previously, you would have to do this everywhere you trigger an event with the Segment SDK. With middlewares, you can do this in a single place :
216-
217-
```js
218-
import DeviceYearClass from 'react-native-device-year-class'
219-
220-
analytics.middleware(async ({next, context}) =>
221-
next({
222-
...context,
223-
device_year_class: await DeviceYearClass()
224-
})
225-
)
226-
```
171+
*Defined in analytics.ts:149*
227172

228173
**Parameters:**
229174

@@ -240,11 +185,7 @@ ___
240185

241186
**reset**(): `Promise`<`void`>
242187

243-
*Defined in [analytics.ts:276](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L276)*
244-
245-
Reset any user state that is cached on the device.
246-
247-
This is useful when a user logs out and you want to clear the identity. It will clear any traits or userId's cached on the device.
188+
*Defined in analytics.ts:276*
248189

249190
**Returns:** `Promise`<`void`>
250191

@@ -255,11 +196,7 @@ ___
255196

256197
**screen**(name: *`string`*, properties?: *[JsonMap]()*, options?: *[Options]()*): `Promise`<`void`>
257198

258-
*Defined in [analytics.ts:225](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L225)*
259-
260-
Record the screens or views your users see.
261-
262-
When a user views a screen in your app, you'll want to record that here. For some tools like Google Analytics and Flurry, screen views are treated specially, and are different from "events" kind of like "page views" on the web. For services that don't treat "screen views" specially, we map "screen" straight to "track" with the same parameters. For example, Mixpanel doesn't treat "screen views" any differently. So a call to "screen" will be tracked as a normal event in Mixpanel, but get sent to Google Analytics and Flurry as a "screen".
199+
*Defined in analytics.ts:225*
263200

264201
**Parameters:**
265202

@@ -278,19 +215,7 @@ ___
278215

279216
**setup**(writeKey: *`string`*, configuration?: *[Configuration](../interfaces/analytics.configuration.md)*): `Promise`<`void`>
280217

281-
*Defined in [analytics.ts:188](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L188)*
282-
283-
Setup the Analytics module. All calls made before are queued and only executed if the configuration was successful.
284-
285-
```js
286-
await analytics.setup('YOUR_WRITE_KEY', {
287-
using: [Mixpanel, GoogleAnalytics],
288-
trackAppLifecycleEvents: true,
289-
ios: {
290-
trackDeepLinks: true
291-
}
292-
})
293-
```
218+
*Defined in analytics.ts:188*
294219

295220
**Parameters:**
296221

@@ -308,11 +233,7 @@ ___
308233

309234
**track**(event: *`string`*, properties?: *[JsonMap]()*, options?: *[Options]()*): `Promise`<`void`>
310235

311-
*Defined in [analytics.ts:207](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L207)*
312-
313-
Record the actions your users perform.
314-
315-
When a user performs an action in your app, you'll want to track that action for later analysis. Use the event name to say what the user did, and properties to specify any interesting details of the action.
236+
*Defined in analytics.ts:207*
316237

317238
**Parameters:**
318239

@@ -331,11 +252,7 @@ ___
331252

332253
**useNativeConfiguration**(): `this`
333254

334-
*Defined in [analytics.ts:161](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L161)*
335-
336-
Use the native configuration.
337-
338-
You'll need to call this method when you configure Analytics's singleton using the native API.
255+
*Defined in analytics.ts:161*
339256

340257
**Returns:** `this`
341258

0 commit comments

Comments
 (0)