Skip to content

Commit

Permalink
migrate from mixpanel to segment
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov committed Jul 22, 2022
1 parent 4b4a787 commit a531f9c
Show file tree
Hide file tree
Showing 4 changed files with 5,872 additions and 5,654 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
"typescript": "^4.5.4"
},
"dependencies": {
"analytics-node": "^6.1.0",
"ascii-table": "0.0.9",
"bn.js": "^5.1.1",
"bs58": "^4.0.1",
"chalk": "^4.0.0",
"flagged-respawn": "^1.0.1",
"is-ci": "^2.0.0",
"jest-environment-node": "^27.0.6",
"mixpanel": "^0.13.0",
"ncp": "^2.0.0",
"near-api-js": "^0.44.2",
"near-seed-phrase": "^0.2.0",
Expand Down
46 changes: 28 additions & 18 deletions utils/eventtracking.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const MIXPANEL_TOKEN = 'e98aa9d6d259d9d78f20cb05cb54f5cb';
const SEGMENT_WRITE_KEY = 'vErt52x0024wPxUfLDZIKcYRPQgh6NyT';

const Analytics = require('analytics-node');
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;
const settings = require('./settings');
const { askYesNoQuestion } = require('./readline');
const uuid = require('uuid');

const analytics = new Analytics(SEGMENT_WRITE_KEY);

const isGitPod = () => {
return !!process.env.GITPOD_WORKSPACE_URL;
};
Expand Down Expand Up @@ -39,7 +41,7 @@ const shouldTrackID = (shellSettings) => {
return !!shellSettings.trackingAccountID;
};

const getMixpanelID = (shellSettings) => isGitPod() ? getGitPodUserHash() : shellSettings.trackingSessionId;
const getSegmentID = (shellSettings) => isGitPod() ? getGitPodUserHash() : shellSettings.trackingSessionId;

const track = async (eventType, eventProperties, options) => {
try {
Expand All @@ -48,19 +50,20 @@ const track = async (eventType, eventProperties, options) => {
return;
}

if (shouldTrackID(shellSettings)) {
if (options.accountId && shouldTrackID(shellSettings)) {
const accountID = options.accountId;
const id = getMixpanelID(shellSettings);
await Promise.all([
mixpanel.alias(accountID, id),
mixpanel.people.set(id, { account_id: accountID })
]);
const id = getSegmentID(shellSettings);
analytics.alias({ previousId: accountID, userId: id });
analytics.identify({
userId: id,
traits: { account_id: accountID }
});
}

const user_country = await getUserCountry();

const mixPanelProperties = {
distinct_id: getMixpanelID(shellSettings),
const segmentProperties = {
distinct_id: getSegmentID(shellSettings),
near_cli_version,
user_country,
os: process.platform,
Expand All @@ -70,13 +73,20 @@ const track = async (eventType, eventProperties, options) => {
is_gitpod: isGitPod(),
timestamp: new Date()
};
Object.assign(mixPanelProperties, eventProperties);
await Promise.all([mixpanel.track(eventType, mixPanelProperties),
mixpanel.people.set(mixPanelProperties.distinct_id, {
Object.assign(segmentProperties, eventProperties);
analytics.track({
userId: segmentProperties.distinct_id,
event: eventType,
properties: segmentProperties
});
analytics.identify({
userId: segmentProperties.distinct_id,
traits: {
deployed_contracts: 0,
network_id: options.networkId,
node_url: options.nodeUrl,
})]);
node_url: options.nodeUrl
}
});
} catch (e) {
console.warn(
'Warning: problem while sending developer event tracking data. This is not critical. Error: ',
Expand Down Expand Up @@ -117,8 +127,8 @@ const askForConsentIfNeeded = async (options) => {

const trackDeployedContract = async () => {
const shellSettings = settings.getShellSettings();
const id = getMixpanelID(shellSettings);
await mixpanel.people.increment(id, 'deployed_contracts');
const id = getSegmentID(shellSettings);
// TODO: find a way to increment a Segment analytic property
};

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion utils/exit-on-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function getNonPrivateDataFromCmdlineOpts(options) {
};
}

// This is a workaround to get Mixpanel to log a crash
// This is a workaround to get Segment to log a crash
process.on('exit', () => {
const crashEventProperties = {
near_cli_command: process.env.NEAR_CLI_ERROR_LAST_COMMAND,
Expand Down
Loading

0 comments on commit a531f9c

Please sign in to comment.