Skip to content

Commit

Permalink
Merge pull request #557 from near/mixpanel-people-setup
Browse files Browse the repository at this point in the history
build person profile
  • Loading branch information
vgrichina authored Oct 3, 2020
2 parents 9b768ea + ca2612a commit 864ca9e
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions utils/eventtracking.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const MIXPANEL_TOKEN = 'e98aa9d6d259d9d78f20cb05cb54f5cb';

const chalk = require('chalk'); // colorize output
const chalk = require('chalk'); // colorize output
const crypto = require('crypto');
const mixpanel = require('mixpanel').init(MIXPANEL_TOKEN);
const near_cli_version = require('../package.json').version;
Expand All @@ -19,7 +19,11 @@ const getGitPodUserHash = () => {
if (!process.env.GITPOD_GIT_USER_EMAIL) {
return null;
}
return crypto.createHash('sha256').update(process.env.GITPOD_GIT_USER_EMAIL, 'utf8').digest('hex').toString();
return crypto
.createHash('sha256')
.update(process.env.GITPOD_GIT_USER_EMAIL, 'utf8')
.digest('hex')
.toString();
};

const shouldOptInByDefault = () => {
Expand All @@ -30,7 +34,10 @@ const shouldTrack = (shellSettings) => {
if (shouldOptInByDefault()) {
return true;
}
return TRACKING_ENABLED_KEY in shellSettings && shellSettings[TRACKING_ENABLED_KEY];
return (
TRACKING_ENABLED_KEY in shellSettings &&
shellSettings[TRACKING_ENABLED_KEY]
);
};

const track = async (eventType, eventProperties, options) => {
Expand All @@ -40,41 +47,49 @@ const track = async (eventType, eventProperties, options) => {
}
try {
const mixPanelProperties = {
distinct_id: isGitPod() ? getGitPodUserHash() : shellSettings[TRACKING_SESSION_ID_KEY],
distinct_id: isGitPod()
? getGitPodUserHash()
: shellSettings[TRACKING_SESSION_ID_KEY],
near_cli_version,
os: process.platform,
network_id: options.networkId,
node_url: options.nodeUrl,
wallet_url: options.walletUrl,
is_gitpod: isGitPod()
is_gitpod: isGitPod(),
};
Object.assign(mixPanelProperties, eventProperties);
await mixpanel.track(eventType, mixPanelProperties);
} catch (e) {
console.log('Warning: problem while sending developer event tracking data. This is not critical. Error: ', e);
console.log(
'Warning: problem while sending developer event tracking data. This is not critical. Error: ',
e
);
}
};

const getEventTrackingConsent = async () => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
output: process.stdout,
});
try {
for (let attempts = 0; attempts < 10; attempts++) {
const answer = await new Promise((resolve) => {
rl.question(
chalk`We would like to collect data on near-cli usage to improve developer experience.` +
chalk` We will never send private information. We only collect which commands are run via an anonymous identifier.` +
chalk`{bold.yellow Would you like to opt in (y/n)? }`,
chalk` We will never send private information. We only collect which commands are run via an anonymous identifier.` +
chalk`{bold.yellow Would you like to opt in (y/n)? }`,
async (consentToEventTracking) => {
if (consentToEventTracking == 'y' || consentToEventTracking == 'Y') {
if (consentToEventTracking.toLowerCase() == 'y') {
resolve(true);
} else if (consentToEventTracking == 'n' || consentToEventTracking == 'N') {
} else if (
consentToEventTracking.toLowerCase() == 'n'
) {
resolve(false);
}
resolve(undefined);
});
}
);
});
if (answer !== undefined) {
return answer;
Expand All @@ -90,11 +105,25 @@ const askForConsentIfNeeded = async (options) => {
const shellSettings = settings.getShellSettings();
// if the appropriate option is not in settings, ask now and save settings.
if (!(TRACKING_ENABLED_KEY in shellSettings)) {
shellSettings[TRACKING_ENABLED_KEY] = shouldOptInByDefault() || await getEventTrackingConsent();
shellSettings[TRACKING_SESSION_ID_KEY] = shellSettings[TRACKING_ENABLED_KEY] ? uuid.v4() : undefined;
shellSettings[TRACKING_ENABLED_KEY] =
shouldOptInByDefault() || (await getEventTrackingConsent());
shellSettings[TRACKING_SESSION_ID_KEY] = shellSettings[
TRACKING_ENABLED_KEY
]
? uuid.v4()
: undefined;
settings.saveShellSettings(shellSettings);
if (shellSettings[TRACKING_ENABLED_KEY]) {
track(module.exports.EVENT_ID_TRACKING_OPT_IN, {}, options);
await Promise.all([
track(module.exports.EVENT_ID_TRACKING_OPT_IN, {}, options),
mixpanel.people.set({
distinct_id: isGitPod()
? getGitPodUserHash()
: shellSettings[TRACKING_SESSION_ID_KEY],
network_id: options.networkId,
node_url: options.nodeUrl,
}),
]);
}
}
};
Expand All @@ -107,5 +136,5 @@ module.exports = {
EVENT_ID_CREATE_ACCOUNT_END: 'event_id_shell_create-account_end',
EVENT_ID_TRACKING_OPT_IN: 'event_id_tracking_opt_in',
EVENT_ID_LOGIN_END: 'event_id_shell_login_end',
EVENT_ID_ERROR: 'event_id_shell_error'
};
EVENT_ID_ERROR: 'event_id_shell_error',
};

0 comments on commit 864ca9e

Please sign in to comment.