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

Commit

Permalink
feat(client): use object-based configuration (segmentio#7)
Browse files Browse the repository at this point in the history
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)
    )
```
  • Loading branch information
fathyb committed Oct 23, 2018
1 parent 365827f commit 6a281f4
Show file tree
Hide file tree
Showing 20 changed files with 394 additions and 827 deletions.
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
packages/core/docs
packages/core/docs/
build/
node_modules/
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ import Mixpanel from '@segment/analytics-react-native-mixpanel'
import GoogleAnalytics from '@segment/analytics-react-native-google-analytics'

analytics
.configure()
.using(Mixpanel, GoogleAnalytics)
.recordScreenViews()
.trackAppLifecycleEvents()
.trackAttributionData()
.ios()
.trackAdvertising()
.trackDeepLinks()
.android()
.disableDevicedId()
.setup("writeKey")
.setup('writeKey', {
using: [Mixpanel, GoogleAnalytics],
recordScreenViews: true,
trackAppLifecycleEvents: true,
trackAttributionData: true,

android: {
flushInterval: 60,
collectDeviceId: true
},
ios: {
trackAdvertising: true,
trackDeepLinks: true
}
})
.then(() =>
console.log('Analytics is ready')
)
Expand Down Expand Up @@ -94,11 +98,9 @@ In your code :
import analytics from '@segment/analytics-react-native'
import GoogleAnalytics from '@segment/analytics-react-native-google-analytics'

await analytics
.configure()
.using(GoogleAnalytics)
// ...
.setup('writeKey')
await analytics.setup('writeKey', {
using: [GoogleAnalytics]
})
```

#### Integrations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM

@ReactMethod
fun setup(options: ReadableMap) {
val android = options.getMap("android")
val builder = Analytics
.Builder(reactApplicationContext, android.getString("writeKey"))
.Builder(reactApplicationContext, options.getString("writeKey"))
.flushQueueSize(options.getInt("flushAt"))

if(options.getBoolean("recordScreenViews")) {
Expand Down
21 changes: 5 additions & 16 deletions packages/core/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

## Index

### Modules

* [ChainedConfiguration](modules/analytics.chainedconfiguration.md)

### Classes

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

### Interfaces

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

### Type aliases

* [Integration](#integration)
* [WriteKey](#writekey)

---

Expand All @@ -26,17 +25,7 @@
**Ƭ Integration**: * `function` | `object`
*

*Defined in analytics.ts:229*

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

### WriteKey

**Ƭ WriteKey**: * `string` &#124; `object`
*

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

___

83 changes: 44 additions & 39 deletions packages/core/docs/classes/analytics.client.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

* [alias](analytics.client.md#alias)
* [catch](analytics.client.md#catch)
* [configure](analytics.client.md#configure)
* [disable](analytics.client.md#disable)
* [enable](analytics.client.md#enable)
* [flush](analytics.client.md#flush)
Expand All @@ -25,6 +24,7 @@
* [middleware](analytics.client.md#middleware)
* [reset](analytics.client.md#reset)
* [screen](analytics.client.md#screen)
* [setup](analytics.client.md#setup)
* [track](analytics.client.md#track)
* [useNativeConfiguration](analytics.client.md#usenativeconfiguration)

Expand All @@ -38,7 +38,7 @@

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

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

Expand All @@ -54,7 +54,7 @@ ___

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

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

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

Expand All @@ -75,7 +75,7 @@ ___

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

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

Catch React-Native bridge errors

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

**Returns:** `this`

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

### configure

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

*Defined in analytics.ts:102*

Configure the Analytics module.

This method returns a fluent-style API to configure the SDK :

```js
analytics
.configure()
.using(Mixpanel, GoogleAnalytics)
.trackAppLifecycle()
.ios()
.trackDeepLinks()
.setup("YOUR_WRITE_KEY")
```

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

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

### disable

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

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

Completely disable the sending of any analytics data.

Expand All @@ -136,7 +111,7 @@ ___

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

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

Enable the sending of analytics data. Enabled by default.

Expand All @@ -151,7 +126,7 @@ ___

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

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

Trigger an upload of all queued events.

Expand All @@ -166,7 +141,7 @@ ___

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

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

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

Expand All @@ -188,7 +163,7 @@ ___

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

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

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

Expand All @@ -210,7 +185,7 @@ ___

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

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

Append a new middleware to the middleware chain.

Expand Down Expand Up @@ -248,7 +223,7 @@ ___

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

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

Reset any user state that is cached on the device.

Expand All @@ -263,7 +238,7 @@ ___

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

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

Record the screens or views your users see.

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

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

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

### setup

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

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

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

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

**Parameters:**

| Param | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| writeKey | `string` | - | Your Segment.io write key |
| `Default value` configuration | [Configuration](../interfaces/analytics.configuration.md) | {} | An optional [Configuration](../interfaces/analytics.configuration.md) object. |

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

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

### track

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

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

Record the actions your users perform.

Expand All @@ -307,7 +312,7 @@ ___

**useNativeConfiguration**(): `this`

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

Use the native configuration.

Expand Down
Loading

0 comments on commit 6a281f4

Please sign in to comment.