Skip to content

Commit 988f382

Browse files
committed
feat(anonymousid): add a method to retrieve the anonymousId
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).
1 parent 16a8b20 commit 988f382

10 files changed

Lines changed: 50 additions & 81 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ vendor/
1212
.vscode/
1313

1414
packages/test-app/project
15+
16+
.classpath
17+
.project
18+
.settings/

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
128128
@ReactMethod
129129
fun disable() =
130130
analytics.optOut(true)
131+
132+
@ReactMethod
133+
fun getAnonymousId(callback: Callback) =
134+
callback.invoke(analytics.getAnalyticsContext().traits().anonymousId())
131135
}
132136

133137
private infix fun<T: ValueMap> T.from(source: ReadableMap): T {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [disable](analytics.client.md#disable)
2020
* [enable](analytics.client.md#enable)
2121
* [flush](analytics.client.md#flush)
22+
* [getAnonymousId](analytics.client.md#getanonymousid)
2223
* [group](analytics.client.md#group)
2324
* [identify](analytics.client.md#identify)
2425
* [middleware](analytics.client.md#middleware)
@@ -134,6 +135,25 @@ This is useful when you want to force all messages queued on the device to be up
134135

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

138+
___
139+
<a id="getanonymousid"></a>
140+
141+
### getAnonymousId
142+
143+
**getAnonymousId**(callback: *`function`*): `Promise`<`void`>
144+
145+
*Defined in [analytics.ts:305](https://github.com/segmentio/analytics-react-native/blob/master/packages/core/src/analytics.ts#L305)*
146+
147+
Retrieve the anonymousId.
148+
149+
**Parameters:**
150+
151+
| Name | Type |
152+
| ------ | ------ |
153+
| callback | `function` |
154+
155+
**Returns:** `Promise`<`void`>
156+
137157
___
138158
<a id="group"></a>
139159

packages/core/ios/RNAnalytics/RNAnalytics.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,10 @@ +(void)initialize {
137137
[SEGAnalytics.sharedAnalytics disable];
138138
}
139139

140+
RCT_EXPORT_METHOD(getAnonymousId:(RCTResponseSenderBlock)callback)
141+
{
142+
NSString *anonymousId = [SEGAnalytics.sharedAnalytics getAnonymousId];
143+
callback(@[anonymousId]);
144+
}
145+
140146
@end

packages/core/src/__mocks__/bridge.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export default {
33
disable: jest.fn(),
44
enable: jest.fn(),
55
flush: jest.fn(),
6+
getAnonymousId: jest.fn(),
67
group: jest.fn(),
78
identify: jest.fn(),
89
reset: jest.fn(),

packages/core/src/__tests__/analytics.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ it('does .flush()', testCall('flush'))
7979
it('does .enable()', testCall('enable'))
8080
it('does .disable()', testCall('disable'))
8181

82+
it('does .getAnonymousId()', () => testCall('getAnonymousId')(jest.fn()))
83+
8284
it('logs uncaught bridge errors', async () => {
8385
const error = {
8486
message: 'test-error'

packages/core/src/analytics.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ export module Analytics {
301301
await this.wrapper.run('disable', disable => disable())
302302
}
303303

304+
/** Retrieve the anonymousId. */
305+
public async getAnonymousId(callback: (anonymousId: string) => void) {
306+
await this.wrapper.run('getAnonymousId', getAnonymousId => getAnonymousId(callback))
307+
}
308+
304309
private handleError(error: Error) {
305310
const { handlers } = this
306311

packages/core/src/bridge.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface Bridge {
3737
flush(): Promise<void>
3838
enable(): Promise<void>
3939
disable(): Promise<void>
40+
getAnonymousId(callback: (anonymousId: string) => void): Promise<void>
4041
}
4142

4243
const bridge: Bridge = NativeModules.RNAnalytics

packages/test-app/App.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

packages/test-app/seed/App.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ const trackOrder = () => {
3333
})
3434
}
3535

36+
const logAnonymousId = () => {
37+
analytics.getAnonymousId((anonymousId: string) => {
38+
console.log(`anonymousId: ${anonymousId}`)
39+
})
40+
}
41+
3642
const buildId = 'CIRCLE_WORKFLOW_ID'
3743

3844
const testSuite = () =>
@@ -61,6 +67,7 @@ export default class App extends Component {
6167
<Button title="Flush" onPress={flush} />
6268
<Button title="Track: Pizza Eaten" onPress={pizzaEaten} />
6369
<Button title="Launch test suite" onPress={testSuite} />
70+
<Button title="Log anonymousId" onPress={logAnonymousId} />
6471
</View>
6572
)
6673
}

0 commit comments

Comments
 (0)