Skip to content

Fix creating device UUID from Expo #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions javascript/mixpanel-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class MixpanelMain {
this.core = MixpanelCore(storage);
this.core.initialize(token);
this.core.startProcessingQueue(token);
this.mixpanelPersistent = MixpanelPersistent.getInstance();
this.mixpanelPersistent = MixpanelPersistent.getInstance(storage, token);
}

async initialize(
Expand Down Expand Up @@ -196,7 +196,7 @@ export default class MixpanelMain {
if (!this.mixpanelPersistent.getDeviceId(token)) {
await this.mixpanelPersistent.loadIdentity(token);
}
return this.identity[token].deviceId;
return this.mixpanelPersistent.getDeviceId(token);
}

async getDistinctId(token) {
Expand Down
32 changes: 21 additions & 11 deletions javascript/mixpanel-persistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import {
import {AsyncStorageAdapter} from "./mixpanel-storage";
import uuid from "uuid";
import {MixpanelLogger} from "mixpanel-react-native/javascript/mixpanel-logger";
import {randomUUID} from 'expo-crypto';

export class MixpanelPersistent {
static instance;

static getInstance(storage) {
static getInstance(storage, token) {
if (!MixpanelPersistent.instance) {
MixpanelPersistent.instance = new MixpanelPersistent(
new AsyncStorageAdapter(storage)
);
MixpanelPersistent.initializationCompletePromise = MixpanelPersistent.instance.initializationCompletePromise();
MixpanelPersistent.initializationCompletePromise = MixpanelPersistent.instance.initializationCompletePromise(token);
}
return MixpanelPersistent.instance;
}
Expand Down Expand Up @@ -50,16 +51,25 @@ export class MixpanelPersistent {
}

async loadDeviceId(token) {
await this.storageAdapter
.getItem(getDeviceIdKey(token))
.then((deviceId) => {
if (!this._identity[token]) {
this._identity[token] = {};
}
this._identity[token].deviceId = deviceId;
});
if (!token) {
return;
}

const storageToken = await this.storageAdapter
.getItem(getDeviceIdKey(token));

if (!this._identity[token]) {
this._identity[token] = {};
}

this._identity[token].deviceId = storageToken;

if (!this._identity[token].deviceId) {
this._identity[token].deviceId = uuid.v4();
try {
this._identity[token].deviceId = randomUUID();
} catch (e) {
this._identity[token].deviceId = uuid.v4();
}
await this.storageAdapter.setItem(
getDeviceIdKey(token),
this._identity[token].deviceId
Expand Down
7 changes: 6 additions & 1 deletion javascript/mixpanel-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ export class AsyncStorageAdapter {
constructor(storage) {
if (!storage) {
try {
this.storage = require("@react-native-async-storage/async-storage");
const storageModule = require("@react-native-async-storage/async-storage");
if (storageModule.default) {
this.storage = storageModule.default
} else {
this.storage = storageModule
}
} catch {
console.error(
"[@RNC/AsyncStorage]: NativeModule: AsyncStorage is null. Please run 'npm install @react-native-async-storage/async-storage' or follow the Mixpanel guide to set up your own Storage class."
Expand Down
Loading