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

Commit 9aeafa3

Browse files
committed
feat(core): add middlewares
1 parent c229fb0 commit 9aeafa3

File tree

21 files changed

+468
-156
lines changed

21 files changed

+468
-156
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import com.segment.analytics.Analytics
3232
import com.segment.analytics.Properties
3333
import com.segment.analytics.Traits
3434
import com.segment.analytics.ValueMap
35+
import java.util.concurrent.TimeUnit
3536

3637
class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaModule(context) {
3738
private val analytics
@@ -58,6 +59,13 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
5859
builder.trackAttributionInformation()
5960
}
6061

62+
if(options.hasKey("flushInterval")) {
63+
builder.flushInterval(
64+
options.getInt("flushInterval").toLong(),
65+
TimeUnit.MILLISECONDS
66+
)
67+
}
68+
6169
if(options.getBoolean("debug")) {
6270
builder.logLevel(Analytics.LogLevel.VERBOSE)
6371
}
@@ -68,23 +76,23 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
6876
}
6977

7078
@ReactMethod
71-
fun track(event: String, properties: ReadableMap) =
79+
fun track(event: String, properties: ReadableMap, context: ReadableMap) =
7280
analytics.track(event, Properties() from properties)
7381

7482
@ReactMethod
75-
fun screen(name: String, properties: ReadableMap) =
83+
fun screen(name: String, properties: ReadableMap, context: ReadableMap) =
7684
analytics.screen(name, Properties() from properties)
7785

7886
@ReactMethod
79-
fun identify(userId: String, traits: ReadableMap) =
87+
fun identify(userId: String, traits: ReadableMap, context: ReadableMap) =
8088
analytics.identify(userId, Traits() from traits, null)
8189

8290
@ReactMethod
83-
fun group(groupId: String, traits: ReadableMap) =
91+
fun group(groupId: String, traits: ReadableMap, context: ReadableMap) =
8492
analytics.group(groupId, Traits() from traits)
8593

8694
@ReactMethod
87-
fun alias(newId: String) =
95+
fun alias(newId: String, context: ReadableMap) =
8896
analytics.alias(newId)
8997

9098
@ReactMethod

packages/core/docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
**Ƭ Integration**: * `function` | `object`
2727
*
2828

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

3131
___
3232
<a id="writekey"></a>
@@ -36,7 +36,7 @@ ___
3636
**Ƭ WriteKey**: * `string` &#124; `object`
3737
*
3838

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

