Skip to content

fix(android): fixed context not passing thru #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ repositories {


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

api 'com.facebook.react:react-native:+'
api 'org.jetbrains.kotlin:kotlin-stdlib:1.3.21'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,6 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
}
}

/**
* Builds Options with integrations
* The format for the integrations variable is { String: Boolean | Map }
*/
private fun getOptions(integrations: ReadableMap?): Options {
var options = Options()

if (integrations !== null) {
val iterator = integrations.keySetIterator()
while (iterator.hasNextKey()) {
val nextKey = iterator.nextKey()
when (integrations.getType(nextKey)) {
ReadableType.Boolean -> options.setIntegration(nextKey, integrations.getBoolean(nextKey))
ReadableType.Map -> {
options.setIntegration(nextKey, true)
options.setIntegrationOptions(nextKey, integrations.getMap(nextKey)?.toHashMap())
}
}
}
}
return options
}

/**
* Tracks application lifecycle events - Application Installed, Application Updated and Application Opened
* This is built to exactly mirror the application lifecycle tracking in analytics-android
Expand Down Expand Up @@ -154,8 +131,8 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM

if(options.hasKey("flushInterval")) {
builder.flushInterval(
options.getInt("flushInterval").toLong(),
TimeUnit.MILLISECONDS
options.getInt("flushInterval").toLong(),
TimeUnit.MILLISECONDS
)
}

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

try {
Analytics.setSingletonInstance(
RNAnalytics.buildWithIntegrations(builder)
RNAnalytics.buildWithIntegrations(builder)
)
} catch(e: Exception) {
return promise.reject("E_SEGMENT_ERROR", e)
Expand All @@ -184,48 +161,84 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
}

@ReactMethod
fun track(event: String, properties: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
analytics.track(event, Properties() from properties, this.getOptions(integrations))
fun track(event: String, properties: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
analytics.track(
event,
Properties() from properties,
optionsFrom(context, integrations)
)

@ReactMethod
fun screen(name: String, properties: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
analytics.screen(null, name, Properties() from properties, this.getOptions(integrations))
fun screen(name: String?, properties: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
analytics.screen(
null,
name,
Properties() from properties,
optionsFrom(context, integrations)
)

@ReactMethod
fun identify(userId: String, traits: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
analytics.identify(userId, Traits() from traits, this.getOptions(integrations))
fun identify(userId: String?, traits: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
analytics.identify(
userId,
Traits() from traits,
optionsFrom(context, integrations)
)

@ReactMethod
fun group(groupId: String, traits: ReadableMap, integrations: ReadableMap, context: ReadableMap) =
analytics.group(groupId, Traits() from traits, this.getOptions(integrations))
fun group(groupId: String, traits: ReadableMap?, integrations: ReadableMap, context: ReadableMap) =
analytics.group(
groupId,
Traits() from traits,
optionsFrom(context, integrations)
)

@ReactMethod
fun alias(newId: String, context: ReadableMap, integrations: ReadableMap) =
analytics.alias(newId, this.getOptions(integrations))
fun alias(newId: String, integrations: ReadableMap?, context: ReadableMap?) =
analytics.alias(
newId,
optionsFrom(context, integrations)
)

@ReactMethod
fun reset() =
analytics.reset()
analytics.reset()

@ReactMethod()
fun flush() =
analytics.flush()
analytics.flush()

@ReactMethod
fun enable() =
analytics.optOut(false)
analytics.optOut(false)

@ReactMethod
fun disable() =
analytics.optOut(true)
analytics.optOut(true)

@ReactMethod
fun getAnonymousId(promise: Promise) =
promise.resolve(analytics.getAnalyticsContext().traits().anonymousId())
promise.resolve(analytics.getAnalyticsContext().traits().anonymousId())
}

private infix fun<T: ValueMap> T.from(source: ReadableMap): T {
putAll(source.toHashMap())
private fun optionsFrom(context: ReadableMap?, integrations: ReadableMap?): Options {
var options = Options()

context?.toHashMap()?.forEach { (key, value) ->
options.putContext(key, value)
}

integrations?.toHashMap()?.forEach { (key, value) ->
options.setIntegration(key, value.toString().toBoolean())
}

return options
}

private infix fun<T: ValueMap> T.from(source: ReadableMap?): T {
if (source != null) {
putAll(source.toHashMap())
}

return this
}
2 changes: 1 addition & 1 deletion packages/core/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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

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

___

113 changes: 15 additions & 98 deletions packages/core/docs/classes/analytics.client.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@

**● ready**: *`false`* = false

*Defined in [analytics.ts:96](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L96)*

Whether the client is ready to send events to Segment.

This becomes `true` when `.setup()` succeeds. All calls will be queued until it becomes `true`.
*Defined in analytics.ts:96*

___

Expand All @@ -55,11 +51,7 @@ ___

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

*Defined in [analytics.ts:266](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L266)*

Merge two user identities, effectively connecting two sets of user data as one. This may not be supported by all integrations.

When you learn more about who the group is, you can record that information with group.
*Defined in analytics.ts:266*

**Parameters:**

Expand All @@ -77,11 +69,7 @@ ___

▸ **catch**(handler: *[ErrorHandler]()*): `this`

*Defined in [analytics.ts:111](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L111)*

Catch React-Native bridge errors

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.
*Defined in analytics.ts:111*

**Parameters:**

Expand All @@ -98,11 +86,7 @@ ___

▸ **disable**(): `Promise`<`void`>

*Defined in [analytics.ts:305](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L305)*

Completely disable the sending of any analytics data.

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.
*Defined in analytics.ts:305*

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

Expand All @@ -113,11 +97,7 @@ ___

▸ **enable**(): `Promise`<`void`>

*Defined in [analytics.ts:295](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L295)*

Enable the sending of analytics data. Enabled by default.

Occasionally used in conjunction with disable user opt-out handling.
*Defined in analytics.ts:295*

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

Expand All @@ -128,11 +108,7 @@ ___

▸ **flush**(): `Promise`<`void`>

*Defined in [analytics.ts:286](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L286)*

Trigger an upload of all queued events.

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.
*Defined in analytics.ts:286*

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

Expand All @@ -143,9 +119,7 @@ ___

▸ **getAnonymousId**(): `Promise`<`string`>

*Defined in [analytics.ts:310](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L310)*

Retrieve the anonymousId.
*Defined in analytics.ts:310*

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

Expand All @@ -156,11 +130,7 @@ ___

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

*Defined in [analytics.ts:253](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L253)*

Associate a user with a group, organization, company, project, or w/e _you_ call them.

When you learn more about who the group is, you can record that information with group.
*Defined in analytics.ts:253*

**Parameters:**

Expand All @@ -179,11 +149,7 @@ ___

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

*Defined in [analytics.ts:240](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L240)*

Associate a user with their unique ID and record traits about them.

When you learn more about who your user is, you can record that information with identify.
*Defined in analytics.ts:240*

**Parameters:**

Expand All @@ -202,28 +168,7 @@ ___

▸ **middleware**(middleware: *[Middleware]()*): `this`

*Defined in [analytics.ts:149](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L149)*

Append a new middleware to the middleware chain.

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.

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.

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!

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 :

```js
import DeviceYearClass from 'react-native-device-year-class'

analytics.middleware(async ({next, context}) =>
next({
...context,
device_year_class: await DeviceYearClass()
})
)
```
*Defined in analytics.ts:149*

**Parameters:**

Expand All @@ -240,11 +185,7 @@ ___

▸ **reset**(): `Promise`<`void`>

*Defined in [analytics.ts:276](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L276)*

Reset any user state that is cached on the device.

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.
*Defined in analytics.ts:276*

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

Expand All @@ -255,11 +196,7 @@ ___

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

*Defined in [analytics.ts:225](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L225)*

Record the screens or views your users see.

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".
*Defined in analytics.ts:225*

**Parameters:**

Expand All @@ -278,19 +215,7 @@ ___

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

*Defined in [analytics.ts:188](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L188)*

Setup the Analytics module. All calls made before are queued and only executed if the configuration was successful.

```js
await analytics.setup('YOUR_WRITE_KEY', {
using: [Mixpanel, GoogleAnalytics],
trackAppLifecycleEvents: true,
ios: {
trackDeepLinks: true
}
})
```
*Defined in analytics.ts:188*

**Parameters:**

Expand All @@ -308,11 +233,7 @@ ___

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

*Defined in [analytics.ts:207](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L207)*

Record the actions your users perform.

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.
*Defined in analytics.ts:207*

**Parameters:**

Expand All @@ -331,11 +252,7 @@ ___

▸ **useNativeConfiguration**(): `this`

*Defined in [analytics.ts:161](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L161)*

Use the native configuration.

You'll need to call this method when you configure Analytics's singleton using the native API.
*Defined in analytics.ts:161*

**Returns:** `this`

Expand Down
Loading