Skip to content

Commit

Permalink
drop amplitude
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon committed Jan 27, 2022
1 parent 39fb7ee commit bd71031
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
43 changes: 14 additions & 29 deletions packages/expo/cli/utils/analytics/rudderstackClient.ts
Expand Up @@ -3,7 +3,7 @@ import os from 'os';
import { URL } from 'url';
import { v4 as uuidv4 } from 'uuid';

import UserSettings from '../user/UserSettings';
import { EXPO_LOCAL, EXPO_STAGING, EXPO_NO_TELEMETRY } from '../env';

const PLATFORM_TO_ANALYTICS_PLATFORM: { [platform: string]: string } = {
darwin: 'Mac',
Expand All @@ -12,34 +12,17 @@ const PLATFORM_TO_ANALYTICS_PLATFORM: { [platform: string]: string } = {
};

let rudderstackClient: RudderAnalytics | null = null;
let userIdentified = false;
let identifier = false;
let identifyData: {
userId: string;
deviceId: string;
traits: Record<string, any>;
} | null = null;

export async function initAsync(): Promise<void> {
// TODO: remove after some time
const amplitudeEnabled = await UserSettings.getAsync('amplitudeEnabled', null);
if (amplitudeEnabled !== null) {
await UserSettings.setAsync('analyticsEnabled', amplitudeEnabled);
await UserSettings.deleteKeyAsync('amplitudeEnabled');
}
const amplitudeDeviceId = await UserSettings.getAsync('amplitudeDeviceId', null);
if (amplitudeDeviceId !== null) {
await UserSettings.setAsync('analyticsDeviceId', amplitudeDeviceId);
await UserSettings.deleteKeyAsync('amplitudeDeviceId');
}
// TODO: cut here
if (process.env.DISABLE_EAS_ANALYTICS) {
await UserSettings.setAsync('analyticsEnabled', false);
}

const analyticsEnabled = await UserSettings.getAsync('analyticsEnabled', true);
if (analyticsEnabled) {
if (EXPO_NO_TELEMETRY) {
const config =
process.env.EXPO_STAGING || process.env.EXPO_LOCAL
EXPO_STAGING || EXPO_LOCAL
? {
// staging environment
rudderstackWriteKey: '1wpX20Da4ltFGSXbPFYUL00Chb7',
Expand All @@ -62,10 +45,12 @@ export async function initAsync(): Promise<void> {
}

export async function setUserDataAsync(userId: string, traits: Record<string, any>): Promise<void> {
const savedDeviceId = await UserSettings.getAsync('analyticsDeviceId', null);
const UserSettings = await import('../user/UserSettings');

const savedDeviceId = await UserSettings.default.getAsync('analyticsDeviceId', null);
const deviceId = savedDeviceId ?? uuidv4();
if (!savedDeviceId) {
await UserSettings.setAsync('analyticsDeviceId', deviceId);
await UserSettings.default.setAsync('analyticsDeviceId', deviceId);
}

identifyData = {
Expand All @@ -74,7 +59,7 @@ export async function setUserDataAsync(userId: string, traits: Record<string, an
traits,
};

ensureUserIdentified();
ensureIdentified();
}

export async function flushAsync(): Promise<void> {
Expand All @@ -87,7 +72,7 @@ export function logEvent(name: string, properties: Record<string, any> = {}): vo
if (!rudderstackClient) {
return;
}
ensureUserIdentified();
ensureIdentified();

const { userId, deviceId } = identifyData ?? {};
const commonEventProperties = { source_version: process.env.__EXPO_VERSION, source: 'expo' };
Expand All @@ -101,8 +86,8 @@ export function logEvent(name: string, properties: Record<string, any> = {}): vo
});
}

function ensureUserIdentified(): void {
if (!rudderstackClient || userIdentified || !identifyData) {
function ensureIdentified(): void {
if (!rudderstackClient || identifier || !identifyData) {
return;
}

Expand All @@ -111,15 +96,15 @@ function ensureUserIdentified(): void {
anonymousId: identifyData.deviceId,
traits: identifyData.traits,
});
userIdentified = true;
identifier = true;
}

function getRudderStackContext(): Record<string, any> {
const platform = PLATFORM_TO_ANALYTICS_PLATFORM[os.platform()] || os.platform();
return {
os: { name: platform, version: os.release() },
device: { type: platform, model: platform },
app: { name: 'expo', version: process.env.__EXPO_VERSION ?? undefined },
app: { name: 'expo', version: process.env.__EXPO_VERSION },
};
}

Expand Down
3 changes: 3 additions & 0 deletions packages/expo/cli/utils/env.ts
Expand Up @@ -16,3 +16,6 @@ export const EXPO_LOCAL = boolish('EXPO_LOCAL', false);

/** Is running in non-interactive CI mode */
export const CI = boolish('CI', false);

/** Disable telemetry (analytics) */
export const EXPO_NO_TELEMETRY = boolish('EXPO_NO_TELEMETRY', false);
2 changes: 0 additions & 2 deletions packages/expo/cli/utils/user/UserSettings.ts
Expand Up @@ -27,8 +27,6 @@ const SETTINGS_FILE_PATH = path.join(getConfigDirectory(), 'user-settings.json')

export type UserSettingsData = {
appleId?: string;
amplitudeDeviceId?: string;
amplitudeEnabled?: boolean;
analyticsDeviceId?: string;
analyticsEnabled?: boolean;
};
Expand Down

0 comments on commit bd71031

Please sign in to comment.