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

Commit

Permalink
feat(core): add middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb committed Oct 2, 2018
1 parent c229fb0 commit 9aeafa3
Show file tree
Hide file tree
Showing 21 changed files with 468 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.segment.analytics.Analytics
import com.segment.analytics.Properties
import com.segment.analytics.Traits
import com.segment.analytics.ValueMap
import java.util.concurrent.TimeUnit

class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaModule(context) {
private val analytics
Expand All @@ -58,6 +59,13 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
builder.trackAttributionInformation()
}

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

if(options.getBoolean("debug")) {
builder.logLevel(Analytics.LogLevel.VERBOSE)
}
Expand All @@ -68,23 +76,23 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
}

@ReactMethod
fun track(event: String, properties: ReadableMap) =
fun track(event: String, properties: ReadableMap, context: ReadableMap) =
analytics.track(event, Properties() from properties)

@ReactMethod
fun screen(name: String, properties: ReadableMap) =
fun screen(name: String, properties: ReadableMap, context: ReadableMap) =
analytics.screen(name, Properties() from properties)

@ReactMethod
fun identify(userId: String, traits: ReadableMap) =
fun identify(userId: String, traits: ReadableMap, context: ReadableMap) =
analytics.identify(userId, Traits() from traits, null)

@ReactMethod
fun group(groupId: String, traits: ReadableMap) =
fun group(groupId: String, traits: ReadableMap, context: ReadableMap) =
analytics.group(groupId, Traits() from traits)

@ReactMethod
fun alias(newId: String) =
fun alias(newId: String, context: ReadableMap) =
analytics.alias(newId)

@ReactMethod
Expand Down
4 changes: 2 additions & 2 deletions packages/core/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
**Ƭ Integration**: * `function` | `object`
*

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

___
<a id="writekey"></a>
Expand All @@ -36,7 +36,7 @@ ___
**Ƭ WriteKey**: * `string` &#124; `object`
*

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

___

