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

Commit

Permalink
feat(anonymousid): add a method to retrieve the anonymousId
Browse files Browse the repository at this point in the history
This adds a `getAnonymousId` method to retrieve the current users anonymousId.

It uses `[SEGAnalytics.sharedAnalytics getAnonymousId]` on iOS and `analytics.getAnalyticsContext().traits().anonymousId()` on Android.

Usage:

```
analytics.getAnonymousId((anonymousId: string) => {
  console.log(`anonymousId: ${anonymousId}`)
})
```

Also adds an example to the seed app (demo https://cloudup.com/c0D76MNLEF9).
  • Loading branch information
f2prateek committed Apr 5, 2019
1 parent 16a8b20 commit 988f382
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 81 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ vendor/
.vscode/

packages/test-app/project

.classpath
.project
.settings/
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
@ReactMethod
fun disable() =
analytics.optOut(true)

@ReactMethod
fun getAnonymousId(callback: Callback) =
callback.invoke(analytics.getAnalyticsContext().traits().anonymousId())
}

private infix fun<T: ValueMap> T.from(source: ReadableMap): T {
Expand Down
20 changes: 20 additions & 0 deletions packages/core/docs/classes/analytics.client.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [disable](analytics.client.md#disable)
* [enable](analytics.client.md#enable)
* [flush](analytics.client.md#flush)
* [getAnonymousId](analytics.client.md#getanonymousid)
* [group](analytics.client.md#group)
* [identify](analytics.client.md#identify)
* [middleware](analytics.client.md#middleware)
Expand Down Expand Up @@ -134,6 +135,25 @@ This is useful when you want to force all messages queued on the device to be up

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

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

### getAnonymousId

**getAnonymousId**(callback: *`function`*): `Promise`<`void`>

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

Retrieve the anonymousId.

**Parameters:**

| Name | Type |
| ------ | ------ |
| callback | `function` |

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

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

Expand Down
6 changes: 6 additions & 0 deletions packages/core/ios/RNAnalytics/RNAnalytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,10 @@ +(void)initialize {
[SEGAnalytics.sharedAnalytics disable];
}

RCT_EXPORT_METHOD(getAnonymousId:(RCTResponseSenderBlock)callback)
{
NSString *anonymousId = [SEGAnalytics.sharedAnalytics getAnonymousId];
callback(@[anonymousId]);
}

@end
1 change: 1 addition & 0 deletions packages/core/src/__mocks__/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default {
disable: jest.fn(),
enable: jest.fn(),
flush: jest.fn(),
getAnonymousId: jest.fn(),
group: jest.fn(),
identify: jest.fn(),
reset: jest.fn(),
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/__tests__/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ it('does .flush()', testCall('flush'))
it('does .enable()', testCall('enable'))
it('does .disable()', testCall('disable'))

it('does .getAnonymousId()', () => testCall('getAnonymousId')(jest.fn()))

it('logs uncaught bridge errors', async () => {
const error = {
message: 'test-error'
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ export module Analytics {
await this.wrapper.run('disable', disable => disable())
}

/** Retrieve the anonymousId. */
public async getAnonymousId(callback: (anonymousId: string) => void) {
await this.wrapper.run('getAnonymousId', getAnonymousId => getAnonymousId(callback))
}

private handleError(error: Error) {
const { handlers } = this

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface Bridge {
flush(): Promise<void>
enable(): Promise<void>
disable(): Promise<void>
getAnonymousId(callback: (anonymousId: string) => void): Promise<void>
}

const bridge: Bridge = NativeModules.RNAnalytics
Expand Down
81 changes: 0 additions & 81 deletions packages/test-app/App.js

This file was deleted.

7 changes: 7 additions & 0 deletions packages/test-app/seed/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const trackOrder = () => {
})
}

const logAnonymousId = () => {
analytics.getAnonymousId((anonymousId: string) => {
console.log(`anonymousId: ${anonymousId}`)
})
}

const buildId = 'CIRCLE_WORKFLOW_ID'

const testSuite = () =>
Expand Down Expand Up @@ -61,6 +67,7 @@ export default class App extends Component {
<Button title="Flush" onPress={flush} />
<Button title="Track: Pizza Eaten" onPress={pizzaEaten} />
<Button title="Launch test suite" onPress={testSuite} />
<Button title="Log anonymousId" onPress={logAnonymousId} />
</View>
)
}
Expand Down

0 comments on commit 988f382

Please sign in to comment.