Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
lazy load analytics

Update prompts.ts

Update actions.ts
  • Loading branch information
EvanBacon committed Jan 27, 2022
1 parent c09589c commit 8fd8e42
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 72 deletions.
16 changes: 10 additions & 6 deletions packages/expo/cli/register/registerAsync.ts
Expand Up @@ -2,27 +2,31 @@ import openBrowserAsync from 'better-opn';

import { CI } from '../utils/env';
import { CommandError } from '../utils/errors';
import { learnMore } from '../utils/link';
import { ora } from '../utils/ora';

export async function registerAsync() {
const REGISTRATION_URL = `https://expo.dev/signup`;
if (CI) {
throw new CommandError(
'NON_INTERACTIVE',
`Cannot register an account in CI. Register an account at: ${REGISTRATION_URL}`
`Cannot register an account in CI. Use the EXPO_TOKEN environment variable to authenticate in CI (${learnMore(
'https://docs.expo.dev/accounts/programmatic-access/'
)})`
);
}

const spinner = ora(`Opening ${REGISTRATION_URL}`).start();
const registrationUrl = `https://expo.dev/signup`;

const spinner = ora(`Opening ${registrationUrl}`).start();
try {
const opened = openBrowserAsync(REGISTRATION_URL);
const opened = openBrowserAsync(registrationUrl);

if (opened) {
spinner.succeed(`Opened ${REGISTRATION_URL}`);
spinner.succeed(`Opened ${registrationUrl}`);
}
return;
} catch (error) {
spinner.fail(`Unable to open a web browser. Register an account at: ${REGISTRATION_URL}`);
spinner.fail(`Unable to open a web browser. Register an account at: ${registrationUrl}`);
throw error;
}
}
71 changes: 29 additions & 42 deletions packages/expo/cli/utils/analytics/rudderstackClient.ts
@@ -1,6 +1,5 @@
import RudderAnalytics from '@expo/rudder-sdk-node';
import os from 'os';
import { URL } from 'url';
import { v4 as uuidv4 } from 'uuid';

import { EXPO_LOCAL, EXPO_STAGING, EXPO_NO_TELEMETRY } from '../env';
Expand All @@ -12,40 +11,38 @@ const PLATFORM_TO_ANALYTICS_PLATFORM: { [platform: string]: string } = {
linux: 'Linux',
};

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

export async function initAsync(): Promise<void> {
if (EXPO_NO_TELEMETRY) {
const config =
EXPO_STAGING || EXPO_LOCAL
? {
// staging environment
rudderstackWriteKey: '1wpX20Da4ltFGSXbPFYUL00Chb7',
rudderstackDataPlaneURL: 'https://cdp.expo.dev',
}
: {
// prod environment
rudderstackWriteKey: '1wpXLFxmujq86etH6G6cc90hPcC',
rudderstackDataPlaneURL: 'https://cdp.expo.dev',
};

rudderstackClient = new RudderAnalytics(
config.rudderstackWriteKey,
new URL('/v1/batch', config.rudderstackDataPlaneURL).toString(),
{
flushInterval: 300,
}
);
function getClient(): RudderAnalytics {
if (client) {
return client;
}

client = new RudderAnalytics(
EXPO_STAGING || EXPO_LOCAL ? '1wpX20Da4ltFGSXbPFYUL00Chb7' : '1wpXLFxmujq86etH6G6cc90hPcC',
'https://cdp.expo.dev/v1/batch',
{
flushInterval: 300,
}
);

// Install flush on exit...
process.on('SIGINT', () => client?.flush?.());
process.on('SIGTERM', () => client?.flush?.());

return client;
}

export async function setUserDataAsync(userId: string, traits: Record<string, any>): Promise<void> {
if (EXPO_NO_TELEMETRY) {
return;
}
const savedDeviceId = await UserSettings.getAsync('analyticsDeviceId', null);
const deviceId = savedDeviceId ?? uuidv4();
if (!savedDeviceId) {
Expand All @@ -61,14 +58,8 @@ export async function setUserDataAsync(userId: string, traits: Record<string, an
ensureIdentified();
}

export async function flushAsync(): Promise<void> {
if (rudderstackClient) {
await rudderstackClient.flush();
}
}

export function logEvent(name: string, properties: Record<string, any> = {}): void {
if (!rudderstackClient) {
if (EXPO_NO_TELEMETRY) {
return;
}
ensureIdentified();
Expand All @@ -77,36 +68,32 @@ export function logEvent(name: string, properties: Record<string, any> = {}): vo
const commonEventProperties = { source_version: process.env.__EXPO_VERSION, source: 'expo' };

const identity = { userId: userId ?? undefined, anonymousId: deviceId ?? uuidv4() };
rudderstackClient.track({
getClient().track({
event: name,
properties: { ...properties, ...commonEventProperties },
...identity,
context: getRudderStackContext(),
context: getContext(),
});
}

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

rudderstackClient.identify({
getClient().identify({
userId: identifyData.userId,
anonymousId: identifyData.deviceId,
traits: identifyData.traits,
});
identifier = true;
identified = true;
}

function getRudderStackContext(): Record<string, any> {
function getContext(): 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 },
};
}

export enum AnalyticsEvent {
ACTION = 'action', // generic event type which is used to determine the 'daily active user' stat, include an `action: expo ${subcommand}` property inside of the event properties object
}
20 changes: 0 additions & 20 deletions packages/expo/cli/utils/api.ts
Expand Up @@ -73,23 +73,3 @@ export function getExpoApiBaseUrl(): string {
return `https://api.expo.dev`;
}
}

export function getExpoWebsiteBaseUrl(): string {
if (EXPO_STAGING) {
return `https://staging.expo.dev`;
} else if (EXPO_LOCAL) {
return `http://expo.test`;
} else {
return `https://expo.dev`;
}
}

export function getEASUpdateURL(projectId: string): string {
if (EXPO_STAGING) {
return new URL(projectId, `https://staging-u.expo.dev`).href;
} else if (EXPO_LOCAL) {
return new URL(`expo-updates/${projectId}`, `http://127.0.0.1:3000`).href;
} else {
return new URL(projectId, `https://u.expo.dev`).href;
}
}
1 change: 1 addition & 0 deletions packages/expo/cli/utils/prompts.ts
Expand Up @@ -68,6 +68,7 @@ export async function confirmAsync(
return value ?? null;
}

/** Select an option from a list of options. */
export async function selectAsync<T>(
message: string,
choices: ExpoChoice<T>[],
Expand Down
5 changes: 1 addition & 4 deletions packages/expo/cli/utils/user/actions.ts
Expand Up @@ -76,10 +76,7 @@ export async function showLoginPromptAsync({

/** Ensure the user is logged in, if not, prompt to login. */
export async function ensureLoggedInAsync(): Promise<Actor> {
let user: Actor | undefined;
try {
user = await getUserAsync();
} catch {}
let user = await getUserAsync().catch(() => null);

if (!user) {
Log.warn(chalk.yellow`An Expo user account is required to proceed.`);
Expand Down

0 comments on commit 8fd8e42

Please sign in to comment.