86 changes: 52 additions & 34 deletions packages/core/docs/classes/analytics.client.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* [flush](analytics.client.md#flush)
* [group](analytics.client.md#group)
* [identify](analytics.client.md#identify)
* [middleware](analytics.client.md#middleware)
* [reset](analytics.client.md#reset)
* [screen](analytics.client.md#screen)
* [track](analytics.client.md#track)
Expand All @@ -36,7 +37,7 @@

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

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

Whether the client is ready to send events to Segment.

Expand All @@ -50,9 +51,9 @@ ___

### alias

**alias**(newId: *`string`*): `this`
**alias**(newId: *`string`*): `Promise`<`void`>

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

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

Expand All @@ -64,7 +65,7 @@ When you learn more about who the group is, you can record that information with
| ------ | ------ | ------ |
| newId | `string` | The new ID you want to alias the existing ID to. The existing ID will be either the previousId if you have called identify, or the anonymous ID. |

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

___
<a id="catch"></a>
Expand All @@ -73,7 +74,7 @@ ___

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

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

Catch React-Native bridge errors

Expand All @@ -94,7 +95,7 @@ ___

**configure**(): [Configuration](../interfaces/analytics.chainedconfiguration.configuration.md)

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

Configure the Analytics module.

Expand All @@ -117,54 +118,54 @@ ___

### disable

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

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

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.

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

___
<a id="enable"></a>

### enable

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

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

Enable the sending of analytics data. Enabled by default.

Occasionally used in conjunction with disable user opt-out handling.

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

___
<a id="flush"></a>

### flush

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

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

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.

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

___
<a id="group"></a>

### group

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

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

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

Expand All @@ -175,18 +176,18 @@ When you learn more about who the group is, you can record that information with
| Param | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| groupId | `string` | - | A database ID for this group. |
| `Default value` traits | [JsonMap]() | {} | A dictionary of traits you know about the group. Things like: name, employees, etc. |
| `Default value` traits | `JsonMap` | {} | A dictionary of traits you know about the group. Things like: name, employees, etc. |

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

___
<a id="identify"></a>

### identify

**identify**(userId: *`string`*, traits?: *[JsonMap]()*): `this`
**identify**(user: *`string`*, traits?: *`JsonMap`*): `Promise`<`void`>

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

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

Expand All @@ -196,8 +197,25 @@ When you learn more about who your user is, you can record that information with

| Param | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| userId | `string` | - | database ID (or email address) for this user. If you don't have a userId but want to record traits, you should pass nil. For more information on how we generate the UUID and Apple's policies on IDs, see [https://segment.io/libraries/ios#ids](https://segment.io/libraries/ios#ids) |
| `Default value` traits | [JsonMap]() | {} | A dictionary of traits you know about the user. Things like: email, name, plan, etc. |
| user | `string` | - | database ID (or email address) for this user. If you don't have a userId but want to record traits, you should pass nil. For more information on how we generate the UUID and Apple's policies on IDs, see [https://segment.io/libraries/ios#ids](https://segment.io/libraries/ios#ids) |
| `Default value` traits | `JsonMap` | {} | A dictionary of traits you know about the user. Things like: email, name, plan, etc. |

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

___
<a id="middleware"></a>

### middleware

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

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

**Parameters:**

| Param | Type |
| ------ | ------ |
| middleware | [Middleware]() |

**Returns:** `this`

Expand All @@ -206,24 +224,24 @@ ___

### reset

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

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

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.

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

___
<a id="screen"></a>

### screen

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

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

Record the screens or views your users see.

Expand All @@ -234,18 +252,18 @@ When a user views a screen in your app, you'll want to record that here. For som
| Param | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| name | `string` | - | The title of the screen being viewed. We recommend using human-readable names like 'Photo Feed' or 'Completed Purchase Screen'. |
| `Default value` properties | [JsonMap]() | {} | A dictionary of properties for the screen view event. If the event was 'Added to Shopping Cart', it might have properties like price, productType, etc. |
| `Default value` properties | `JsonMap` | {} | A dictionary of properties for the screen view event. If the event was 'Added to Shopping Cart', it might have properties like price, productType, etc. |

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

___
<a id="track"></a>

### track

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

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

Record the actions your users perform.

Expand All @@ -256,9 +274,9 @@ When a user performs an action in your app, you'll want to track that action for
| Param | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| event | `string` | - | The name of the event you're tracking. We recommend using human-readable names like \`Played a Song\` or \`Updated Status\`. |
| `Default value` properties | [JsonMap]() | {} | A dictionary of properties for the event. If the event was 'Added to Shopping Cart', it might have properties like price, productType, etc. |
| `Default value` properties | `JsonMap` | {} | A dictionary of properties for the event. If the event was 'Added to Shopping Cart', it might have properties like price, productType, etc. |

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

___

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

* [android](analytics.chainedconfiguration.android.md#android)
* [disableDeviceId](analytics.chainedconfiguration.android.md#disabledeviceid)
* [flushInterval](analytics.chainedconfiguration.android.md#flushinterval)
* [ios](analytics.chainedconfiguration.android.md#ios)
* [setup](analytics.chainedconfiguration.android.md#setup)

Expand All @@ -29,7 +30,7 @@

*Inherited from [Base](analytics.chainedconfiguration.base.md).[android](analytics.chainedconfiguration.base.md#android)*

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

Access Android specific settings

Expand All @@ -42,7 +43,7 @@ ___

**disableDeviceId**(): `this`

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

Disable the collection of the device identifier. Enabled by default.

Expand All @@ -54,6 +55,25 @@ The device identifier is obtained using :

**Returns:** `this`

___
<a id="flushinterval"></a>

### flushInterval

**flushInterval**(every: *`number`*): `this`

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

Set the interval at which the client should flush events. The client will automatically flush events to Segment every [flushInterval](analytics.chainedconfiguration.android.md#flushinterval) duration, regardless of [flushAt](analytics.chainedconfiguration.configuration.md#flushat).

**Parameters:**

| Param | Type | Description |
| ------ | ------ | ------ |
| every | `number` | the interval in milliseconds |

**Returns:** `this`

___
<a id="ios"></a>

Expand All @@ -63,7 +83,7 @@ ___

*Inherited from [Base](analytics.chainedconfiguration.base.md).[ios](analytics.chainedconfiguration.base.md#ios)*

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

Access iOS specific settings

Expand All @@ -78,7 +98,7 @@ ___

*Inherited from [Base](analytics.chainedconfiguration.base.md).[setup](analytics.chainedconfiguration.base.md#setup)*

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

Finalize the configuration and initialize the Analytics client.

Expand Down
Loading

0 comments on commit 9aeafa3

Please sign in to comment.