Skip to content

Commit

Permalink
filter users events
Browse files Browse the repository at this point in the history
  • Loading branch information
oriefrati committed Jan 11, 2024
1 parent 83a366b commit 6691b60
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions examples/client/Locomotion/src/services/Mixpanel.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
/* eslint-disable class-methods-use-this */
import Config from 'react-native-config';
import { Mixpanel } from 'mixpanel-react-native';
import { getUniqueId } from 'react-native-device-info';
import { Alert } from 'react-native';
import { getDeviceId } from './device';

export const getElementName = props => props.testID || props.id;

const getRandomNumberByUuid = (uuid) => {
try {
const decimalValue = parseInt(uuid.substr(0, 8), 16);
const mappedNumber = (decimalValue % 100) + 1;
return mappedNumber;
} catch (err) {
console.log('getRandomNumberByUuid error', err);
return 0;
}
};

const { MIXPANEL_EVENTS_NUMBER } = Config;

const shouldTrackEvents = () => {
if (!MIXPANEL_EVENTS_NUMBER) {
return true;
}
const deviceUuid = getUniqueId();
const number = getRandomNumberByUuid(deviceUuid);
Alert.alert(deviceUuid, number.toString());
return number <= parseInt(MIXPANEL_EVENTS_NUMBER, 10);
};

class MixpanelService {
constructor() {
this.isInit = false;
this.mixpanel = {};
this.init();
this.shouldTrackEvents = shouldTrackEvents();
if (this.shouldTrackEvents) {
this.init();
}
}

init = async () => {
if (!this.isInit && Config.MIXPANEL_TOKEN) {
const trackAutomaticEvents = true;
this.mixpanel = new Mixpanel(Config.MIXPANEL_TOKEN, trackAutomaticEvents);

this.mixpanel.init();
this.mixpanel.setLoggingEnabled(true);
this.isInit = true;
}
};

setUser = async (user) => {
if (!this.isInit) return;

const uniqueId = (user && user.id) || getDeviceId();
this.user = user;
if (user && user.id) {
if (user && user.id && this.isInit) {
this.mixpanel.optInTracking();
this.mixpanel.identify(uniqueId);
this.mixpanel.getPeople().set({
Expand All @@ -37,6 +68,7 @@ class MixpanelService {
};

trackWithProperties = (event, props) => {
if (!this.isInit) return;
if (this.isInit && this.mixpanel) {
this.mixpanel.track(event,
{
Expand Down Expand Up @@ -64,6 +96,7 @@ class MixpanelService {
};

trackElementClick = (props, properties = {}) => {
if (!this.isInit) return;
const elmName = getElementName(props);
const eventName = `${elmName}`;
if (elmName) {
Expand All @@ -77,6 +110,7 @@ class MixpanelService {
};

registerSuperProperties = (properties) => {
if (!this.isInit) return;
this.mixpanel.registerSuperProperties(properties);
};

Expand Down

0 comments on commit 6691b60

Please sign in to comment.