Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zihejia committed Feb 28, 2024
1 parent a03310c commit 1378a19
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 44 deletions.
12 changes: 10 additions & 2 deletions Samples/MixpanelExpo/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
StyleSheet,
SafeAreaView,
} from "react-native";
import { Platform } from "react-native";

import { Mixpanel } from "mixpanel-react-native";

Expand All @@ -23,7 +24,7 @@ const App = () => {

const group = mixpanel.getGroup("company_id", 111);
const track = async () => {
await mixpanel.track("Track Event1!");
await mixpanel.track("Track Event!");
};

const identify = async () => {
Expand All @@ -38,6 +39,12 @@ const App = () => {
}, 2000);
};

const createAlias = async () => {
const distinctId = await mixpanel.getDistinctId();
alert(distinctId);
await mixpanel.alias("New Alias", distinctId);
};

const trackWProperties = async () => {
const properties = { "Cool Property": "Property Value" };
await mixpanel.track("Track event with property", properties);
Expand Down Expand Up @@ -228,7 +235,8 @@ const App = () => {
label: "Register Super Properties Once",
onPress: registerSuperPropertiesOnce,
},
{ id: "10", label: "Flush", onPress: flush },
{ id: "10", label: "Create Alias", onPress: createAlias },
{ id: "11", label: "Flush", onPress: flush },
],
},
{
Expand Down
5 changes: 4 additions & 1 deletion Samples/MixpanelExpo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"expo-status-bar": "~1.11.1",
"mixpanel-react-native": "github:mixpanel/mixpanel-react-native#js-core",
"react": "18.2.0",
"react-native": "0.73.4"
"react-native": "0.73.4",
"react-native-web": "~0.19.6",
"react-dom": "18.2.0",
"@expo/metro-runtime": "~3.1.3"
},
"devDependencies": {
"@babel/core": "^7.20.0"
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class Mixpanel {

if (!MixpanelReactNative) {
console.warn(
"MixpanelReactNative is not available, using JavaScript fallback. If you do not want to use the JavaScript fallback, please follow the guide on the Github repository: https://github.com/mixpanel/mixpanel-react-native."
"MixpanelReactNative is not available; using JavaScript mode. If you prefer not to use the JavaScript mode, please follow the guide in the GitHub repository: https://github.com/mixpanel/mixpanel-react-native."
);
this.mixpanelImpl = new MixpanelMain(token, trackAutomaticEvents);
} else {
Expand Down Expand Up @@ -253,7 +253,7 @@ export class Mixpanel {
}

/**
* The alias method creates an alias which Mixpanel will use to remap one id to another.
* @deprecated The alias method creates an alias which Mixpanel will use to remap one id to another.
* Multiple aliases can point to the same identifier.
*
* `mixpane.alias("New ID", mixpane.distinctId)`
Expand All @@ -272,7 +272,7 @@ export class Mixpanel {
if (!StringHelper.isValid(distinctId)) {
StringHelper.raiseError(PARAMS.DISTINCT_ID);
}
MixpanelReactNative.alias(this.token, alias, distinctId);
this.mixpanelImpl.alias(this.token, alias, distinctId);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions javascript/mixpanel-constants.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export const MixpanelType = {
EVENTS: '/track/',
USER: '/engage/',
GROUPS: '/groups/',
EVENTS: "/track/",
USER: "/engage/",
GROUPS: "/groups/",
};

export const getQueueKey = (token, type) => `MIXPANEL_${token}_${type}_QUEUE`;

export const getDeviceIdKey = token => `MIXPANEL_${token}_DEVICE_ID`;
export const getDistinctIdKey = token => `MIXPANEL_${token}_DISTINCT_ID`;
export const getUserIdKey = token => `MIXPANEL_${token}_USER_ID`;
export const getDeviceIdKey = (token) => `MIXPANEL_${token}_DEVICE_ID`;
export const getDistinctIdKey = (token) => `MIXPANEL_${token}_DISTINCT_ID`;
export const getUserIdKey = (token) => `MIXPANEL_${token}_USER_ID`;

export const getOptedOutKey = token => `MIXPANEL_${token}_OPT_OUT`;
export const getSuperPropertiesKey = token =>
export const getOptedOutKey = (token) => `MIXPANEL_${token}_OPT_OUT`;
export const getSuperPropertiesKey = (token) =>
`MIXPANEL_${token}_SUPER_PROPERTIES`;
export const getTimeEventsKey = token => `MIXPANEL_${token}_TIME_EVENTS`;
export const getTimeEventsKey = (token) => `MIXPANEL_${token}_TIME_EVENTS`;

export const defaultServerURL = `https://api.mixpanel.com`;
export const defaultBatchSize = 50;
export const defaultFlushInterval = 60 * 1000; // 60s
export const defaultFlushInterval = 10 * 1000; // 10s
78 changes: 54 additions & 24 deletions javascript/mixpanel-main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Platform } from "react-native";
import { MixpanelCore } from "./mixpanel-core";
import { MixpanelType } from "./mixpanel-constants";
import { MixpanelConfig } from "./mixpanel-config";
import { MixpanelPersistent } from "./mixpanel-persistent";
import { MixpanelLogger } from "mixpanel-react-native/javascript/mixpanel-logger";
import packageJson from "mixpanel-react-native/package.json";

export default class MixpanelMain {
constructor(token) {
Expand All @@ -25,7 +27,7 @@ export default class MixpanelMain {
if (trackAutomaticEvents) {
MixpanelLogger.log(
token,
`Automatic events tracking is not supported in JS mode. Ignoring.`
`Automatic events tracking is not supported in Javascript mode. Ignoring.`
);
}
await this.mixpanelPersistent.initializationCompletePromise(token);
Expand All @@ -35,9 +37,36 @@ export default class MixpanelMain {
await this.optInTracking(token);
}
this.setServerURL(token, serverURL);
if (superProperties) {
await this.registerSuperProperties(token, superProperties);
await this.registerSuperProperties(token, {
...superProperties,
});
}

getMetaData() {
const { OS, Version, constants } = Platform;
const { Brand, Manufacturer, Model } = constants;

let metadata = {
$os: OS,
$os_version: Version,
...JSON.parse(JSON.stringify(packageJson.metadata)),
$lib_version: packageJson.version,
};
if (OS === "ios") {
metadata = {
...metadata,
$manufacturer: "Apple",
};
} else if (OS === "android") {
metadata = {
...metadata,
$android_brand: Brand,
$android_manufacturer: Manufacturer,
$android_model: Model,
};
}

return metadata;
}

async reset(token) {
Expand All @@ -60,6 +89,7 @@ export default class MixpanelMain {
const eventProperties = Object.freeze({
token,
time: Date.now(),
...this.getMetaData(),
...superProperties,
...properties,
...identityProps,
Expand Down Expand Up @@ -143,6 +173,15 @@ export default class MixpanelMain {
});
}

async alias(token, alias, distinctId) {
MixpanelLogger.log(token, `Alias '${alias}' to '${distinctId}'`);
await this.track(token, "$create_alias", {
alias,
distinct_id: distinctId,
});
await this.identify(token, distinctId);
}

async getDeviceId(token) {
if (!this.mixpanelPersistent.getDeviceId(token)) {
await this.mixpanelPersistent.loadIdentity(token);
Expand Down Expand Up @@ -221,19 +260,8 @@ export default class MixpanelMain {

async clearSuperProperties(token) {
MixpanelLogger.log(token, `Clear super properties`);
// do not clear Mixpanel Properties
const excludeProperties = ["$lib_version", "mp_lib"];
let currentProperties = this.mixpanelPersistent.getSuperProperties(token);

const updatedProperties = Object.keys(currentProperties)
.filter((key) => excludeProperties.includes(key))
.reduce((obj, key) => {
obj[key] = currentProperties[key];
return obj;
}, {});

this._updateSuperProperties(token, updatedProperties);
MixpanelLogger.log(token, `Updated Super Properties:`, updatedProperties);
this._updateSuperProperties(token, {});
MixpanelLogger.log(token, `Updated Super Properties:`, {});
}

async timeEvent(token, eventName) {
Expand Down Expand Up @@ -477,20 +505,22 @@ export default class MixpanelMain {

async setGroup(token, groupKey, groupID) {
MixpanelLogger.log(token, `Set group: `, groupKey, groupID);
const properties = { [groupKey]: groupID };
const properties = { [groupKey]: [groupID] };
await this.registerSuperProperties(token, properties);
await this.set(token, properties);
}

async addGroup(token, groupKey, groupID) {
MixpanelLogger.log(token, `Add group: `, groupKey, groupID);
this.registerSuperProperties(token, {
[groupKey]: [
...(this.mixpanelPersistent.getSuperProperties(token)[groupKey] || []),
groupID,
],
});
await this.union(token, { [groupKey]: groupID });
const superProperties = this.mixpanelPersistent.getSuperProperties(token);
console.info("superProperties", superProperties);
const groupArray = superProperties[groupKey] || [];
if (!groupArray.includes(groupID)) {
this.registerSuperProperties(token, {
[groupKey]: [...groupArray, groupID],
});
}
await this.union(token, { [groupKey]: [groupID] });
}

async removeGroup(token, groupKey, groupID) {
Expand Down
4 changes: 4 additions & 0 deletions javascript/mixpanel-persistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ export class MixpanelPersistent {
await this.storage.removeItem(getDeviceIdKey(token));
await this.storage.removeItem(getDistinctIdKey(token));
await this.storage.removeItem(getUserIdKey(token));
await this.storage.removeItem(getSuperPropertiesKey(token));
await this.storage.removeItem(getTimeEventsKey(token));
await this.loadIdentity(token);
await this.loadSuperProperties(token);
await this.loadTimeEvents(token);
}
}
22 changes: 18 additions & 4 deletions javascript/mixpanel-storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import AsyncStorage from "@react-native-async-storage/async-storage";
import { MixpanelLogger } from "mixpanel-react-native/javascript/mixpanel-logger";

export class IMixpanelStorage {
async getItem(key) {
Expand All @@ -16,14 +17,27 @@ export class IMixpanelStorage {

export class AsyncStorageAdapter extends IMixpanelStorage {
async getItem(key) {
return AsyncStorage.getItem(key);
try {
return await AsyncStorage.getItem(key);
} catch {
MixpanelLogger.error("error getting item from AsyncStorage");
return null;
}
}

async setItem(key, value) {
return AsyncStorage.setItem(key, value);
try {
await AsyncStorage.setItem(key, value);
} catch {
MixpanelLogger.error("error setting item in AsyncStorage");
}
}

async removeItem(key) {
return AsyncStorage.removeItem(key);
try {
await AsyncStorage.removeItem(key);
} catch {
MixpanelLogger.error("error removing item in AsyncStorage");
}
}
}

0 comments on commit 1378a19

Please sign in to comment.