Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit cb1b412

Browse files
bsneedBrandon Sneed
and
Brandon Sneed
authored
fix(android): fixed context not passing thru (segmentio#147)
* fix(android): fixed context not passing thru * fix(ts): made linter happy * fix(android): adjust gradle project identifiers Co-authored-by: Brandon Sneed <[email protected]>
1 parent 3c29a88 commit cb1b412

File tree

8 files changed

+97
-182
lines changed

8 files changed

+97
-182
lines changed

packages/core/android/build.gradle

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

4040

4141
dependencies {
42-
api 'com.segment.analytics.android:analytics:4.+'
42+
api 'com.segment.analytics.android:analytics:4.5.0-beta.0'
4343

4444
api 'com.facebook.react:react-native:+'
4545
api "org.jetbrains.kotlin:kotlin-stdlib:${safeExtGet("kotlinVersion", defaultKotlinVersion)}"

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(e2: IllegalStateException) {
175152
// pass if the error is due to calling setSingletonInstance multiple times
@@ -189,48 +166,84 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
189166
}
190167

191168
@ReactMethod
192-
fun track(event: String, properties: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
193-
analytics.track(event, Properties() from properties, this.getOptions(integrations))
169+
fun track(event: String, properties: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
170+
analytics.track(
171+
event,
172+
Properties() from properties,
173+
optionsFrom(context, integrations)
174+
)
194175

195176
@ReactMethod
196-
fun screen(name: String, properties: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
197-
analytics.screen(null, name, Properties() from properties, this.getOptions(integrations))
177+
fun screen(name: String?, properties: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
178+
analytics.screen(
179+
null,
180+
name,
181+
Properties() from properties,
182+
optionsFrom(context, integrations)
183+
)
198184

199185
@ReactMethod
200-
fun identify(userId: String, traits: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
201-
analytics.identify(userId, Traits() from traits, this.getOptions(integrations))
186+
fun identify(userId: String?, traits: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
187+
analytics.identify(
188+
userId,
189+
Traits() from traits,
190+
optionsFrom(context, integrations)
191+
)
202192

203193
@ReactMethod
204-
fun group(groupId: String, traits: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
205-
analytics.group(groupId, Traits() from traits, this.getOptions(integrations))
194+
fun group(groupId: String, traits: ReadableMap?, integrations: ReadableMap, context: ReadableMap) =
195+
analytics.group(
196+
groupId,
197+
Traits() from traits,
198+
optionsFrom(context, integrations)
199+
)
206200

207201
@ReactMethod
208-
fun alias(newId: String, context: ReadableMap, integrations: ReadableMap) =
209-
analytics.alias(newId, this.getOptions(integrations))
202+
fun alias(newId: String, integrations: ReadableMap?, context: ReadableMap?) =
203+
analytics.alias(
204+
newId,
205+
optionsFrom(context, integrations)
206+
)
210207

211208
@ReactMethod
212209
fun reset() =
213-
analytics.reset()
210+
analytics.reset()
214211

215212
@ReactMethod()
216213
fun flush() =
217-
analytics.flush()
214+
analytics.flush()
218215

219216
@ReactMethod
220217
fun enable() =
221-
analytics.optOut(false)
218+
analytics.optOut(false)
222219

223220
@ReactMethod
224221
fun disable() =
225-
analytics.optOut(true)
222+
analytics.optOut(true)
226223

227224
@ReactMethod
228225
fun getAnonymousId(promise: Promise) =
229-
promise.resolve(analytics.getAnalyticsContext().traits().anonymousId())
226+
promise.resolve(analytics.getAnalyticsContext().traits().anonymousId())
230227
}
231228

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

235248
return this
236249
}

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)