Skip to content

Commit 029242d

Browse files
authored
Merge pull request #134 from sidferreira/getDeviceId
Adds getDeviceId for React Native
2 parents d80afe7 + eaecb53 commit 029242d

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

android/src/main/java/com/mixpanel/reactnative/MixpanelReactNativeModule.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ public void getDistinctId(final String token, Promise promise) {
111111
}
112112
}
113113

114+
@ReactMethod
115+
public void getDeviceId(final String token, Promise promise) {
116+
MixpanelAPI instance = MixpanelAPI.getInstance(this.mReactContext, token);
117+
synchronized (instance) {
118+
promise.resolve(instance.getAnonymousId());
119+
}
120+
}
121+
114122
@ReactMethod
115123
public void track(final String token, final String eventName, ReadableMap properties, Promise promise) throws JSONException {
116124
MixpanelAPI instance = MixpanelAPI.getInstance(this.mReactContext, token);

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class Mixpanel {
3232
eventElapsedTime(eventName: string): Promise<number>;
3333
reset(): void;
3434
getDistinctId(): Promise<string>;
35+
getDeviceId(): Promise<string>;
3536
flush(): void;
3637
}
3738

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,24 @@ export class Mixpanel {
467467
return MixpanelReactNative.getDistinctId(this.token);
468468
}
469469

470+
/**
471+
* Returns the current device id of the device.
472+
* This id automatically generated by the library and regenerated when logout or reset is called.
473+
*
474+
* example of usage:
475+
* <pre>
476+
* <code>
477+
* const deviceId = await mixpanel.getDeviceId();
478+
* </code>
479+
* </pre>
480+
*
481+
* @return {Promise<string>} A Promise to the device id
482+
*
483+
*/
484+
getDeviceId() {
485+
return MixpanelReactNative.getDeviceId(this.token);
486+
}
487+
470488
/**
471489
* Push all queued Mixpanel events and People Analytics changes to Mixpanel servers.
472490
*

ios/MixpanelReactNative.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ @interface RCT_EXTERN_MODULE(MixpanelReactNative, NSObject)
4646

4747
RCT_EXTERN_METHOD(getDistinctId:(NSString *)token resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
4848

49+
RCT_EXTERN_METHOD(getDeviceId:(NSString *)token resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
50+
4951
// MARK: - Super Properties
5052

5153
RCT_EXTERN_METHOD(registerSuperProperties:(NSString *)token properties:(NSDictionary *)properties resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

ios/MixpanelReactNative.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ open class MixpanelReactNative: NSObject {
165165
let instance = MixpanelReactNative.getMixpanelInstance(token)
166166
resolve(instance?.distinctId)
167167
}
168+
169+
@objc
170+
func getDeviceId(_ token: String, resolver resolve: RCTPromiseResolveBlock,
171+
rejecter reject: RCTPromiseRejectBlock) -> Void {
172+
let instance = MixpanelReactNative.getMixpanelInstance(token)
173+
resolve(instance?.anonymousId)
174+
}
168175

169176
// MARK: - Super Properties
170177

0 commit comments

Comments
 (0)