Skip to content

Commit

Permalink
feat(@angular/cli): allows analytics sharing with a third party
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl authored and Keen Yee Liau committed Mar 26, 2019
1 parent c42c82f commit 6e5095b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 34 deletions.
67 changes: 56 additions & 11 deletions packages/angular/cli/models/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,39 +448,43 @@ export async function promptProjectAnalytics(force = false): Promise<boolean> {


/**
* Get the global analytics setting for the user. This returns a string for UID, false if the user
* opted out of analytics, true if the user wants to stay anonymous (no client id), and undefined
* if the user has not been prompted yet.
* Get the global analytics object for the user. This returns an instance of UniversalAnalytics,
* or undefined if analytics are disabled.
*
* If any problem happens, it is considered the user has been opting out of analytics.
*/
export function getGlobalAnalytics(): string | boolean | undefined {
export function getGlobalAnalytics(): UniversalAnalytics | undefined {
analyticsDebug('getGlobalAnalytics');
const propertyId = 'UA-8594346-29';

if ('NG_CLI_ANALYTICS' in process.env) {
if (process.env['NG_CLI_ANALYTICS'] == 'false' || process.env['NG_CLI_ANALYTICS'] == '') {
analyticsDebug('NG_CLI_ANALYTICS is false');

return false;
return undefined;
}
if (process.env['NG_CLI_ANALYTICS'] === 'ci') {
analyticsDebug('Running in CI mode');

return 'ci';
return new UniversalAnalytics(propertyId, 'ci');
}
}

// If anything happens we just keep the NOOP analytics.
try {
const globalWorkspace = getWorkspace('global');
const analyticsConfig = globalWorkspace
const analyticsConfig: string | undefined | null | { uid?: string } = globalWorkspace
&& globalWorkspace.getCli()
&& globalWorkspace.getCli()['analytics'];
analyticsDebug('Client Analytics config found: %j', analyticsConfig);

if (analyticsConfig === false) {
return false;
analyticsDebug('Analytics disabled. Ignoring all analytics.');

return undefined;
} else if (analyticsConfig === undefined || analyticsConfig === null) {
analyticsDebug('Analytics settings not found. Ignoring all analytics.');

// globalWorkspace can be null if there is no file. analyticsConfig would be null in this
// case. Since there is no file, the user hasn't answered and the expected return value is
// undefined.
Expand All @@ -493,13 +497,54 @@ export function getGlobalAnalytics(): string | boolean | undefined {
uid = analyticsConfig['uid'];
}

analyticsDebug('client id: %s', uid);
analyticsDebug('client id: %j', uid);
if (uid == undefined) {
return undefined;
}

return uid;
return new UniversalAnalytics(propertyId, uid);
}
} catch (err) {
analyticsDebug('Error happened during reading of analytics config: %s', err.message);

return false;
return undefined;
}
}


/**
* Return the usage analytics sharing setting, which is either a property string (GA-XXXXXXX-XX),
* or undefined if no sharing.
*/
export function getSharedAnalytics(): UniversalAnalytics | undefined {
analyticsDebug('getSharedAnalytics');

const envVarName = 'NG_CLI_ANALYTICS_SHARE';
if (envVarName in process.env) {
if (process.env[envVarName] == 'false' || process.env[envVarName] == '') {
analyticsDebug('NG_CLI_ANALYTICS is false');

return undefined;
}
}

// If anything happens we just keep the NOOP analytics.
try {
const globalWorkspace = getWorkspace('global');
const analyticsConfig = globalWorkspace
&& globalWorkspace.getCli()
&& globalWorkspace.getCli()['analyticsSharing'];

if (!analyticsConfig || !analyticsConfig.tracking || !analyticsConfig.uuid) {
return undefined;
} else {
analyticsDebug('Analytics sharing info: %j', analyticsConfig);

return new UniversalAnalytics(analyticsConfig.tracking, analyticsConfig.uuid);
}
} catch (err) {
analyticsDebug('Error happened during reading of analytics sharing config: %s', err.message);

return undefined;
}
}
38 changes: 15 additions & 23 deletions packages/angular/cli/models/command-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { readFileSync } from 'fs';
import { dirname, join, resolve } from 'path';
import { findUp } from '../utilities/find-up';
import { parseJsonSchemaToCommandDescription } from '../utilities/json-schema';
import { UniversalAnalytics, getGlobalAnalytics } from './analytics';
import { UniversalAnalytics, getGlobalAnalytics, getSharedAnalytics } from './analytics';
import { Command } from './command';
import {
CommandDescription,
Expand All @@ -37,30 +37,22 @@ export interface CommandMapOptions {
}


/**
* Create the analytics instance.
* @private
*/
async function _createAnalytics(): Promise<analytics.Analytics> {
const config = getGlobalAnalytics();

switch (config) {
case undefined:
case false:
analyticsDebug('Analytics disabled. Ignoring all analytics.');

return new analytics.NoopAnalytics();

case true:
analyticsDebug('Analytics enabled, anonymous user.');

return new UniversalAnalytics('UA-8594346-29', '');

case 'ci':
analyticsDebug('Logging analytics as CI.');

return new UniversalAnalytics('UA-8594346-29', 'ci');

default:
analyticsDebug('Analytics enabled. User ID: %j', config);

return new UniversalAnalytics('UA-8594346-29', config);
const maybeSharedAnalytics = getSharedAnalytics();

if (config && maybeSharedAnalytics) {
return new analytics.MultiAnalytics([config, maybeSharedAnalytics]);
} else if (config) {
return config;
} else if (maybeSharedAnalytics) {
return maybeSharedAnalytics;
} else {
return new analytics.NoopAnalytics();
}
}

Expand Down

0 comments on commit 6e5095b

Please sign in to comment.