Skip to content

Commit eaecb53

Browse files
committed
Adds getDeviceId for React Native
1 parent 3bf1969 commit eaecb53

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
@@ -30,6 +30,7 @@ export class Mixpanel {
3030
eventElapsedTime(eventName: string): Promise<number>;
3131
reset(): void;
3232
getDistinctId(): Promise<string>;
33+
getDeviceId(): Promise<string>;
3334
flush(): void;
3435
}
3536

index.js

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

455+
/**
456+
* Returns the current device id of the device.
457+
* This id automatically generated by the library and regenerated when logout or reset is called.
458+
*
459+
* example of usage:
460+
* <pre>
461+
* <code>
462+
* const deviceId = await mixpanel.getDeviceId();
463+
* </code>
464+
* </pre>
465+
*
466+
* @return {Promise<string>} A Promise to the device id
467+
*
468+
*/
469+
getDeviceId() {
470+
return MixpanelReactNative.getDeviceId(this.token);
471+
}
472+
455473
/**
456474
* Push all queued Mixpanel events and People Analytics changes to Mixpanel servers.
457475
*

ios/MixpanelReactNative.m

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

4545
RCT_EXTERN_METHOD(getDistinctId:(NSString *)token resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
4646

47+
RCT_EXTERN_METHOD(getDeviceId:(NSString *)token resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
48+
4749
// MARK: - Super Properties
4850

4951
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
@@ -155,6 +155,13 @@ open class MixpanelReactNative: NSObject {
155155
let instance = MixpanelReactNative.getMixpanelInstance(token)
156156
resolve(instance?.distinctId)
157157
}
158+
159+
@objc
160+
func getDeviceId(_ token: String, resolver resolve: RCTPromiseResolveBlock,
161+
rejecter reject: RCTPromiseRejectBlock) -> Void {
162+
let instance = MixpanelReactNative.getMixpanelInstance(token)
163+
resolve(instance?.anonymousId)
164+
}
158165

159166
// MARK: - Super Properties
160167

0 commit comments

Comments
 (0)