{"size": 5294, "ext": "js", "max_stars_repo_path": "node_modules/decentraland/dist/utils/analytics.js", "max_stars_repo_name": "sirivus/studioonline", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "node_modules/decentraland/dist/utils/analytics.js", "max_issues_repo_name": "sirivus/studioonline", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "node_modules/decentraland/dist/utils/analytics.js", "max_forks_repo_name": "sirivus/studioonline", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 46.4385964912, "max_line_length": 222, "alphanum_fraction": 0.6501700038}
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.finishPendingTracking = exports.Analytics = exports.analytics = void 0;
const uuid_1 = require("uuid");
const analytics_node_1 = __importDefault(require("analytics-node"));
const config_1 = require("../config");
const moduleHelpers_1 = require("./moduleHelpers");
const logging_1 = require("./logging");
const chalk_1 = __importDefault(require("chalk"));
// Setup segment.io
const SINGLEUSER = 'cli-user';
const ANONYMOUS_DATA_QUESTION = 'Send Anonymous data';
// eslint-disable-next-line @typescript-eslint/no-namespace
var Analytics;
(function (Analytics) {
    Analytics.sceneCreated = (properties) => trackAsync('Scene created', properties);
    Analytics.preview = (properties) => trackAsync('Preview started', properties);
    Analytics.sceneDeploy = (properties) => trackAsync('Scene deploy started', properties);
    Analytics.sceneDeploySuccess = (properties) => trackAsync('Scene deploy success', properties);
    Analytics.sceneLink = (properties) => trackAsync('Scene ethereum link started', properties);
    Analytics.sceneLinkSuccess = (properties) => trackAsync('Scene ethereum link succeeded', properties);
    Analytics.deploy = (properties) => trackAsync('Scene deploy requested', properties);
    Analytics.pinRequest = (properties) => trackAsync('Pin requested', properties);
    Analytics.pinSuccess = (properties) => trackAsync('Pin success', properties);
    Analytics.infoCmd = (properties) => trackAsync('Info command', properties);
    Analytics.statusCmd = (properties) => trackAsync('Status command', properties);
    Analytics.sendData = (shareData) => trackAsync(ANONYMOUS_DATA_QUESTION, { shareData });
    async function identify(devId) {
        exports.analytics.identify({
            userId: SINGLEUSER,
            traits: {
                os: process.platform,
                createdAt: new Date().getTime(),
                isCI: process.env.CI === 'true' ||
                    (process.argv.includes('--ci') && process.argv.includes('--c')),
                devId
            }
        });
    }
    Analytics.identify = identify;
    async function reportError(workingDir, type, message, stackTrace) {
        return track('Error', {
            errorType: type,
            message,
            stackTrace
        }, workingDir);
    }
    Analytics.reportError = reportError;
    async function requestPermission() {
        const { fileExists, segmentKey } = (0, config_1.getConfig)();
        if (!segmentKey)
            return;
        exports.analytics = new analytics_node_1.default(segmentKey);
        if (!fileExists) {
            console.log(chalk_1.default.dim(`Decentraland CLI sends anonymous usage stats to improve their products, if you want to disable it change the configuration at ${chalk_1.default.bold('~/.dclinfo')}\n`));
            const newUserId = (0, uuid_1.v4)();
            await (0, config_1.createDCLInfo)({ userId: newUserId, trackStats: true });
            (0, logging_1.debug)(`${chalk_1.default.bold('.dclinfo')} file created`);
            await identify(newUserId);
            Analytics.sendData(true);
        }
    }
    Analytics.requestPermission = requestPermission;
})(Analytics = exports.Analytics || (exports.Analytics = {}));
/**
 * Tracks an specific event using the Segment API
 * @param eventName The name of the event to be tracked
 * @param properties Any object containing serializable data
 */
async function track(eventName, properties = {}, workingDir) {
    const { userId, trackStats } = (0, config_1.getConfig)();
    if (!(await (0, moduleHelpers_1.isOnline)())) {
        return null;
    }
    return new Promise(async (resolve) => {
        const dclApiVersion = await (0, moduleHelpers_1.getInstalledVersion)(workingDir, 'decentraland-api');
        const newProperties = Object.assign(Object.assign({}, properties), { os: process.platform, nodeVersion: process.version, cliVersion: (0, moduleHelpers_1.getInstalledCLIVersion)(), isCI: process.env.CI === 'true' ||
                (process.argv.includes('--ci') && process.argv.includes('--c')), devId: userId });
        const shouldTrack = trackStats || eventName === ANONYMOUS_DATA_QUESTION;
        if (!shouldTrack) {
            resolve();
        }
        // Some commands may be running outside of a DCL project
        if (dclApiVersion) {
            newProperties.dclApiVersion = dclApiVersion;
        }
        const event = {
            userId: SINGLEUSER,
            event: eventName,
            properties: newProperties
        };
        try {
            exports.analytics.track(event, () => {
                resolve();
            });
        }
        catch (e) {
            resolve();
        }
    });
}
const pendingTracking = [];
function trackAsync(eventName, properties = {}) {
    const pTracking = track(eventName, properties).then().catch(logging_1.debug);
    pendingTracking.push(pTracking);
}
async function finishPendingTracking() {
    return Promise.all(pendingTracking);
}
exports.finishPendingTracking = finishPendingTracking;
//# sourceMappingURL=analytics.js.map