4141
___
4242

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

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [flush](analytics.client.md#flush)
2323
* [group](analytics.client.md#group)
2424
* [identify](analytics.client.md#identify)
25+
* [middleware](analytics.client.md#middleware)
2526
* [reset](analytics.client.md#reset)
2627
* [screen](analytics.client.md#screen)
2728
* [track](analytics.client.md#track)
@@ -36,7 +37,7 @@
3637

3738
**● ready**: *`false`* = false
3839

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

4142
Whether the client is ready to send events to Segment.
4243

@@ -50,9 +51,9 @@ ___
5051

5152
### alias
5253

53-
**alias**(newId: *`string`*): `this`
54+
**alias**(newId: *`string`*): `Promise`<`void`>
5455

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

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

@@ -64,7 +65,7 @@ When you learn more about who the group is, you can record that information with
6465
| ------ | ------ | ------ |
6566
| 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. |
6667

67-
**Returns:** `this`
68+
**Returns:** `Promise`<`void`>
6869

6970
___
7071
<a id="catch"></a>
@@ -73,7 +74,7 @@ ___
7374

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

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

7879
Catch React-Native bridge errors
7980

@@ -94,7 +95,7 @@ ___
9495

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

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

99100
Configure the Analytics module.
100101

@@ -117,54 +118,54 @@ ___
117118

118119
### disable
119120

120-
**disable**(): `this`
121+
**disable**(): `Promise`<`void`>
121122

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

124125
Completely disable the sending of any analytics data.
125126

126127
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.
127128

128-
**Returns:** `this`
129+
**Returns:** `Promise`<`void`>
129130

130131
___
131132
<a id="enable"></a>
132133

133134
### enable
134135

135-
**enable**(): `this`
136+
**enable**(): `Promise`<`void`>
136137

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

139140
Enable the sending of analytics data. Enabled by default.
140141

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

143-
**Returns:** `this`
144+
**Returns:** `Promise`<`void`>
144145

145146
___
146147
<a id="flush"></a>
147148

148149
### flush
149150

150-
**flush**(): `this`
151+
**flush**(): `Promise`<`void`>
151152

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

154155
Trigger an upload of all queued events.
155156

156157
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.
157158

158-
**Returns:** `this`
159+
**Returns:** `Promise`<`void`>
159160

160161
___
161162
<a id="group"></a>
162163

163164
### group
164165

165-
**group**(groupId: *`string`*, traits?: *[JsonMap]()*): `this`
166+
**group**(groupId: *`string`*, traits?: *`JsonMap`*): `Promise`<`void`>
166167

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

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

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

180-
**Returns:** `this`
181+
**Returns:** `Promise`<`void`>
181182

182183
___
183184
<a id="identify"></a>
184185

185186
### identify
186187

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

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

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

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

197198
| Param | Type | Default value | Description |
198199
| ------ | ------ | ------ | ------ |
199-
| 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) |
200-
| `Default value` traits | [JsonMap]() | {} | A dictionary of traits you know about the user. Things like: email, name, plan, etc. |
200+
| 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) |
201+
| `Default value` traits | `JsonMap` | {} | A dictionary of traits you know about the user. Things like: email, name, plan, etc. |
202+
203+
**Returns:** `Promise`<`void`>
204+
205+
___
206+
<a id="middleware"></a>
207+
208+
### middleware
209+
210+
**middleware**(middleware: *[Middleware]()*): `this`
211+
212+
*Defined in [analytics.ts:33](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L33)*
213+
214+
**Parameters:**
215+
216+
| Param | Type |
217+
| ------ | ------ |
218+
| middleware | [Middleware]() |
201219

202220
**Returns:** `this`
203221

@@ -206,24 +224,24 @@ ___
206224

207225
### reset
208226

209-
**reset**(): `this`
227+
**reset**(): `Promise`<`void`>
210228

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

213231
Reset any user state that is cached on the device.
214232

215233
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.
216234

217-
**Returns:** `this`
235+
**Returns:** `Promise`<`void`>
218236

219237
___
220238
<a id="screen"></a>
221239

222240
### screen
223241

224-
**screen**(name: *`string`*, properties?: *[JsonMap]()*): `this`
242+
**screen**(name: *`string`*, properties?: *`JsonMap`*): `Promise`<`void`>
225243

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

228246
Record the screens or views your users see.
229247

@@ -234,18 +252,18 @@ When a user views a screen in your app, you'll want to record that here. For som
234252
| Param | Type | Default value | Description |
235253
| ------ | ------ | ------ | ------ |
236254
| name | `string` | - | The title of the screen being viewed. We recommend using human-readable names like 'Photo Feed' or 'Completed Purchase Screen'. |
237-
| `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. |
255+
| `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. |
238256

239-
**Returns:** `this`
257+
**Returns:** `Promise`<`void`>
240258

241259
___
242260
<a id="track"></a>
243261

244262
### track
245263

246-
**track**(event: *`string`*, properties?: *[JsonMap]()*): `this`
264+
**track**(event: *`string`*, properties?: *`JsonMap`*): `Promise`<`void`>
247265

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

250268
Record the actions your users perform.
251269

@@ -256,9 +274,9 @@ When a user performs an action in your app, you'll want to track that action for
256274
| Param | Type | Default value | Description |
257275
| ------ | ------ | ------ | ------ |
258276
| event | `string` | - | The name of the event you're tracking. We recommend using human-readable names like \`Played a Song\` or \`Updated Status\`. |
259-
| `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. |
277+
| `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. |
260278

261-
**Returns:** `this`
279+
**Returns:** `Promise`<`void`>
262280

263281
___
264282

packages/core/docs/interfaces/analytics.chainedconfiguration.android.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
* [android](analytics.chainedconfiguration.android.md#android)
1616
* [disableDeviceId](analytics.chainedconfiguration.android.md#disabledeviceid)
17+
* [flushInterval](analytics.chainedconfiguration.android.md#flushinterval)
1718
* [ios](analytics.chainedconfiguration.android.md#ios)
1819
* [setup](analytics.chainedconfiguration.android.md#setup)
1920

@@ -29,7 +30,7 @@
2930

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

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

3435
Access Android specific settings
3536

@@ -42,7 +43,7 @@ ___
4243

4344
**disableDeviceId**(): `this`
4445

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

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

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

5556
**Returns:** `this`
5657

58+
___
59+
<a id="flushinterval"></a>
60+
61+
### flushInterval
62+
63+
**flushInterval**(every: *`number`*): `this`
64+
65+
*Defined in [analytics.ts:264](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L264)*
66+
67+
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).
68+
69+
**Parameters:**
70+
71+
| Param | Type | Description |
72+
| ------ | ------ | ------ |
73+
| every | `number` | the interval in milliseconds |
74+
75+
**Returns:** `this`
76+
5777
___
5878
<a id="ios"></a>
5979

@@ -63,7 +83,7 @@ ___
6383

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

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

6888
Access iOS specific settings
6989

@@ -78,7 +98,7 @@ ___
7898

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

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

83103
Finalize the configuration and initialize the Analytics client.
84104

0 commit comments

Comments
 (0)