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

Commit 6a281f4

Browse files
committed
feat(client): use object-based configuration (segmentio#7)
BREAKING CHANGE: We've dropped the chained configuration for an object one instead. This will make Analytics blend even better with tools like Prettier. Before: ```js analytics .configure() .using(Mixpanel, GoogleAnalytics) .recordScreenViews() .trackAppLifecycleEvents() .trackAttributionData() .android() .flushInterval(60) .disableDevicedId() .ios() .trackAdvertising() .trackDeepLinks() .setup("writeKey") .then(() => console.log('Analytics is ready') ) .catch(err => console.error('Something went wrong', err) ) ``` Now: ```js analytics .setup('writeKey', { using: [Mixpanel, GoogleAnalytics], recordScreenViews: true, trackAppLifecycleEvents: true, trackAttributionData: true, android: { flushInterval: 60, collectDeviceId: false }, ios: { trackAdvertising: true, trackDeepLinks: true } }) .then(() => console.log('Analytics is ready') ) .catch(err => console.error('Something went wrong', err) ) ```
1 parent 365827f commit 6a281f4

20 files changed

+394
-827
lines changed

.prettierignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
packages/core/docs
1+
packages/core/docs/
2+
build/
3+
node_modules/

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@ import Mixpanel from '@segment/analytics-react-native-mixpanel'
3737
import GoogleAnalytics from '@segment/analytics-react-native-google-analytics'
3838

3939
analytics
40-
.configure()
41-
.using(Mixpanel, GoogleAnalytics)
42-
.recordScreenViews()
43-
.trackAppLifecycleEvents()
44-
.trackAttributionData()
45-
.ios()
46-
.trackAdvertising()
47-
.trackDeepLinks()
48-
.android()
49-
.disableDevicedId()
50-
.setup("writeKey")
40+
.setup('writeKey', {
41+
using: [Mixpanel, GoogleAnalytics],
42+
recordScreenViews: true,
43+
trackAppLifecycleEvents: true,
44+
trackAttributionData: true,
45+
46+
android: {
47+
flushInterval: 60,
48+
collectDeviceId: true
49+
},
50+
ios: {
51+
trackAdvertising: true,
52+
trackDeepLinks: true
53+
}
54+
})
5155
.then(() =>
5256
console.log('Analytics is ready')
5357
)
@@ -94,11 +98,9 @@ In your code :
9498
import analytics from '@segment/analytics-react-native'
9599
import GoogleAnalytics from '@segment/analytics-react-native-google-analytics'
96100

97-
await analytics
98-
.configure()
99-
.using(GoogleAnalytics)
100-
// ...
101-
.setup('writeKey')
101+
await analytics.setup('writeKey', {
102+
using: [GoogleAnalytics]
103+
})
102104
```
103105

104106
#### Integrations

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
4242

4343
@ReactMethod
4444
fun setup(options: ReadableMap) {
45-
val android = options.getMap("android")
4645
val builder = Analytics
47-
.Builder(reactApplicationContext, android.getString("writeKey"))
46+
.Builder(reactApplicationContext, options.getString("writeKey"))
4847
.flushQueueSize(options.getInt("flushAt"))
4948

5049
if(options.getBoolean("recordScreenViews")) {

packages/core/docs/README.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
## Index
44

5-
### Modules
6-
7-
* [ChainedConfiguration](modules/analytics.chainedconfiguration.md)
8-
95
### Classes
106

117
* [Client](classes/analytics.client.md)
128

9+
### Interfaces
10+
11+
* [Configuration](interfaces/analytics.configuration.md)
12+
1313
### Type aliases
1414

1515
* [Integration](#integration)
16-
* [WriteKey](#writekey)
1716

1817
---
1918

@@ -26,17 +25,7 @@
2625
**Ƭ Integration**: * `function` | `object`
2726
*
2827

29-
*Defined in analytics.ts:229*
30-
31-
___
32-
<a id="writekey"></a>
33-
34-
### WriteKey
35-
36-
**Ƭ WriteKey**: * `string` &#124; `object`
37-
*
38-
39-
*Defined in analytics.ts:231*
28+
*Defined in [analytics.ts:8](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L8)*
4029

4130
___
4231

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

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
* [alias](analytics.client.md#alias)
1818
* [catch](analytics.client.md#catch)
19-
* [configure](analytics.client.md#configure)
2019
* [disable](analytics.client.md#disable)
2120
* [enable](analytics.client.md#enable)
2221
* [flush](analytics.client.md#flush)
@@ -25,6 +24,7 @@
2524
* [middleware](analytics.client.md#middleware)
2625
* [reset](analytics.client.md#reset)
2726
* [screen](analytics.client.md#screen)
27+
* [setup](analytics.client.md#setup)
2828
* [track](analytics.client.md#track)
2929
* [useNativeConfiguration](analytics.client.md#usenativeconfiguration)
3030

@@ -38,7 +38,7 @@
3838

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

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

4343
Whether the client is ready to send events to Segment.
4444

@@ -54,7 +54,7 @@ ___
5454

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

57-
*Defined in analytics.ts:174*
57+
*Defined in [analytics.ts:260](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L260)*
5858

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

@@ -75,7 +75,7 @@ ___
7575

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

78-
*Defined in analytics.ts:28*
78+
*Defined in [analytics.ts:109](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L109)*
7979

8080
Catch React-Native bridge errors
8181

@@ -89,39 +89,14 @@ These errors are emitted when calling the native counterpart.
8989

9090
**Returns:** `this`
9191

92-
___
93-
<a id="configure"></a>
94-
95-
### configure
96-
97-
**configure**(): [Configuration](../interfaces/analytics.chainedconfiguration.configuration.md)
98-
99-
*Defined in analytics.ts:102*
100-
101-
Configure the Analytics module.
102-
103-
This method returns a fluent-style API to configure the SDK :
104-
105-
```js
106-
analytics
107-
.configure()
108-
.using(Mixpanel, GoogleAnalytics)
109-
.trackAppLifecycle()
110-
.ios()
111-
.trackDeepLinks()
112-
.setup("YOUR_WRITE_KEY")
113-
```
114-
115-
**Returns:** [Configuration](../interfaces/analytics.chainedconfiguration.configuration.md)
116-
11792
___
11893
<a id="disable"></a>
11994

12095
### disable
12196

12297
**disable**(): `Promise`<`void`>
12398

124-
*Defined in analytics.ts:213*
99+
*Defined in [analytics.ts:299](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L299)*
125100

126101
Completely disable the sending of any analytics data.
127102

@@ -136,7 +111,7 @@ ___
136111

137112
**enable**(): `Promise`<`void`>
138113

139-
*Defined in analytics.ts:203*
114+
*Defined in [analytics.ts:289](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L289)*
140115

141116
Enable the sending of analytics data. Enabled by default.
142117

@@ -151,7 +126,7 @@ ___
151126

152127
**flush**(): `Promise`<`void`>
153128

154-
*Defined in analytics.ts:194*
129+
*Defined in [analytics.ts:280](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L280)*
155130

156131
Trigger an upload of all queued events.
157132

@@ -166,7 +141,7 @@ ___
166141

167142
**group**(groupId: *`string`*, traits?: *`JsonMap`*): `Promise`<`void`>
168143

169-
*Defined in analytics.ts:161*
144+
*Defined in [analytics.ts:247](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L247)*
170145

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

@@ -188,7 +163,7 @@ ___
188163

189164
**identify**(user: *`string`*, traits?: *`JsonMap`*): `Promise`<`void`>
190165

191-
*Defined in analytics.ts:149*
166+
*Defined in [analytics.ts:235](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L235)*
192167

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

@@ -210,7 +185,7 @@ ___
210185

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

213-
*Defined in analytics.ts:66*
188+
*Defined in [analytics.ts:147](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L147)*
214189

215190
Append a new middleware to the middleware chain.
216191

@@ -248,7 +223,7 @@ ___
248223

249224
**reset**(): `Promise`<`void`>
250225

251-
*Defined in analytics.ts:184*
226+
*Defined in [analytics.ts:270](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L270)*
252227

253228
Reset any user state that is cached on the device.
254229

@@ -263,7 +238,7 @@ ___
263238

264239
**screen**(name: *`string`*, properties?: *`JsonMap`*): `Promise`<`void`>
265240

266-
*Defined in analytics.ts:135*
241+
*Defined in [analytics.ts:221](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L221)*
267242

268243
Record the screens or views your users see.
269244

@@ -278,14 +253,44 @@ When a user views a screen in your app, you'll want to record that here. For som
278253

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

256+
___
257+
<a id="setup"></a>
258+
259+
### setup
260+
261+
**setup**(writeKey: *`string`*, configuration?: *[Configuration](../interfaces/analytics.configuration.md)*): `Promise`<`void`>
262+
263+
*Defined in [analytics.ts:186](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L186)*
264+
265+
Setup the Analytics module. All calls made before are queued and executed if the configuration was successful.
266+
267+
```js
268+
await analytics.setup('YOUR_WRITE_KEY', {
269+
using: [Mixpanel, GoogleAnalytics],
270+
trackAppLifecycleEvents: true,
271+
ios: {
272+
trackDeepLinks: true
273+
}
274+
})
275+
```
276+
277+
**Parameters:**
278+
279+
| Param | Type | Default value | Description |
280+
| ------ | ------ | ------ | ------ |
281+
| writeKey | `string` | - | Your Segment.io write key |
282+
| `Default value` configuration | [Configuration](../interfaces/analytics.configuration.md) | {} | An optional [Configuration](../interfaces/analytics.configuration.md) object. |
283+
284+
**Returns:** `Promise`<`void`>
285+
281286
___
282287
<a id="track"></a>
283288

284289
### track
285290

286291
**track**(event: *`string`*, properties?: *`JsonMap`*): `Promise`<`void`>
287292

288-
*Defined in analytics.ts:117*
293+
*Defined in [analytics.ts:203](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L203)*
289294

290295
Record the actions your users perform.
291296

@@ -307,7 +312,7 @@ ___
307312

308313
**useNativeConfiguration**(): `this`
309314

310-
*Defined in analytics.ts:78*
315+
*Defined in [analytics.ts:159](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L159)*
311316

312317
Use the native configuration.
313318

0 commit comments

Comments
 (0)